è°è°ä¸è´æ§
ä¸è´æ§å°±æ¯æ°æ®ä¿æä¸è´ï¼å¨åå¸å¼ç³»ç»ä¸ï¼å¯ä»¥ç解为å¤ä¸ªèç¹ä¸æ°æ®çå¼æ¯ä¸è´çã
强ä¸è´æ§ï¼è¿ç§ä¸è´æ§çº§å«æ¯æ符åç¨æ·ç´è§çï¼å®è¦æ±ç³»ç»åå ¥ä»ä¹ï¼è¯»åºæ¥çä¹ä¼æ¯ä»ä¹ï¼ç¨æ·ä½éªå¥½ï¼ä½å®ç°èµ·æ¥å¾å¾å¯¹ç³»ç»çæ§è½å½±å大
å¼±ä¸è´æ§ï¼è¿ç§ä¸è´æ§çº§å«çº¦æäºç³»ç»å¨åå ¥æååï¼ä¸æ¿è¯ºç«å³å¯ä»¥è¯»å°åå ¥çå¼ï¼ä¹ä¸æ¿è¯ºå¤ä¹ ä¹åæ°æ®è½å¤è¾¾å°ä¸è´ï¼ä½ä¼å°½å¯è½å°ä¿è¯å°æ个æ¶é´çº§å«ï¼æ¯å¦ç§çº§å«ï¼åï¼æ°æ®è½å¤è¾¾å°ä¸è´ç¶æ
æç»ä¸è´æ§ï¼æç»ä¸è´æ§æ¯å¼±ä¸è´æ§çä¸ä¸ªç¹ä¾ï¼ç³»ç»ä¼ä¿è¯å¨ä¸å®æ¶é´å ï¼è½å¤è¾¾å°ä¸ä¸ªæ°æ®ä¸è´çç¶æãè¿éä¹æ以å°æç»ä¸è´æ§åç¬æåºæ¥ï¼æ¯å 为å®æ¯å¼±ä¸è´æ§ä¸é常æ¨å´çä¸ç§ä¸è´æ§æ¨¡åï¼ä¹æ¯ä¸çå¨å¤§ååå¸å¼ç³»ç»çæ°æ®ä¸è´æ§ä¸æ¯è¾æ¨å´ç模å
ä¸ä¸ªç»å ¸çç¼å模å¼
ç¼åå¯ä»¥æåæ§è½ãç¼è§£æ°æ®åºååï¼ä½æ¯ä½¿ç¨ç¼åä¹ä¼å¯¼è´æ°æ®ä¸ä¸è´æ§çé®é¢ãä¸è¬æ们æ¯å¦ä½ä½¿ç¨ç¼åå¢ï¼æä¸ç§ç»å ¸çç¼å模å¼ï¼
Cache-Aside Pattern
Read-Through/Write through
Write behind
Cache-Aside Pattern
Cache-Aside Patternï¼å³æè·¯ç¼å模å¼ï¼å®çæåºæ¯ä¸ºäºå°½å¯è½å°è§£å³ç¼åä¸æ°æ®åºçæ°æ®ä¸ä¸è´é®é¢ã
Cache-Aside读æµç¨
Cache-Aside Patternç读请æ±æµç¨å¦ä¸ï¼
读çæ¶åï¼å 读ç¼åï¼ç¼åå½ä¸çè¯ï¼ç´æ¥è¿åæ°æ®
ç¼å没æå½ä¸çè¯ï¼å°±å»è¯»æ°æ®åºï¼ä»æ°æ®åºååºæ°æ®ï¼æ¾å ¥ç¼ååï¼åæ¶è¿åååºã
Cache-Aside åæµç¨
Cache-Aside Patternçå请æ±æµç¨å¦ä¸ï¼
æ´æ°çæ¶åï¼å æ´æ°æ°æ®åºï¼ç¶ååå é¤ç¼åã
Read-Through/Write-Throughï¼è¯»åç©¿éï¼
Read/Write Through模å¼ä¸ï¼æå¡ç«¯æç¼åä½ä¸ºä¸»è¦æ°æ®åå¨ãåºç¨ç¨åºè·æ°æ®åºç¼å交äºï¼é½æ¯éè¿æ½è±¡ç¼åå±å®æçã
Read-Through
Read-Throughçç®è¦æµç¨å¦ä¸
ä»ç¼å读åæ°æ®ï¼è¯»å°ç´æ¥è¿å
å¦æ读åä¸å°çè¯ï¼ä»æ°æ®åºå è½½ï¼åå ¥ç¼ååï¼åè¿åååºã
è¿ä¸ªç®è¦æµç¨æ¯ä¸æ¯è·Cache-Asideå¾åå¢ï¼å ¶å®Read-Throughå°±æ¯å¤äºä¸å±Cache-Providerï¼æµç¨å¦ä¸ï¼
Read-Throughå®é åªæ¯å¨Cache-Asideä¹ä¸è¿è¡äºä¸å±å°è£ ï¼å®ä¼è®©ç¨åºä»£ç åå¾æ´ç®æ´ï¼åæ¶ä¹åå°æ°æ®æºä¸çè´è½½ã
Write-Through
Write-Through模å¼ä¸ï¼å½åçå请æ±æ¶ï¼ä¹æ¯ç±ç¼åæ½è±¡å±å®ææ°æ®æºåç¼åæ°æ®çæ´æ°,æµç¨å¦ä¸ï¼
Write behind ï¼å¼æ¥ç¼ååå ¥ï¼
Write behindè·Read-Through/Write-Throughæç¸ä¼¼çå°æ¹ï¼é½æ¯ç±Cache Provider
æ¥è´è´£ç¼ååæ°æ®åºç读åãå®ä¸¤åæ个å¾å¤§çä¸åï¼Read/Write Throughæ¯åæ¥æ´æ°ç¼ååæ°æ®çï¼Write Behindåæ¯åªæ´æ°ç¼åï¼ä¸ç´æ¥æ´æ°æ°æ®åºï¼éè¿æ¹éå¼æ¥çæ¹å¼æ¥æ´æ°æ°æ®åºã
è¿ç§æ¹å¼ä¸ï¼ç¼ååæ°æ®åºçä¸è´æ§ä¸å¼ºï¼å¯¹ä¸è´æ§è¦æ±é«çç³»ç»è¦è°¨æ 使ç¨ãä½æ¯å®éåé¢ç¹åçåºæ¯ï¼MySQLçInnoDB Buffer Poolæºå¶å°±ä½¿ç¨å°è¿ç§æ¨¡å¼ã
æä½ç¼åçæ¶åï¼å é¤ç¼åå¢ï¼è¿æ¯æ´æ°ç¼åï¼
ä¸è¬ä¸å¡åºæ¯ï¼æ们使ç¨çå°±æ¯Cache-Aside模å¼ã æäºå°ä¼ä¼´å¯è½ä¼é®ï¼ Cache-Asideå¨åå ¥è¯·æ±çæ¶åï¼ä¸ºä»ä¹æ¯å é¤ç¼åèä¸æ¯æ´æ°ç¼åå¢ï¼
æ们å¨æä½ç¼åçæ¶åï¼å°åºåºè¯¥å é¤ç¼åè¿æ¯æ´æ°ç¼åå¢ï¼æ们å æ¥ç个ä¾åï¼
线ç¨Aå åèµ·ä¸ä¸ªåæä½ï¼ç¬¬ä¸æ¥å æ´æ°æ°æ®åº
线ç¨Bååèµ·ä¸ä¸ªåæä½ï¼ç¬¬äºæ¥æ´æ°äºæ°æ®åº
ç±äºç½ç»çåå ï¼çº¿ç¨Bå æ´æ°äºç¼å
线ç¨Aæ´æ°ç¼åã
è¿æ¶åï¼ç¼åä¿åçæ¯Açæ°æ®ï¼èæ°æ®ï¼ï¼æ°æ®åºä¿åçæ¯Bçæ°æ®ï¼æ°æ°æ®ï¼ï¼æ°æ®ä¸ä¸è´äºï¼èæ°æ®åºç°å¦ãå¦ææ¯å é¤ç¼åå代æ´æ°ç¼ååä¸ä¼åºç°è¿ä¸ªèæ°æ®é®é¢ã
æ´æ°ç¼åç¸å¯¹äºå é¤ç¼åï¼è¿æ两ç¹å£å¿ï¼
å¦æä½ åå ¥çç¼åå¼ï¼æ¯ç»è¿å¤æ计ç®æå¾å°çè¯ãæ´æ°ç¼åé¢çé«çè¯ï¼å°±æµªè´¹æ§è½å¦ã
å¨åæ°æ®åºåºæ¯å¤ï¼è¯»æ°æ®åºæ¯å°çæ åµä¸ï¼æ°æ®å¾å¤æ¶åè¿æ²¡è¢«è¯»åå°ï¼å被æ´æ°äºï¼è¿ä¹æµªè´¹äºæ§è½å¢(å®é ä¸ï¼åå¤çåºæ¯ï¼ç¨ç¼åä¹ä¸æ¯å¾åç®äº)
ååçæ åµä¸ï¼å æä½æ°æ®åºè¿æ¯å æä½ç¼åï¼
Cache-Aside
ç¼å模å¼ä¸ï¼æäºå°ä¼ä¼´è¿æ¯æçé®ï¼å¨åå
¥è¯·æ±çæ¶åï¼ä¸ºä»ä¹æ¯å
æä½æ°æ®åºå¢ï¼ä¸ºä»ä¹ä¸å
æä½ç¼åå¢ï¼
å设æAãB两个请æ±ï¼è¯·æ±Aåæ´æ°æä½ï¼è¯·æ±Båæ¥è¯¢è¯»åæä½ã
线ç¨Aåèµ·ä¸ä¸ªåæä½ï¼ç¬¬ä¸æ¥del cache
æ¤æ¶çº¿ç¨Båèµ·ä¸ä¸ªè¯»æä½ï¼cache miss
线ç¨B继ç»è¯»DBï¼è¯»åºæ¥ä¸ä¸ªèæ°æ®
ç¶å线ç¨Bæèæ°æ®è®¾ç½®å ¥cache
线ç¨Aåå ¥DBææ°çæ°æ®
é
±ç´«å°±æé®é¢å¦ï¼ç¼ååæ°æ®åºçæ°æ®ä¸ä¸è´äºãç¼åä¿åçæ¯èæ°æ®ï¼æ°æ®åºä¿åçæ¯æ°æ°æ®ãå æ¤ï¼Cache-Aside
ç¼å模å¼ï¼éæ©äºå
æä½æ°æ®åºèä¸æ¯å
æä½ç¼åã
ç¼å延æ¶åå
æäºå°ä¼ä¼´å¯è½ä¼è¯´ï¼ä¸ä¸å®è¦å æä½æ°æ®åºåï¼éç¨ç¼å延æ¶åå çç¥å°±å¥½å¦ï¼ä»ä¹æ¯å»¶æ¶åå å¢ï¼
å å é¤ç¼å
åæ´æ°æ°æ®åº
ä¼ç ä¸ä¼ï¼æ¯å¦1ç§ï¼ï¼å次å é¤ç¼åã
è¿ä¸ªä¼ç ä¸ä¼ï¼ä¸è¬å¤ä¹ å¢ï¼é½æ¯1ç§ï¼
è¿ä¸ªä¼ç æ¶é´ = 读ä¸å¡é»è¾æ°æ®çèæ¶ + å ç¾æ¯«ç§ã 为äºç¡®ä¿è¯»è¯·æ±ç»æï¼å请æ±å¯ä»¥å é¤è¯»è¯·æ±å¯è½å¸¦æ¥çç¼åèæ°æ®ã
å é¤ç¼åéè¯æºå¶
ä¸ç®¡æ¯å»¶æ¶åå è¿æ¯Cache-Asideçå æä½æ°æ®åºåå é¤ç¼åï¼å¦æ第äºæ¥çå é¤ç¼å失败å¢ï¼å é¤å¤±è´¥ä¼å¯¼è´èæ°æ®å¦~
å é¤å¤±è´¥å°±å¤å é¤å 次å,ä¿è¯å é¤ç¼åæåå~ æ以å¯ä»¥å¼å ¥å é¤ç¼åéè¯æºå¶
å请æ±æ´æ°æ°æ®åº
ç¼åå 为æäºåå ï¼å é¤å¤±è´¥
æå é¤å¤±è´¥çkeyæ¾å°æ¶æ¯éå
æ¶è´¹æ¶æ¯éåçæ¶æ¯ï¼è·åè¦å é¤çkey
éè¯å é¤ç¼åæä½
读åbiglogå¼æ¥å é¤ç¼å
éè¯å é¤ç¼åæºå¶è¿å¯ä»¥ï¼å°±æ¯ä¼é æ好å¤ä¸å¡ä»£ç å ¥ä¾µãå ¶å®ï¼è¿å¯ä»¥éè¿æ°æ®åºçbinlogæ¥å¼æ¥æ·æ±°keyã
以mysqlä¸ºä¾ å¯ä»¥ä½¿ç¨é¿éçcanalå°binlogæ¥å¿ééåéå°MQéåéé¢ï¼ç¶åéè¿ACKæºå¶ç¡®è®¤å¤çè¿æ¡æ´æ°æ¶æ¯ï¼å é¤ç¼åï¼ä¿è¯æ°æ®ç¼åä¸è´æ§ã