1.RedisåMemcachedçåºå«
2.å¦ä½å¦å¥½C++å¢?源码小可乐源码
RedisåMemcachedçåºå«
Redisçä½è Salvatore Sanfilippoæ¾ç»å¯¹è¿ä¸¤ç§åºäºå åçæ°æ®åå¨ç³»ç»è¿è¡è¿æ¯è¾ï¼1ãRedisæ¯ææå¡å¨ç«¯çæ°æ®æä½ï¼Redisç¸æ¯Memcachedæ¥è¯´ï¼æ¥ææ´å¤çæ°æ®ç»æå并æ¯ææ´ä¸°å¯çæ°æ®æä½ï¼é常å¨Memcachedéï¼ä½ éè¦å°æ°æ®æ¿å°å®¢æ·ç«¯æ¥è¿è¡ç±»ä¼¼çä¿®æ¹åsetåå»ãè¿å¤§å¤§å¢å äºç½ç»IOç次æ°åæ°æ®ä½ç§¯ãå¨Redisä¸ï¼è¿äºå¤æçæä½é常åä¸è¬çGET/SETä¸æ ·é«æãæ以ï¼å¦æéè¦ç¼åè½å¤æ¯ææ´å¤æçç»æåæä½ï¼é£ä¹Redisä¼æ¯ä¸éçéæ©ã
2ãå å使ç¨æç对æ¯ï¼ä½¿ç¨ç®åçkey-valueåå¨çè¯ï¼Memcachedçå åå©ç¨çæ´é«ï¼èå¦æRediséç¨hashç»ææ¥åkey-valueåå¨ï¼ç±äºå ¶ç»åå¼çå缩ï¼å ¶å åå©ç¨çä¼é«äºMemcachedã
3ãæ§è½å¯¹æ¯ï¼ç±äºRedisåªä½¿ç¨åæ ¸ï¼èMemcachedå¯ä»¥ä½¿ç¨å¤æ ¸ï¼æ以平åæ¯ä¸ä¸ªæ ¸ä¸Rediså¨åå¨å°æ°æ®æ¶æ¯Memcachedæ§è½æ´é«ãèå¨k以ä¸çæ°æ®ä¸ï¼Memcachedæ§è½è¦é«äºRedisï¼è½ç¶Redisæè¿ä¹å¨åå¨å¤§æ°æ®çæ§è½ä¸è¿è¡ä¼åï¼ä½æ¯æ¯èµ·Memcachedï¼è¿æ¯ç¨æéè²ã
å ·ä½ä¸ºä»ä¹ä¼åºç°ä¸é¢çç»è®ºï¼ä»¥ä¸ä¸ºæ¶éå°çèµæï¼
1ãæ°æ®ç±»åæ¯æä¸å
ä¸Memcachedä» æ¯æç®åçkey-valueç»æçæ°æ®è®°å½ä¸åï¼Redisæ¯æçæ°æ®ç±»åè¦ä¸°å¯å¾å¤ãæ为常ç¨çæ°æ®ç±»å主è¦ç±äºç§ï¼StringãHashãListãSetåSorted SetãRediså é¨ä½¿ç¨ä¸ä¸ªredisObject对象æ¥è¡¨ç¤ºææçkeyåvalueãredisObjectæ主è¦çä¿¡æ¯å¦å¾æ示ï¼
type代表ä¸ä¸ªvalueå¯¹è±¡å ·ä½æ¯ä½ç§æ°æ®ç±»åï¼encodingæ¯ä¸åæ°æ®ç±»åå¨rediså é¨çåå¨æ¹å¼ï¼æ¯å¦ï¼type=string代表valueåå¨çæ¯ä¸ä¸ªæ®éå符串ï¼é£ä¹å¯¹åºçencodingå¯ä»¥æ¯rawæè æ¯intï¼å¦ææ¯intå代表å®é rediså é¨æ¯ææ°å¼åç±»åå¨å表示è¿ä¸ªå符串çï¼å½ç¶åææ¯è¿ä¸ªå符串æ¬èº«å¯ä»¥ç¨æ°å¼è¡¨ç¤ºï¼æ¯å¦:ââ³ ââè¿æ ·çå符串ãåªææå¼äºRedisçèæå ååè½ï¼vmå段å段æä¼çæ£çåé å åï¼è¯¥åè½é»è®¤æ¯å ³éç¶æçã
1ï¼String
常ç¨å½ä»¤ï¼set/get/decr/incr/mgetçï¼
åºç¨åºæ¯ï¼Stringæ¯æ常ç¨çä¸ç§æ°æ®ç±»åï¼æ®éçkey/valueåå¨é½å¯ä»¥å½ä¸ºæ¤ç±»ï¼
å®ç°æ¹å¼ï¼Stringå¨rediså é¨åå¨é»è®¤å°±æ¯ä¸ä¸ªå符串ï¼è¢«redisObjectæå¼ç¨ï¼å½éå°incrãdecrçæä½æ¶ä¼è½¬ææ°å¼åè¿è¡è®¡ç®ï¼æ¤æ¶redisObjectçencodingå段为intã
2ï¼Hash
常ç¨å½ä»¤ï¼hget/hset/hgetallç
åºç¨åºæ¯ï¼æ们è¦åå¨ä¸ä¸ªç¨æ·ä¿¡æ¯å¯¹è±¡æ°æ®ï¼å ¶ä¸å æ¬ç¨æ·IDãç¨æ·å§åãå¹´é¾åçæ¥ï¼éè¿ç¨æ·IDæ们å¸æè·å该ç¨æ·çå§åæè å¹´é¾æè çæ¥ï¼
å®ç°æ¹å¼ï¼RedisçHashå®é æ¯å é¨åå¨çValue为ä¸ä¸ªHashMapï¼å¹¶æä¾äºç´æ¥ååè¿ä¸ªMapæåçæ¥å£ãå¦å¾æ示ï¼Keyæ¯ç¨æ·ID, valueæ¯ä¸ä¸ªMapãè¿ä¸ªMapçkeyæ¯æåçå±æ§åï¼valueæ¯å±æ§å¼ãè¿æ ·å¯¹æ°æ®çä¿®æ¹åååé½å¯ä»¥ç´æ¥éè¿å ¶å é¨MapçKey(Redisé称å é¨Mapçkey为field), ä¹å°±æ¯éè¿ key(ç¨æ·ID) + field(å±æ§æ ç¾) å°±å¯ä»¥æä½å¯¹åºå±æ§æ°æ®ãå½åHashMapçå®ç°æ两ç§æ¹å¼ï¼å½HashMapçæåæ¯è¾å°æ¶Redis为äºèçå åä¼éç¨ç±»ä¼¼ä¸ç»´æ°ç»çæ¹å¼æ¥ç´§ååå¨ï¼èä¸ä¼éç¨çæ£çHashMapç»æï¼è¿æ¶å¯¹åºçvalueçredisObjectçencoding为zipmapï¼å½æåæ°éå¢å¤§æ¶ä¼èªå¨è½¬æçæ£çHashMap,æ¤æ¶encoding为htã
请ç¹å»è¾å ¥å¾çæè¿°
3ï¼List
常ç¨å½ä»¤ï¼lpush/rpush/lpop/rpop/lrangeçï¼
åºç¨åºæ¯ï¼Redis listçåºç¨åºæ¯é常å¤ï¼ä¹æ¯Redisæéè¦çæ°æ®ç»æä¹ä¸ï¼æ¯å¦twitterçå ³æ³¨å表ï¼ç²ä¸å表çé½å¯ä»¥ç¨Redisçlistç»ææ¥å®ç°ï¼
å®ç°æ¹å¼ï¼Redis listçå®ç°ä¸ºä¸ä¸ªååé¾è¡¨ï¼å³å¯ä»¥æ¯æååæ¥æ¾åéåï¼æ´æ¹ä¾¿æä½ï¼ä¸è¿å¸¦æ¥äºé¨åé¢å¤çå åå¼éï¼Rediså é¨çå¾å¤å®ç°ï¼å æ¬åéç¼å²éåçä¹é½æ¯ç¨çè¿ä¸ªæ°æ®ç»æã
4ï¼Set
常ç¨å½ä»¤ï¼sadd/spop/smembers/sunionçï¼
åºç¨åºæ¯ï¼Redis set对å¤æä¾çåè½ä¸list类似æ¯ä¸ä¸ªå表çåè½ï¼ç¹æ®ä¹å¤å¨äºsetæ¯å¯ä»¥èªå¨æéçï¼å½ä½ éè¦åå¨ä¸ä¸ªå表æ°æ®ï¼åä¸å¸æåºç°éå¤æ°æ®æ¶ï¼setæ¯ä¸ä¸ªå¾å¥½çéæ©ï¼å¹¶ä¸setæä¾äºå¤ææ个æåæ¯å¦å¨ä¸ä¸ªsetéåå çéè¦æ¥å£ï¼è¿ä¸ªä¹æ¯listæä¸è½æä¾çï¼
å®ç°æ¹å¼ï¼set çå é¨å®ç°æ¯ä¸ä¸ª valueæ°¸è¿ä¸ºnullçHashMapï¼å®é å°±æ¯éè¿è®¡ç®hashçæ¹å¼æ¥å¿«éæéçï¼è¿ä¹æ¯setè½æä¾å¤æä¸ä¸ªæåæ¯å¦å¨éåå çåå ã
5ï¼Sorted Set
常ç¨å½ä»¤ï¼zadd/zrange/zrem/zcardçï¼
åºç¨åºæ¯ï¼Redis sorted setç使ç¨åºæ¯ä¸set类似ï¼åºå«æ¯setä¸æ¯èªå¨æåºçï¼èsorted setå¯ä»¥éè¿ç¨æ·é¢å¤æä¾ä¸ä¸ªä¼å 级(score)çåæ°æ¥ä¸ºæåæåºï¼å¹¶ä¸æ¯æå ¥æåºçï¼å³èªå¨æåºãå½ä½ éè¦ä¸ä¸ªæåºç并ä¸ä¸éå¤çéåå表ï¼é£ä¹å¯ä»¥éæ©sorted setæ°æ®ç»æï¼æ¯å¦twitter çpublic timelineå¯ä»¥ä»¥å表æ¶é´ä½ä¸ºscoreæ¥åå¨ï¼è¿æ ·è·åæ¶å°±æ¯èªå¨ææ¶é´æ好åºçã
å®ç°æ¹å¼ï¼Redis sorted setçå é¨ä½¿ç¨HashMapåè·³è·è¡¨(SkipList)æ¥ä¿è¯æ°æ®çåå¨åæåºï¼HashMapéæ¾çæ¯æåå°scoreçæ å°ï¼èè·³è·è¡¨éåæ¾çæ¯ææçæåï¼æåºä¾æ®æ¯HashMapéåçscore,使ç¨è·³è·è¡¨çç»æå¯ä»¥è·å¾æ¯è¾é«çæ¥æ¾æçï¼å¹¶ä¸å¨å®ç°ä¸æ¯è¾ç®åã
2ãå å管çæºå¶ä¸å
å¨Redisä¸ï¼å¹¶ä¸æ¯ææçæ°æ®é½ä¸ç´åå¨å¨å åä¸çãè¿æ¯åMemcachedç¸æ¯ä¸ä¸ªæ大çåºå«ãå½ç©çå åç¨å®æ¶ï¼Rediså¯ä»¥å°ä¸äºå¾ä¹ 没ç¨å°çvalue交æ¢å°ç£çãRedisåªä¼ç¼åææçkeyçä¿¡æ¯ï¼å¦æRedisåç°å åç使ç¨éè¶ è¿äºæä¸ä¸ªéå¼ï¼å°è§¦åswapçæä½ï¼Redisæ ¹æ®âswappability = age*log(size_in_memory)â计ç®åºåªäºkey对åºçvalueéè¦swapå°ç£çãç¶ååå°è¿äºkey对åºçvalueæä¹ åå°ç£çä¸ï¼åæ¶å¨å åä¸æ¸ é¤ãè¿ç§ç¹æ§ä½¿å¾Rediså¯ä»¥ä¿æè¶ è¿å ¶æºå¨æ¬èº«å å大å°çæ°æ®ãå½ç¶ï¼æºå¨æ¬èº«çå åå¿ é¡»è¦è½å¤ä¿æææçkeyï¼æ¯ç«è¿äºæ°æ®æ¯ä¸ä¼è¿è¡swapæä½çãåæ¶ç±äºRediså°å åä¸çæ°æ®swapå°ç£çä¸çæ¶åï¼æä¾æå¡ç主线ç¨åè¿è¡swapæä½çå线ç¨ä¼å ±äº«è¿é¨åå åï¼æ以å¦ææ´æ°éè¦swapçæ°æ®ï¼Rediså°é»å¡è¿ä¸ªæä½ï¼ç´å°å线ç¨å®æswapæä½åæå¯ä»¥è¿è¡ä¿®æ¹ãå½ä»Redisä¸è¯»åæ°æ®çæ¶åï¼å¦æ读åçkey对åºçvalueä¸å¨å åä¸ï¼é£ä¹Rediså°±éè¦ä»swapæ件ä¸å è½½ç¸åºæ°æ®ï¼ç¶ååè¿åç»è¯·æ±æ¹ã è¿éå°±åå¨ä¸ä¸ªI/O线ç¨æ± çé®é¢ãå¨é»è®¤çæ åµä¸ï¼Redisä¼åºç°é»å¡ï¼å³å®æææçswapæ件å è½½åæä¼ç¸åºãè¿ç§çç¥å¨å®¢æ·ç«¯çæ°éè¾å°ï¼è¿è¡æ¹éæä½çæ¶åæ¯è¾åéãä½æ¯å¦æå°Redisåºç¨å¨ä¸ä¸ªå¤§åçç½ç«åºç¨ç¨åºä¸ï¼è¿æ¾ç¶æ¯æ æ³æ»¡è¶³å¤§å¹¶åçæ åµçãæ以Redisè¿è¡æ们设置I/O线ç¨æ± ç大å°ï¼å¯¹éè¦ä»swapæ件ä¸å è½½ç¸åºæ°æ®ç读å请æ±è¿è¡å¹¶åæä½ï¼åå°é»å¡çæ¶é´ã
对äºåRedisåMemcachedè¿ç§åºäºå åçæ°æ®åºç³»ç»æ¥è¯´ï¼å å管ççæçé«ä½æ¯å½±åç³»ç»æ§è½çå ³é®å ç´ ãä¼ ç»Cè¯è¨ä¸çmalloc/freeå½æ°æ¯æ常ç¨çåé åéæ¾å åçæ¹æ³ï¼ä½æ¯è¿ç§æ¹æ³åå¨çå¾å¤§ç缺é·ï¼é¦å ï¼å¯¹äºå¼å人åæ¥è¯´ä¸å¹é çmallocåfree容æé æå åæ³é²ï¼å ¶æ¬¡é¢ç¹è°ç¨ä¼é æ大éå åç¢çæ æ³åæ¶éæ°å©ç¨ï¼éä½å åå©ç¨çï¼æåä½ä¸ºç³»ç»è°ç¨ï¼å ¶ç³»ç»å¼éè¿è¿å¤§äºä¸è¬å½æ°è°ç¨ãæ以ï¼ä¸ºäºæé«å åç管çæçï¼é«æçå å管çæ¹æ¡é½ä¸ä¼ç´æ¥ä½¿ç¨malloc/freeè°ç¨ãRedisåMemcachedå使ç¨äºèªèº«è®¾è®¡çå å管çæºå¶ï¼ä½æ¯å®ç°æ¹æ³åå¨å¾å¤§çå·®å¼ï¼ä¸é¢å°ä¼å¯¹ä¸¤è çå å管çæºå¶åå«è¿è¡ä»ç»ã
Memcachedé»è®¤ä½¿ç¨Slab Allocationæºå¶ç®¡çå åï¼å ¶ä¸»è¦ææ³æ¯æç §é¢å è§å®ç大å°ï¼å°åé çå ååå²æç¹å®é¿åº¦çå以åå¨ç¸åºé¿åº¦çkey-valueæ°æ®è®°å½ï¼ä»¥å®å ¨è§£å³å åç¢çé®é¢ãSlab Allocationæºå¶åªä¸ºåå¨å¤é¨æ°æ®è设计ï¼ä¹å°±æ¯è¯´ææçkey-valueæ°æ®é½åå¨å¨Slab Allocationç³»ç»éï¼èMemcachedçå ¶å®å å请æ±åéè¿æ®éçmalloc/freeæ¥ç³è¯·ï¼å 为è¿äºè¯·æ±çæ°éåé¢çå³å®äºå®ä»¬ä¸ä¼å¯¹æ´ä¸ªç³»ç»çæ§è½é æå½±åSlab Allocationçåçç¸å½ç®åã å¦å¾æ示ï¼å®é¦å ä»æä½ç³»ç»ç³è¯·ä¸å¤§åå åï¼å¹¶å°å ¶åå²æåç§å°ºå¯¸çåChunkï¼å¹¶æ尺寸ç¸åçååæç»Slab Classãå ¶ä¸ï¼Chunkå°±æ¯ç¨æ¥åå¨key-valueæ°æ®çæå°åä½ãæ¯ä¸ªSlab Classç大å°ï¼å¯ä»¥å¨Memcachedå¯å¨çæ¶åéè¿å¶å®Growth Factoræ¥æ§å¶ãåå®å¾ä¸Growth Factorçåå¼ä¸º1.ï¼å¦æ第ä¸ç»Chunkç大å°ä¸ºä¸ªåèï¼ç¬¬äºç»Chunkç大å°å°±ä¸ºä¸ªåèï¼ä¾æ¤ç±»æ¨ã
请ç¹å»è¾å ¥å¾çæè¿°
å½Memcachedæ¥æ¶å°å®¢æ·ç«¯åéè¿æ¥çæ°æ®æ¶é¦å ä¼æ ¹æ®æ¶å°æ°æ®ç大å°éæ©ä¸ä¸ªæåéçSlab Classï¼ç¶åéè¿æ¥è¯¢Memcachedä¿åçç该Slab Classå 空é²Chunkçå表就å¯ä»¥æ¾å°ä¸ä¸ªå¯ç¨äºåå¨æ°æ®çChunkãå½ä¸æ¡æ°æ®åºè¿ææè 丢å¼æ¶ï¼è¯¥è®°å½æå ç¨çChunkå°±å¯ä»¥åæ¶ï¼éæ°æ·»å å°ç©ºé²å表ä¸ãä»ä»¥ä¸è¿ç¨æ们å¯ä»¥çåºMemcachedçå å管çå¶æçé«ï¼èä¸ä¸ä¼é æå åç¢çï¼ä½æ¯å®æ大ç缺ç¹å°±æ¯ä¼å¯¼è´ç©ºé´æµªè´¹ãå 为æ¯ä¸ªChunké½åé äºç¹å®é¿åº¦çå å空é´ï¼æ以åé¿æ°æ®æ æ³å åå©ç¨è¿äºç©ºé´ãå¦å¾ æ示ï¼å°ä¸ªåèçæ°æ®ç¼åå°ä¸ªåèçChunkä¸ï¼å©ä½ç个åè就浪费æäºã
请ç¹å»è¾å ¥å¾çæè¿°
Redisçå å管ç主è¦éè¿æºç ä¸zmalloc.håzmalloc.c两个æ件æ¥å®ç°çãRedis为äºæ¹ä¾¿å åç管çï¼å¨åé ä¸åå åä¹åï¼ä¼å°è¿åå åç大å°åå ¥å ååç头é¨ãå¦å¾æ示ï¼real_ptræ¯redisè°ç¨mallocåè¿åçæéãrediså°å ååç大å°sizeåå ¥å¤´é¨ï¼sizeæå æ®çå å大å°æ¯å·²ç¥çï¼ä¸ºsize_tç±»åçé¿åº¦ï¼ç¶åè¿året_ptrãå½éè¦éæ¾å åçæ¶åï¼ret_ptrè¢«ä¼ ç»å å管çç¨åºãéè¿ret_ptrï¼ç¨åºå¯ä»¥å¾å®¹æçç®åºreal_ptrçå¼ï¼ç¶åå°real_pträ¼ ç»freeéæ¾å åã
请ç¹å»è¾å ¥å¾çæè¿°
Rediséè¿å®ä¹ä¸ä¸ªæ°ç»æ¥è®°å½ææçå ååé æ åµï¼è¿ä¸ªæ°ç»çé¿åº¦ä¸ºZMALLOC_MAX_ALLOC_STATãæ°ç»çæ¯ä¸ä¸ªå ç´ ä»£è¡¨å½åç¨åºæåé çå ååç个æ°ï¼ä¸å ååç大å°ä¸ºè¯¥å ç´ çä¸æ ãå¨æºç ä¸ï¼è¿ä¸ªæ°ç»ä¸ºzmalloc_allocationsãzmalloc_allocations[]代表已ç»åé çé¿åº¦ä¸ºbytesçå ååç个æ°ãzmalloc.cä¸æä¸ä¸ªéæåéused_memoryç¨æ¥è®°å½å½ååé çå åæ»å¤§å°ãæ以ï¼æ»çæ¥çï¼Rediséç¨çæ¯å è£ çmallc/freeï¼ç¸è¾äºMemcachedçå å管çæ¹æ³æ¥è¯´ï¼è¦ç®åå¾å¤ã
3ãæ°æ®æä¹ åæ¯æ
Redisè½ç¶æ¯åºäºå åçåå¨ç³»ç»ï¼ä½æ¯å®æ¬èº«æ¯æ¯æå åæ°æ®çæä¹ åçï¼èä¸æä¾ä¸¤ç§ä¸»è¦çæä¹ åçç¥ï¼RDBå¿«ç §åAOFæ¥å¿ãèmemcachedæ¯ä¸æ¯ææ°æ®æä¹ åæä½çã
1ï¼RDBå¿«ç §
Redisæ¯æå°å½åæ°æ®çå¿«ç §åæä¸ä¸ªæ°æ®æ件çæä¹ åæºå¶ï¼å³RDBå¿«ç §ãä½æ¯ä¸ä¸ªæç»åå ¥çæ°æ®åºå¦ä½çæå¿«ç §å¢ï¼Redisåå©äºforkå½ä»¤çcopy on writeæºå¶ãå¨çæå¿«ç §æ¶ï¼å°å½åè¿ç¨forkåºä¸ä¸ªåè¿ç¨ï¼ç¶åå¨åè¿ç¨ä¸å¾ªç¯ææçæ°æ®ï¼å°æ°æ®åæ为RDBæ件ãæ们å¯ä»¥éè¿Redisçsaveæ令æ¥é ç½®RDBå¿«ç §çæçæ¶æºï¼æ¯å¦é ç½®åéå°±çæå¿«ç §ï¼ä¹å¯ä»¥é ç½®æ次åå ¥å°±çæå¿«ç §ï¼ä¹å¯ä»¥å¤ä¸ªè§åä¸èµ·å®æ½ãè¿äºè§åçå®ä¹å°±å¨Redisçé ç½®æ件ä¸ï¼ä½ ä¹å¯ä»¥éè¿RedisçCONFIG SETå½ä»¤å¨Redisè¿è¡æ¶è®¾ç½®è§åï¼ä¸éè¦éå¯Redisã
RedisçRDBæ件ä¸ä¼åæï¼å ä¸ºå ¶åæä½æ¯å¨ä¸ä¸ªæ°è¿ç¨ä¸è¿è¡çï¼å½çæä¸ä¸ªæ°çRDBæ件æ¶ï¼Redisçæçåè¿ç¨ä¼å å°æ°æ®åå°ä¸ä¸ªä¸´æ¶æ件ä¸ï¼ç¶åéè¿ååæ§renameç³»ç»è°ç¨å°ä¸´æ¶æ件éå½å为RDBæ件ï¼è¿æ ·å¨ä»»ä½æ¶ååºç°æ éï¼RedisçRDBæ件é½æ»æ¯å¯ç¨çãåæ¶ï¼RedisçRDBæ件ä¹æ¯Redis主ä»åæ¥å é¨å®ç°ä¸çä¸ç¯ãRDBæä»çä¸è¶³ï¼å°±æ¯ä¸æ¦æ°æ®åºåºç°é®é¢ï¼é£ä¹æ们çRDBæ件ä¸ä¿åçæ°æ®å¹¶ä¸æ¯å ¨æ°çï¼ä»ä¸æ¬¡RDBæ件çæå°Redisåæºè¿æ®µæ¶é´çæ°æ®å ¨é¨ä¸¢æäºãå¨æäºä¸å¡ä¸ï¼è¿æ¯å¯ä»¥å¿åçã
2ï¼AOFæ¥å¿
AOFæ¥å¿çå ¨ç§°æ¯append only fileï¼å®æ¯ä¸ä¸ªè¿½å åå ¥çæ¥å¿æ件ãä¸ä¸è¬æ°æ®åºçbinlogä¸åçæ¯ï¼AOFæ件æ¯å¯è¯å«ç纯ææ¬ï¼å®çå 容就æ¯ä¸ä¸ªä¸ªçRedisæ åå½ä»¤ãåªæé£äºä¼å¯¼è´æ°æ®åçä¿®æ¹çå½ä»¤æä¼è¿½å å°AOFæ件ãæ¯ä¸æ¡ä¿®æ¹æ°æ®çå½ä»¤é½çæä¸æ¡æ¥å¿ï¼AOFæ件ä¼è¶æ¥è¶å¤§ï¼æ以Redisåæä¾äºä¸ä¸ªåè½ï¼å«åAOF rewriteãå ¶åè½å°±æ¯éæ°çæä¸ä»½AOFæ件ï¼æ°çAOFæ件ä¸ä¸æ¡è®°å½çæä½åªä¼æä¸æ¬¡ï¼èä¸åä¸ä»½èæ件é£æ ·ï¼å¯è½è®°å½äºå¯¹åä¸ä¸ªå¼çå¤æ¬¡æä½ãå ¶çæè¿ç¨åRDB类似ï¼ä¹æ¯forkä¸ä¸ªè¿ç¨ï¼ç´æ¥éåæ°æ®ï¼åå ¥æ°çAOF临æ¶æ件ãå¨åå ¥æ°æ件çè¿ç¨ä¸ï¼ææçåæä½æ¥å¿è¿æ¯ä¼åå°åæ¥èçAOFæ件ä¸ï¼åæ¶è¿ä¼è®°å½å¨å åç¼å²åºä¸ãå½éå®æä½å®æåï¼ä¼å°ææç¼å²åºä¸çæ¥å¿ä¸æ¬¡æ§åå ¥å°ä¸´æ¶æ件ä¸ãç¶åè°ç¨ååæ§çrenameå½ä»¤ç¨æ°çAOFæ件å代èçAOFæ件ã
AOFæ¯ä¸ä¸ªåæ件æä½ï¼å ¶ç®çæ¯å°æä½æ¥å¿åå°ç£çä¸ï¼æ以å®ä¹åæ ·ä¼éå°æ们ä¸é¢è¯´çåæä½çæµç¨ãå¨Redisä¸å¯¹AOFè°ç¨writeåå ¥åï¼éè¿appendfsyncé项æ¥æ§å¶è°ç¨fsyncå°å ¶åå°ç£çä¸çæ¶é´ï¼ä¸é¢appendfsyncçä¸ä¸ªè®¾ç½®é¡¹ï¼å®å ¨å¼ºåº¦éæ¸å强ã
appendfsync no å½è®¾ç½®appendfsync为noçæ¶åï¼Redisä¸ä¼ä¸»å¨è°ç¨fsyncå»å°AOFæ¥å¿å 容åæ¥å°ç£çï¼æ以è¿ä¸åå°±å®å ¨ä¾èµäºæä½ç³»ç»çè°è¯äºã对大å¤æ°Linuxæä½ç³»ç»ï¼æ¯æ¯ç§è¿è¡ä¸æ¬¡fsyncï¼å°ç¼å²åºä¸çæ°æ®åå°ç£çä¸ã
appendfsync everysec å½è®¾ç½®appendfsync为everysecçæ¶åï¼Redisä¼é»è®¤æ¯éä¸ç§è¿è¡ä¸æ¬¡fsyncè°ç¨ï¼å°ç¼å²åºä¸çæ°æ®åå°ç£çãä½æ¯å½è¿ä¸æ¬¡çfsyncè°ç¨æ¶é¿è¶ è¿1ç§æ¶ãRedisä¼éå延è¿fsyncççç¥ï¼åçä¸ç§éãä¹å°±æ¯å¨ä¸¤ç§ååè¿è¡fsyncï¼è¿ä¸æ¬¡çfsyncå°±ä¸ç®¡ä¼æ§è¡å¤é¿æ¶é´é½ä¼è¿è¡ãè¿æ¶åç±äºå¨fsyncæ¶æ件æ述符ä¼è¢«é»å¡ï¼æ以å½åçåæä½å°±ä¼é»å¡ãæ以ç»è®ºå°±æ¯ï¼å¨ç»å¤§å¤æ°æ åµä¸ï¼Redisä¼æ¯éä¸ç§è¿è¡ä¸æ¬¡fsyncãå¨æåçæ åµä¸ï¼ä¸¤ç§éä¼è¿è¡ä¸æ¬¡fsyncæä½ãè¿ä¸æä½å¨å¤§å¤æ°æ°æ®åºç³»ç»ä¸è¢«ç§°ä¸ºgroup commitï¼å°±æ¯ç»åå¤æ¬¡åæä½çæ°æ®ï¼ä¸æ¬¡æ§å°æ¥å¿åå°ç£çã
appednfsync always å½è®¾ç½®appendfsync为alwaysæ¶ï¼æ¯ä¸æ¬¡åæä½é½ä¼è°ç¨ä¸æ¬¡fsyncï¼è¿æ¶æ°æ®æ¯æå®å ¨çï¼å½ç¶ï¼ç±äºæ¯æ¬¡é½ä¼æ§è¡fsyncï¼æä»¥å ¶æ§è½ä¹ä¼åå°å½±åã
对äºä¸è¬æ§çä¸å¡éæ±ï¼å»ºè®®ä½¿ç¨RDBçæ¹å¼è¿è¡æä¹ åï¼åå æ¯RDBçå¼é并ç¸æ¯AOFæ¥å¿è¦ä½å¾å¤ï¼å¯¹äºé£äºæ æ³å¿æ°æ®ä¸¢å¤±çåºç¨ï¼å»ºè®®ä½¿ç¨AOFæ¥å¿ã
4ãé群管ççä¸å
Memcachedæ¯å ¨å åçæ°æ®ç¼å²ç³»ç»ï¼Redisè½ç¶æ¯ææ°æ®çæä¹ åï¼ä½æ¯å ¨å åæ¯ç«ææ¯å ¶é«æ§è½çæ¬è´¨ãä½ä¸ºåºäºå åçåå¨ç³»ç»æ¥è¯´ï¼æºå¨ç©çå åç大å°å°±æ¯ç³»ç»è½å¤å®¹çº³çæ大æ°æ®éãå¦æéè¦å¤ççæ°æ®éè¶ è¿äºåå°æºå¨çç©çå å大å°ï¼å°±éè¦æ建åå¸å¼é群æ¥æ©å±åå¨è½åã
Memcachedæ¬èº«å¹¶ä¸æ¯æåå¸å¼ï¼å æ¤åªè½å¨å®¢æ·ç«¯éè¿åä¸è´æ§åå¸è¿æ ·çåå¸å¼ç®æ³æ¥å®ç°Memcachedçåå¸å¼åå¨ãä¸å¾ç»åºäºMemcachedçåå¸å¼åå¨å®ç°æ¶æãå½å®¢æ·ç«¯åMemcachedé群åéæ°æ®ä¹åï¼é¦å ä¼éè¿å ç½®çåå¸å¼ç®æ³è®¡ç®åºè¯¥æ¡æ°æ®çç®æ èç¹ï¼ç¶åæ°æ®ä¼ç´æ¥åéå°è¯¥èç¹ä¸åå¨ãä½å®¢æ·ç«¯æ¥è¯¢æ°æ®æ¶ï¼åæ ·è¦è®¡ç®åºæ¥è¯¢æ°æ®æå¨çèç¹ï¼ç¶åç´æ¥å该èç¹åéæ¥è¯¢è¯·æ±ä»¥è·åæ°æ®ã
请ç¹å»è¾å ¥å¾çæè¿°
ç¸è¾äºMemcachedåªè½éç¨å®¢æ·ç«¯å®ç°åå¸å¼åå¨ï¼Redisæ´ååäºå¨æå¡å¨ç«¯æ建åå¸å¼åå¨ãææ°çæ¬çRediså·²ç»æ¯æäºåå¸å¼åå¨åè½ãRedis Clusteræ¯ä¸ä¸ªå®ç°äºåå¸å¼ä¸å 许åç¹æ éçRedisé«çº§çæ¬ï¼å®æ²¡æä¸å¿èç¹ï¼å ·æ线æ§å¯ä¼¸ç¼©çåè½ãä¸å¾ç»åºRedis Clusterçåå¸å¼åå¨æ¶æï¼å ¶ä¸èç¹ä¸èç¹ä¹é´éè¿äºè¿å¶åè®®è¿è¡éä¿¡ï¼èç¹ä¸å®¢æ·ç«¯ä¹é´éè¿asciiåè®®è¿è¡éä¿¡ãå¨æ°æ®çæ¾ç½®çç¥ä¸ï¼Redis Clusterå°æ´ä¸ªkeyçæ°å¼ååæ个åå¸æ§½ï¼æ¯ä¸ªèç¹ä¸å¯ä»¥åå¨ä¸ä¸ªæå¤ä¸ªåå¸æ§½ï¼ä¹å°±æ¯è¯´å½åRedis Clusteræ¯æçæ大èç¹æ°å°±æ¯ãRedis Cluster使ç¨çåå¸å¼ç®æ³ä¹å¾ç®åï¼crc( key ) % HASH_SLOTS_NUMBERã
请ç¹å»è¾å ¥å¾çæè¿°
为äºä¿è¯åç¹æ éä¸çæ°æ®å¯ç¨æ§ï¼Redis Clusterå¼å ¥äºMasterèç¹åSlaveèç¹ãå¨Redis Clusterä¸ï¼æ¯ä¸ªMasterèç¹é½ä¼æ对åºç两个ç¨äºåä½çSlaveèç¹ãè¿æ ·å¨æ´ä¸ªé群ä¸ï¼ä»»æ两个èç¹çå®æºé½ä¸ä¼å¯¼è´æ°æ®çä¸å¯ç¨ãå½Masterèç¹éåºåï¼é群ä¼èªå¨éæ©ä¸ä¸ªSlaveèç¹æ为æ°çMasterèç¹ã
请ç¹å»è¾å ¥å¾çæè¿°
å¦ä½å¦å¥½C++å¢?
æè¿éæäºç»éªï¼å¸æå¯¹ä½ æ帮å©ï¼
1.æC++å½æä¸é¨æ°çè¯è¨å¦ä¹ ï¼åC没å¥å ³ç³»ï¼ççãï¼ï¼
2.çãThinking In C++ãï¼ä¸è¦çãC++åææ»ç¸ãï¼
3.çãThe C++ Programming LanguageãåãInside The C++ Object Modelã,ä¸è¦å 为ä»ä»¬å¾é¾èæ们èªå·±æ¯åå¦è æ以就ä¸çï¼
4.ä¸è¦è¢«VCãBCBãBCãMCãTCçè¯æ±æè¿·æââä»ä»¬é½æ¯éæå¼åç¯å¢ï¼èæ们è¦å¦çæ¯ä¸é¨è¯è¨ï¼
5.ä¸è¦æ¾è¿ä»»ä½ä¸ä¸ªçä¸å»å¾ç®åçå°ç¼ç¨é®é¢ââä»ä»¬å¾å¾å¹¶ä¸é£ä¹ç®åï¼æè å¯ä»¥å¼ä¼¸åºå¾å¤ç¥è¯ç¹ï¼
6.ä¼ç¨Visual C++ï¼å¹¶ä¸è¯´æä½ ä¼C++ï¼
7.å¦class并ä¸é¾ï¼templateãSTLãgeneric programmingä¹ä¸è¿å¦æ¤ââé¾çæ¯é¿æåæå®è·µåä¸éä½åçåè§ç¾¤ä¹¦ï¼
8.å¦æä¸æ¯å¤©æçè¯ï¼æ³å¦ç¼ç¨å°±ä¸è¦æ³ç©æ¸¸æââä½ ä»¥ä¸ºä½ åå°äºï¼å ¶å®ä½ çC++水平并没æåä½ éå ³çè½åä¸èµ·åé«ââå ¶å®å¯ä»¥æ¶å»è®°ä½ï¼å¦C++æ¯ä¸ºäºç¼æ¸¸æçï¼
9.çVisual C++ç书ï¼æ¯å¦ä¸äºC++è¯è¨çï¼
.æµ®èºç人容æ说ï¼XXè¯è¨ä¸è¡äºï¼åºè¯¥å¦YYï¼ââæ¯ä½ èªå·±ä¸è¡äºå§ï¼ï¼
.æµ®èºç人容æé®ï¼æå°åºè¯¥å¦ä»ä¹ï¼ââå«é®ï¼å¦å°±å¯¹äºï¼
.æµ®èºç人容æé®ï¼XXæé±éåï¼ââå»ºè®®ä½ å»æ¢é¶è¡ï¼
.æµ®èºç人容æ说ï¼æè¦ä¸æçï¼æè±æä¸è¡ï¼ââä¸è¡ï¼å¦åï¼
.æµ®èºç人容æé®ï¼XXåYYåªä¸ªå¥½ï¼ââåè¯ä½ å§ï¼é½å¥½ââåªè¦ä½ å¦å°±è¡ï¼
.æµ®èºç人å两ç§ï¼a)åªè§æèä¸å¦ç人ï¼b)åªå¦èä¸åæç人ï¼
.ææ¶é«¦çææ¯æå¨å´è¾¹ï¼è¿ä¸å¦æè¿æ¶çææ¯è®°å¨å¿éï¼
.C++ä¸ä» ä» æ¯æ¯æé¢å对象çç¨åºè®¾è®¡è¯è¨ï¼
.å¦ä¹ ç¼ç¨æ好çæ¹æ³ä¹ä¸å°±æ¯é 读æºä»£ç ï¼
.å¨ä»»ä½æ¶å»é½ä¸è¦è®¤ä¸ºèªå·±æä¸ç书已ç»è¶³å¤äºï¼
.请é 读ãThe Standard C++ Bibleã(ä¸æçï¼æ åC++å®å ¸)ï¼ææ¡C++æ åï¼
.çå¾æç书ï¼è¯·ä»ç»çï¼çä¸æç书ï¼è¯·ç¡¬ç头ç®çï¼
.å«ææç第ä¸é书就è½è®°ä½åææ¡ä»ä¹ââ请ç第äºéã第ä¸éï¼
.请çãEffective C++ãåãMore Effective C++ã以åãExceptional C++ãï¼
.ä¸è¦åçå¨éæå¼åç¯å¢çæ篮ä¸ï¼è¦å¦ä¼æ§å¶éæå¼åç¯å¢ï¼è¿è¦å¦ä¼ç¨å½ä»¤è¡æ¹å¼å¤çç¨åºï¼
.åå«äººä¸èµ·è®¨è®ºææä¹çC++ç¥è¯ç¹ï¼èä¸æ¯äºåµXXè¡ä¸è¡æè YYä¸ZZåªä¸ªå¥½ï¼
.请çãç¨åºè®¾è®¡å®è·µãï¼å¹¶ä¸¥æ ¼çæç §å ¶è¦æ±å»åï¼
.ä¸è¦å 为CåC++ä¸æä¸äºè¯æ³åå ³é®åçä¸å»ç¸åï¼å°±è®¤ä¸ºå®ä»¬çæä¹åä½ç¨å®å ¨ä¸æ ·ï¼
.C++ç»ä¸æ¯æè°çCçâæ©å âââå¦æC++ä¸å¼å§å°±èµ·åå«Zè¯è¨ï¼ä½ ä¸å®ä¸ä¼æCåZè¯è¨èç³»å¾é£ä¹ç´§å¯ï¼
.请ä¸è¦è®¤ä¸ºå¦è¿XXè¯è¨åæ¹å¦C++ä¼æä»ä¹é®é¢ââä½ åªä¸è¿åå¨å¦ä¸é¨å ¨æ°çè¯è¨èå·²ï¼
.读å®äºãInside The C++ Object Modelã以ååæ¥è®¤å®èªå·±æ¯ä¸æ¯å·²ç»å¦ä¼äºC++ï¼
.å¦ä¹ ç¼ç¨çç§è¯æ¯ï¼ç¼ç¨ï¼ç¼ç¨ï¼åç¼ç¨ï¼
.请çæä¸å书ç±ï¼ãC++é¢å对象é«æç¼ç¨ï¼C++ Effective Object-Oriented Software Constructionï¼ããé¢å对象软件æé (Object-Oriented Software Construction)ãã设计模å¼ï¼Design Patternsï¼ããThe Art of Computer Programmingãï¼
.è®°ä½ï¼é¢å对象ææ¯ä¸åªæ¯C++ä¸æçï¼
.请æ书ä¸çç¨åºä¾å亲æè¾å ¥å°çµèä¸å®è·µï¼å³ä½¿é å¥å çä¸ææºä»£ç ï¼
.æå¨ä¹¦ä¸çå°çææä¹çä¾åæ©å ï¼
.请éè§C++ä¸çå¼å¸¸å¤çææ¯ï¼å¹¶å°å ¶åå®çè¿ç¨å°èªå·±çç¨åºä¸ï¼
.ç»å¸¸å顾èªå·±ä»¥ååè¿çç¨åºï¼å¹¶å°è¯éåï¼æèªå·±å¦å°çæ°ç¥è¯è¿ç¨è¿å»ï¼
.ä¸è¦æ¼æ书ä¸ä»»ä½ä¸ä¸ªç»ä¹ é¢ââè¯·å ¨é¨åå®å¹¶è®°å½ä¸è§£é¢æè·¯ï¼
.C++è¯è¨åC++çéæå¼åç¯å¢è¦åæ¶å¦ä¹ åææ¡ï¼
.æ¢ç¶å³å®äºå¦C++,就请åæå¦ä¸å»ï¼å 为å¦ä¹ ç¨åºè®¾è®¡è¯è¨çç®çæ¯ææ¡ç¨åºè®¾è®¡ææ¯ï¼èç¨åºè®¾è®¡ææ¯æ¯è·¨è¯è¨çï¼
.就让C++è¯è¨çåç§å¹³å°åå¼åç¯å¢å»æ¿ççç«äºå§ï¼æ们è¦ä»¥å¦ä¹ C++è¯è¨æ¬èº«ä¸ºä¸»ï¼
.å½ä½ åC++ç¨åºåå°ä¸åå´åç°èªå·±ç¨çæ¹æ³å¾æå£æ¶ï¼è¯·ä¸è¦é©¬ä¸åæï¼è¯·å°½å¿«å°ä½ä¸çé¨åç²ç¥çå®æ以ä¿è¯è¿ä¸ªè®¾è®¡çå®æ´æ§ï¼ç¶ååæèªå·±çé误并éæ°è®¾è®¡åç¼åï¼åè§ï¼ï¼
.å«å¿æ¥ï¼è®¾è®¡C++çclassç¡®å®ä¸å®¹æï¼èªå·±ç¨åºä¸çclassåèªå·±çclass设计水平æ¯å¨ä¸æçç¼ç¨å®è·µä¸å®åååå±çï¼
.å³ä¸è¦å 为ç¨åºâå¾å°âå°±ä¸éµå¾ªæäºä½ ä¸çç»çè§åââå¥½ä¹ æ¯æ¯å¹å »åºæ¥çï¼èä¸æ¯ä¸æ¬¡è®°ä½çï¼
.æ¯å¦å°ä¸ä¸ªC++é¾ç¹çæ¶åï¼å°è¯ç对å«äººè®²è§£è¿ä¸ªç¥è¯ç¹å¹¶è®©ä»ç解ââä½ è½è®²æ¸ æ¥æ说æä½ ççç解äºï¼
.è®°å½ä¸å¨åå«äººäº¤æµæ¶åç°çèªå·±å¿½è§æä¸ç解çç¥è¯ç¹ï¼
.请ä¸æç对èªå·±åçç¨åºæåºæ´é«çè¦æ±,åªæä½ çç¨åºçæ¬å·ä¼åæVersion .XXï¼
.ä¿åå¥½ä½ åè¿çææçç¨åºââé£æ¯ä½ æ好ç积累ä¹ä¸ï¼
.请ä¸è¦åæµ®èºç人ï¼
.请çç±C++!