# 第äºç« ï¼å¤å¶ ![](img/ch5.png) > ä¸å¯è½åºéçä¸è¥¿æ¯ï¼âä¸å¯è½âåºéçä¸è¥¿ææ¾èçç¹ç¹å°±æ¯ï¼ä¸æ¦ççåºéï¼é常就彻åºç©å®äºã > > ââ éæ ¼ææ¯ã»äºå½æ¯ï¼1992ï¼ ------ [TOC] å¤å¶æå³çå¨éè¿ç½ç»è¿æ¥çå¤å°æºå¨ä¸ä¿çç¸åæ°æ®çå¯æ¬ãæ£å¦å¨ [第äºé¨å](part-ii.md) çä»ç»ä¸æ讨论çé£æ ·ï¼æ们å¸æè½å¤å¶æ°æ®ï¼å¯è½åºäºåç§åæ ·çåå ï¼ * 使å¾æ°æ®ä¸ç¨æ·å¨å°çä¸æ¥è¿ï¼ä»èåå°å»¶è¿ï¼ * å³ä½¿ç³»ç»çä¸é¨ååºç°æ éï¼ç³»ç»ä¹è½ç»§ç»å·¥ä½ï¼ä»èæé«å¯ç¨æ§ï¼ * 伸缩å¯ä»¥æ¥å读请æ±çæºå¨æ°éï¼ä»èæé«è¯»åååéï¼ æ¬ç« å°åè®¾ä½ çæ°æ®éé常å°ï¼æ¯å°æºå¨é½å¯ä»¥ä¿åæ´ä¸ªæ°æ®éçå¯æ¬ãå¨ [第å ç« ](ch6.md) ä¸å°æ¾å®½è¿ä¸ªå设ï¼è®¨è®ºå¯¹å个æºå¨æ¥è¯´å¤ªå¤§çæ°æ®éçåå²ï¼åçï¼ãå¨åé¢çç« èä¸ï¼æ们å°è®¨è®ºå¤å¶æ°æ®ç³»ç»ä¸å¯è½åççåç§æ éï¼ä»¥åå¦ä½å¤çè¿äºæ éã å¦æå¤å¶ä¸çæ°æ®ä¸ä¼éæ¶é´èæ¹åï¼é£å¤å¶å°±å¾ç®åï¼å°æ°æ®å¤å¶å°æ¯ä¸ªèç¹ä¸æ¬¡å°±ä¸äºå¤§åãå¤å¶çå°é¾ä¹å¤å¨äºå¤çå¤å¶æ°æ®ç **åæ´ï¼changeï¼**ï¼è¿å°±æ¯æ¬ç« æè¦è®²çãæ们å°è®¨è®ºä¸ç§æµè¡çåæ´å¤å¶ç®æ³ï¼**åé¢å¯¼è ï¼single leaderï¼å主ï¼**ï¼**å¤é¢å¯¼è ï¼multi leaderï¼å¤ä¸»ï¼** å **æ é¢å¯¼è ï¼leaderlessï¼æ 主ï¼**ãå ä¹ææåå¸å¼æ°æ®åºé½ä½¿ç¨è¿ä¸ç§æ¹æ³ä¹ä¸ã å¨å¤å¶æ¶éè¦è¿è¡è®¸å¤æè¡¡ï¼ä¾å¦ï¼ä½¿ç¨åæ¥å¤å¶è¿æ¯å¼æ¥å¤å¶ï¼å¦ä½å¤ç失败çå¯æ¬ï¼è¿äºé常æ¯æ°æ®åºä¸çé ç½®é项ï¼ç»èå æ°æ®åºèå¼ï¼ä½åçå¨è®¸å¤ä¸åçå®ç°ä¸é½ç±»ä¼¼ãæ¬ç« ä¼è®¨è®ºè¿äºå³ççåæã æ°æ®åºçå¤å¶ç®å¾ä¸æ¯èç常è°äº ââ70 年代ç 究å¾åºçåºæ¬ååè³ä»æ²¡æ太大ååã1ãï¼å 为ç½ç»çåºæ¬çº¦æä»ä¿æä¸åãç¶èå¨ç 究ä¹å¤ï¼è®¸å¤å¼å人åä»ç¶å设ä¸ä¸ªæ°æ®åºåªæä¸ä¸ªèç¹ãåå¸å¼æ°æ®åºå为主æµåªæ¯æè¿åççäºã许å¤ç¨åºåé½æ¯è¿ä¸é¢åçæ°æï¼å æ¤å¯¹äºè¯¸å¦ **æç»ä¸è´æ§ï¼eventual consistencyï¼** çé®é¢åå¨è®¸å¤è¯¯è§£ãå¨ â[å¤å¶å»¶è¿é®é¢](#å¤å¶å»¶è¿é®é¢)â ä¸èï¼æ们å°æ´å 精确å°äºè§£æç»ä¸è´æ§ï¼å¹¶è®¨è®ºè¯¸å¦ **读己ä¹åï¼read-your-writesï¼** å **åè°è¯»ï¼monotonic readï¼** çå 容ã ## é¢å¯¼è ä¸è¿½éè åå¨äºæ°æ®åºæ·è´çæ¯ä¸ªèç¹è¢«ç§°ä¸º **å¯æ¬ï¼replicaï¼** ãå½åå¨å¤ä¸ªå¯æ¬æ¶ï¼ä¼ä¸å¯é¿å çåºç°ä¸ä¸ªé®é¢ï¼å¦ä½ç¡®ä¿æææ°æ®é½è½å¨äºææçå¯æ¬ä¸ï¼ æ¯ä¸æ¬¡åæ°æ®åºçåå ¥æä½é½éè¦ä¼ æå°ææå¯æ¬ä¸ï¼å¦åå¯æ¬å°±ä¼å å«ä¸ä¸æ ·çæ°æ®ãæ常è§ç解å³æ¹æ¡è¢«ç§°ä¸º **åºäºé¢å¯¼è çå¤å¶ï¼leader-based replicationï¼** ï¼ä¹ç§° **主å¨/被å¨ï¼active/passiveï¼** å¤å¶æ **主/ä»ï¼master/slaveï¼** å¤å¶ï¼ï¼å¦ [å¾ 5-1](#fig5-1.png) æ示ãå®çå·¥ä½åçå¦ä¸ï¼ 1. å ¶ä¸ä¸ä¸ªå¯æ¬è¢«æå®ä¸º **é¢å¯¼è ï¼leaderï¼**ï¼ä¹ç§°ä¸º **主åºï¼master|primaryï¼** ãå½å®¢æ·ç«¯è¦åæ°æ®åºåå ¥æ¶ï¼å®å¿ é¡»å°è¯·æ±åéç»è¯¥ **é¢å¯¼è **ï¼å ¶ä¼å°æ°æ°æ®åå ¥å ¶æ¬å°åå¨ã 2. å ¶ä»å¯æ¬è¢«ç§°ä¸º **追éè ï¼followersï¼**ï¼äº¦ç§°ä¸º **åªè¯»å¯æ¬ï¼read replicasï¼**ã**ä»åºï¼slavesï¼**ã**å¤åºï¼ secondariesï¼** æ **çå¤ï¼hot-standbyï¼**[^i]ãæ¯å½é¢å¯¼è å°æ°æ°æ®åå ¥æ¬å°åå¨æ¶ï¼å®ä¹ä¼å°æ°æ®åæ´åéç»ææç追éè ï¼ç§°ä¹ä¸º **å¤å¶æ¥å¿ï¼replication logï¼** æ **åæ´æµï¼change streamï¼**ãæ¯ä¸ªè·éè ä»é¢å¯¼è æåæ¥å¿ï¼å¹¶ç¸åºæ´æ°å ¶æ¬å°æ°æ®åºå¯æ¬ï¼æ¹æ³æ¯æç §ä¸é¢å¯¼è ç¸åçå¤ç顺åºæ¥è¿è¡ææåå ¥ã 3. å½å®¢æ·æ³è¦ä»æ°æ®åºä¸è¯»åæ°æ®æ¶ï¼å®å¯ä»¥åé¢å¯¼è æä»»ä¸è¿½éè è¿è¡æ¥è¯¢ãä½åªæé¢å¯¼è æè½æ¥ååå ¥æä½ï¼ä»å®¢æ·ç«¯çè§åº¦æ¥çä»åºé½æ¯åªè¯»çï¼ã [^i]: ä¸åç人对 **çï¼hotï¼**ã**温ï¼warmï¼** å **å·ï¼coldï¼** å¤ä»½æå¡å¨æä¸åçå®ä¹ãä¾å¦å¨ PostgreSQL ä¸ï¼**çå¤ï¼hot standbyï¼** æçæ¯è½æ¥å客æ·ç«¯è¯»è¯·æ±çå¯æ¬ãè **温å¤ï¼warm standbyï¼** åªæ¯è¿½éé¢å¯¼è ï¼ä½ä¸å¤ç客æ·ç«¯çä»»ä½æ¥è¯¢ãå°±æ¬ä¹¦èè¨ï¼è¿äºå·®å¼å¹¶ä¸éè¦ã ![](img/fig5-1.png) **å¾ 5-1 åºäºé¢å¯¼è çï¼ä¸»/ä»ï¼å¤å¶** è¿ç§å¤å¶æ¨¡å¼æ¯è®¸å¤å ³ç³»æ°æ®åºçå ç½®åè½ï¼å¦ PostgreSQLï¼ä» 9.0 çæ¬å¼å§ï¼ãMySQLãOracle Data Guardã2ãå SQL Server ç AlwaysOn å¯ç¨æ§ç»ã3ããå®ä¹è¢«ç¨äºä¸äºéå ³ç³»æ°æ®åºï¼å æ¬ MongoDBãRethinkDB å Espressoã4ããæåï¼åºäºé¢å¯¼è çå¤å¶å¹¶ä¸ä» éäºæ°æ®åºï¼å Kafkaã5ãå RabbitMQ é«å¯ç¨éåã6ãè¿æ ·çåå¸å¼æ¶æ¯ä»£çä¹ä½¿ç¨å®ãæäºç½ç»æ件系ç»ï¼ä¾å¦ DRBD è¿æ ·çåå¤å¶è®¾å¤ä¹ä¸ä¹ç±»ä¼¼ã ### åæ¥å¤å¶ä¸å¼æ¥å¤å¶ å¤å¶ç³»ç»çä¸ä¸ªéè¦ç»èæ¯ï¼å¤å¶æ¯ **åæ¥ï¼synchronouslyï¼** åççè¿æ¯ **å¼æ¥ï¼asynchronouslyï¼** åççãï¼å¨å ³ç³»åæ°æ®åºä¸è¿é常æ¯ä¸ä¸ªé 置项ï¼å ¶ä»ç³»ç»åé常硬ç¼ç ä¸ºå ¶ä¸ä¸ä¸ªï¼ã æ³è±¡ä¸ä¸ [å¾ 5-1](fig5-1.png) ä¸åççåºæ¯ï¼å³ç½ç«çç¨æ·æ´æ°ä»ä»¬ç个人头åãå¨æ个æ¶é´ç¹ï¼å®¢æ·å主åºåéæ´æ°è¯·æ±ï¼ä¸ä¹ ä¹å主åºå°±æ¶å°äºè¯·æ±ãå¨æ个æ¶é´ç¹ï¼ä¸»åºåä¼å°æ°æ®åæ´è½¬åç»èªå·±çä»åºãæç»ï¼ä¸»åºéç¥å®¢æ·æ´æ°æåã [å¾ 5-2](img/fig5-2.png) æ¾ç¤ºäºç³»ç»å个ç»ä»¶ä¹é´çéä¿¡ï¼ç¨æ·å®¢æ·ç«¯ã主åºå两个ä»åºãæ¶é´ä»å·¦åå³æµå¨ã请æ±æååºæ¶æ¯ç¨ç²ç®å¤´è¡¨ç¤ºã ![](img/fig5-2.png) **å¾ 5-2 åºäºé¢å¯¼è çå¤å¶ï¼ä¸ä¸ªåæ¥ä»åºåä¸ä¸ªå¼æ¥ä»åº** å¨ [å¾ 5-2](img/fig5-2.png) ç示ä¾ä¸ï¼ä»åº 1 çå¤å¶æ¯åæ¥çï¼å¨åç¨æ·æ¥ååå ¥æå并使ç»æå¯¹å ¶ä»ç¨æ·å¯è§ä¹åï¼ä¸»åºéè¦çå¾ ä»åº 1 ç确认ï¼ç¡®ä¿ä»åº 1 å·²ç»æ¶å°åå ¥æä½ãèä»åº 2 çå¤å¶æ¯å¼æ¥çï¼ä¸»åºåéæ¶æ¯ï¼ä½ä¸çå¾ è¯¥ä»åºçååºã å¨è¿å¹ å¾ä¸ï¼ä»åº 2 å¤çæ¶æ¯ååå¨ä¸ä¸ªæ¾èç延è¿ãé常æ åµä¸ï¼å¤å¶çé度ç¸å½å¿«ï¼å¤§å¤æ°æ°æ®åºç³»ç»è½å¨ä¸å°ä¸ç§å å®æä»åºçåæ¥ï¼ä½å®ä»¬ä¸è½æä¾å¤å¶ç¨æ¶çä¿è¯ãæäºæ åµä¸ï¼ä»åºå¯è½è½å主åºå åéææ´ä¹ ï¼ä¾å¦ï¼ä»åºæ£å¨ä»æ éä¸æ¢å¤ï¼ç³»ç»æ£å¨æ大容ééè¿è¿è¡ï¼æè å½èç¹é´åå¨ç½ç»é®é¢æ¶ã åæ¥å¤å¶çä¼ç¹æ¯ï¼ä»åºè½ä¿è¯æä¸ä¸»åºä¸è´çææ°æ°æ®å¯æ¬ãå¦æ主åºçªç¶å¤±æï¼æ们å¯ä»¥ç¡®ä¿¡è¿äºæ°æ®ä»ç¶è½å¨ä»åºä¸æ¾å°ã缺ç¹æ¯ï¼å¦æåæ¥ä»åºæ²¡æååºï¼æ¯å¦å®å·²ç»å´©æºï¼æè åºç°ç½ç»æ éï¼æå ¶å®ä»»ä½åå ï¼ï¼ä¸»åºå°±æ æ³å¤çåå ¥æä½ã主åºå¿ é¡»é»æ¢ææåå ¥ï¼å¹¶çå¾ åæ¥å¯æ¬å次å¯ç¨ã å æ¤ï¼å°ææä»åºé½è®¾ç½®ä¸ºåæ¥çæ¯ä¸åå®é çï¼ä»»ä½ä¸ä¸ªèç¹çä¸æé½ä¼å¯¼è´æ´ä¸ªç³»ç»åæ»ä¸åãå®é ä¸ï¼å¦æå¨æ°æ®åºä¸å¯ç¨åæ¥å¤å¶ï¼é常æå³çå ¶ä¸ **ä¸ä¸ª** ä»åºæ¯åæ¥çï¼èå ¶ä»çä»åºåæ¯å¼æ¥çãå¦æ该åæ¥ä»åºåå¾ä¸å¯ç¨æç¼æ ¢ï¼åå°ä¸ä¸ªå¼æ¥ä»åºæ¹ä¸ºåæ¥è¿è¡ãè¿ä¿è¯ä½ è³å°å¨ä¸¤ä¸ªèç¹ä¸æ¥æææ°çæ°æ®å¯æ¬ï¼ä¸»åºååæ¥ä»åºãè¿ç§é ç½®ææ¶ä¹è¢«ç§°ä¸º **ååæ¥ï¼semi-synchronousï¼**ã7ãã é常æ åµä¸ï¼åºäºé¢å¯¼è çå¤å¶é½é 置为å®å ¨å¼æ¥ãå¨è¿ç§æ åµä¸ï¼å¦æ主åºå¤±æä¸ä¸å¯æ¢å¤ï¼åä»»ä½å°æªå¤å¶ç»ä»åºçåå ¥é½ä¼ä¸¢å¤±ãè¿æå³çå³ä½¿å·²ç»å客æ·ç«¯ç¡®è®¤æåï¼åå ¥ä¹ä¸è½ä¿è¯æ¯ **æä¹ ï¼Durableï¼** çãç¶èï¼ä¸ä¸ªå®å ¨å¼æ¥çé ç½®ä¹æä¼ç¹ï¼å³ä½¿ææçä»åºé½è½åäºï¼ä¸»åºä¹å¯ä»¥ç»§ç»å¤çåå ¥ã å¼±åçæä¹ æ§å¯è½å¬èµ·æ¥åæ¯ä¸ä¸ªåçæè¡·ï¼ä½å¼æ¥å¤å¶å ¶å®å·²ç»è¢«å¹¿æ³ä½¿ç¨äºï¼ç¹å«æ¯å¨æå¾å¤ä»åºçåºæ¯ä¸ï¼æè å½ä»åºå¨å°çä¸åå¸å¾å¹¿çæ¶åãæ们å°å¨è®¨è®º â[å¤å¶å»¶è¿é®é¢](#å¤å¶å»¶è¿é®é¢)â æ¶åå°è¿ä¸ªé®é¢ã > ### å ³äºå¤å¶çç 究 > > 对äºå¼æ¥å¤å¶ç³»ç»èè¨ï¼ä¸»åºæ éæ¶ä¼ä¸¢å¤±æ°æ®å¯è½æ¯ä¸ä¸ªä¸¥éçé®é¢ï¼å æ¤ç 究人åä»å¨ç 究ä¸ä¸¢æ°æ®ä½ä»è½æä¾è¯å¥½æ§è½åå¯ç¨æ§çå¤å¶æ¹æ³ãä¾å¦ï¼**é¾å¼å¤å¶ï¼chain replicationï¼**ã8,9ãæ¯åæ¥å¤å¶çä¸ç§åä½ï¼å·²ç»å¨ä¸äºç³»ç»ï¼å¦ Microsoft Azure Storageã10,11ãï¼ä¸æåå®ç°ã > > å¤å¶çä¸è´æ§ä¸ **å ±è¯**ï¼consensusï¼ä½¿å 个èç¹å°±æ个å¼è¾¾æä¸è´ï¼ä¹é´æçå¯åçèç³»ï¼[第ä¹ç« ](ch9.md) å°è¯¦ç»å°æ¢è®¨è¿ä¸é¢åçç论ãæ¬ç« 主è¦è®¨è®ºå®è·µä¸çæ°æ®åºå¸¸ç¨çç®åå¤å¶å½¢å¼ã > ### 设置æ°ä»åº ææ¶åéè¦è®¾ç½®ä¸ä¸ªæ°çä»åºï¼ä¹è®¸æ¯ä¸ºäºå¢å å¯æ¬çæ°éï¼ææ¿æ¢å¤±è´¥çèç¹ãå¦ä½ç¡®ä¿æ°çä»åºæ¥æ主åºæ°æ®ç精确å¯æ¬ï¼ ç®åå°å°æ°æ®æ件ä»ä¸ä¸ªèç¹å¤å¶å°å¦ä¸ä¸ªèç¹é常æ¯ä¸å¤çï¼å®¢æ·ç«¯ä¸æåæ°æ®åºåå ¥æ°æ®ï¼æ°æ®æ»æ¯å¨ä¸æå°ååï¼æ åçæ件å¤å¶ä¼çå°æ°æ®åºçä¸åé¨åå¨ä¸åçæ¶é´ç¹çå 容ï¼å ¶ç»æå¯è½æ²¡æä»»ä½æä¹ã å¯ä»¥éè¿éå®æ°æ®åºï¼ä½¿å ¶ä¸å¯ç¨äºåå ¥ï¼æ¥ä½¿ç£çä¸çæ件ä¿æä¸è´ï¼ä½æ¯è¿ä¼è¿èé«å¯ç¨çç®æ ã幸è¿çæ¯ï¼è®¾ç½®æ°ä»åºé常并ä¸éè¦åæºãä»æ¦å¿µä¸è®²ï¼å ¶è¿ç¨å¦ä¸æç¤ºï¼ 1. å¨æ个æ¶å»è·å主åºçä¸è´æ§å¿«ç §ï¼å¦æå¯è½ï¼ä¸å¿ éå®æ´ä¸ªæ°æ®åºï¼ã大å¤æ°æ°æ®åºé½å ·æè¿ä¸ªåè½ï¼å 为å®æ¯å¤ä»½å¿ éçã对äºæäºåºæ¯ï¼å¯è½éè¦ç¬¬ä¸æ¹å·¥å ·ï¼ä¾å¦ç¨äº MySQL ç innobackupexã12ãã 2. å°å¿«ç §å¤å¶å°æ°çä»åºèç¹ã 3. ä»åºè¿æ¥å°ä¸»åºï¼å¹¶æåå¿«ç §ä¹ååççæææ°æ®åæ´ãè¿è¦æ±å¿«ç §ä¸ä¸»åºå¤å¶æ¥å¿ä¸çä½ç½®ç²¾ç¡®å ³èã该ä½ç½®æä¸åçå称ï¼ä¾å¦ PostgreSQL å°å ¶ç§°ä¸º **æ¥å¿åºåå·ï¼log sequence numberï¼LSNï¼**ï¼MySQL å°å ¶ç§°ä¸º **äºè¿å¶æ¥å¿åæ ï¼binlog coordinatesï¼**ã 4. å½ä»åºå¤çå®å¿«ç §ä¹å积累çæ°æ®åæ´ï¼æä»¬å°±è¯´å® **赶ä¸ï¼caught upï¼** äºä¸»åºï¼ç°å¨å®å¯ä»¥ç»§ç»åæ¶å¤ç主åºäº§ççæ°æ®ååäºã 建ç«ä»åºçå®é æ¥éª¤å æ°æ®åºèå¼ãå¨æäºç³»ç»ä¸ï¼è¿ä¸ªè¿ç¨æ¯å®å ¨èªå¨åçï¼èå¨å¦å¤ä¸äºç³»ç»ä¸ï¼å®å¯è½æ¯ä¸ä¸ªéè¦ç±ç®¡çåæå¨æ§è¡çãæç¹ç¥ç§çå¤æ¥éª¤å·¥ä½æµã ### å¤çèç¹å®æº ç³»ç»ä¸çä»»ä½èç¹é½å¯è½å®æºï¼å¯è½å 为æå¤çæ éï¼ä¹å¯è½ç±äºè®¡åå çç»´æ¤ï¼ä¾å¦ï¼éå¯æºå¨ä»¥å®è£ å æ ¸å®å ¨è¡¥ä¸ï¼ã对è¿ç»´èè¨ï¼è½å¨ç³»ç»ä¸ä¸ææå¡çæ åµä¸éå¯å个èç¹å¥½å¤å¤å¤ãæ们çç®æ æ¯ï¼å³ä½¿ä¸ªå«èç¹å¤±æï¼ä¹è½ä¿ææ´ä¸ªç³»ç»è¿è¡ï¼å¹¶å°½å¯è½æ§å¶èç¹åæºå¸¦æ¥çå½±åã å¦ä½éè¿åºäºé¢å¯¼è çå¤å¶å®ç°é«å¯ç¨ï¼ #### ä»åºå¤±æï¼è¿½èµ¶æ¢å¤ å¨å ¶æ¬å°ç£çä¸ï¼æ¯ä¸ªä»åºè®°å½ä»ä¸»åºæ¶å°çæ°æ®åæ´ãå¦æä»åºå´©æºå¹¶éæ°å¯å¨ï¼æè ï¼å¦æ主åºåä»åºä¹é´çç½ç»ææ¶ä¸æï¼åæ¯è¾å®¹ææ¢å¤ï¼ä»åºå¯ä»¥ä»æ¥å¿ä¸ç¥éï¼å¨åçæ éä¹åå¤ççæåä¸ä¸ªäºå¡ãå æ¤ï¼ä»åºå¯ä»¥è¿æ¥å°ä¸»åºï¼å¹¶è¯·æ±å¨ä»åºæå¼æé´åççæææ°æ®åæ´ãå½åºç¨å®ææè¿äºåæ´åï¼å®å°±èµ¶ä¸äºä¸»åºï¼å¹¶å¯ä»¥å以åä¸æ ·ç»§ç»æ¥æ¶æ°æ®åæ´æµã #### 主åºå¤±æï¼æ éåæ¢ ä¸»åºå¤±æå¤çèµ·æ¥ç¸å½æ£æï¼å ¶ä¸ä¸ä¸ªä»åºéè¦è¢«æå为æ°ç主åºï¼éè¦éæ°é 置客æ·ç«¯ï¼ä»¥å°å®ä»¬çåæä½åéç»æ°ç主åºï¼å ¶ä»ä»åºéè¦å¼å§æåæ¥èªæ°ä¸»åºçæ°æ®åæ´ãè¿ä¸ªè¿ç¨è¢«ç§°ä¸º **æ éåæ¢ï¼failoverï¼**ã æ éåæ¢å¯ä»¥æå¨è¿è¡ï¼éç¥ç®¡çå主åºæäºï¼å¹¶éåå¿ è¦çæ¥éª¤æ¥å建æ°ç主åºï¼æèªå¨è¿è¡ãèªå¨çæ éåæ¢è¿ç¨é常ç±ä»¥ä¸æ¥éª¤ç»æï¼ 1. 确认主åºå¤±æãæå¾å¤äºæ å¯è½ä¼åºéï¼å´©æºãåçµãç½ç»é®é¢ççã没æä¸æ ä¸å¤±çæ¹æ³æ¥æ£æµåºç°äºä»ä¹é®é¢ï¼æ以大å¤æ°ç³»ç»åªæ¯ç®åä½¿ç¨ **è¶ æ¶ï¼Timeoutï¼** ï¼èç¹é¢ç¹å°ç¸äºæ¥åä¼ éæ¶æ¯ï¼å¦æä¸ä¸ªèç¹å¨ä¸æ®µæ¶é´å ï¼ä¾å¦ 30 ç§ï¼æ²¡æååºï¼å°±è®¤ä¸ºå®æäºï¼å 为计åå ç»´æ¤èæ æå ³é主åºä¸ç®ï¼ã 2. éæ©ä¸ä¸ªæ°ç主åºãè¿å¯ä»¥éè¿é举è¿ç¨ï¼ä¸»åºç±å©ä½å¯æ¬ä»¥å¤æ°é举产çï¼æ¥å®æï¼æè å¯ä»¥ç±ä¹åéå®ç **æ§å¶å¨èç¹ï¼controller nodeï¼** æ¥æå®æ°ç主åºã主åºçæ佳人éé常æ¯æ¥ææ§ä¸»åºææ°æ°æ®å¯æ¬çä»åºï¼ä»¥æå°åæ°æ®æ失ï¼ã让ææçèç¹åæä¸ä¸ªæ°çé¢å¯¼è ï¼æ¯ä¸ä¸ª **å ±è¯** é®é¢ï¼å°å¨ [第ä¹ç« ](ch9.md) 详ç»è®¨è®ºã 3. éæ°é 置系ç»ä»¥å¯ç¨æ°ç主åºã客æ·ç«¯ç°å¨éè¦å°å®ä»¬çå请æ±åéç»æ°ä¸»åºï¼å°å¨ â[请æ±è·¯ç±](ch6.md#请æ±è·¯ç±)â ä¸è®¨è®ºè¿ä¸ªé®é¢ï¼ãå¦ææ§ä¸»åºæ¢å¤ï¼å¯è½ä»ç¶è®¤ä¸ºèªå·±æ¯ä¸»åºï¼è没ææè¯å°å ¶ä»å¯æ¬å·²ç»è®©å®å¤±å»é¢å¯¼æäºãç³»ç»éè¦ç¡®ä¿æ§ä¸»åºæè¯å°æ°ä¸»åºçåå¨ï¼å¹¶æ为ä¸ä¸ªä»åºã æ éåæ¢çè¿ç¨ä¸æå¾å¤å°æ¹å¯è½åºéï¼ * å¦æ使ç¨å¼æ¥å¤å¶ï¼åæ°ä¸»åºå¯è½æ²¡ææ¶å°è主åºå®æºåæåçåå ¥æä½ãå¨éåºæ°ä¸»åºåï¼å¦æè主åºéæ°å å ¥é群ï¼å该å¦ä½å¤çè¿äºè主åºå°æªå¤å¶çåå ¥ï¼å¨æ¤æé´ï¼æ°ä¸»åºå¯è½å·²ç»æ¶å°äºä¸è主åºå°æªå¤å¶çåå ¥ç¸å²çªçåå ¥ãæ常è§ç解å³æ¹æ¡æ¯ç®å丢å¼è主åºæªå¤å¶çåå ¥ï¼è¿å¾å¯è½æç ´å®¢æ·å¯¹äºæ°æ®æä¹ æ§çææã * å¦ææ°æ®åºéè¦åå ¶ä»å¤é¨åå¨ç¸åè°ï¼é£ä¹ä¸¢å¼åå ¥å 容æ¯æå ¶å±é©çæä½ãä¾å¦å¨ GitHub ã13ãçä¸åºäºæ ä¸ï¼ä¸ä¸ªè¿æ¶ç MySQL ä»åºè¢«æå为主åºãæ°æ®åºä½¿ç¨èªå¢ ID ä½ä¸ºä¸»é®ï¼å 为æ°ä¸»åºç计æ°å¨è½åäºè主åºç计æ°å¨ï¼æ以æ°ä¸»åºéæ°åé äºä¸äºå·²ç»è¢«è主åºåé æç ID ä½ä¸ºä¸»é®ãè¿äºä¸»é®ä¹å¨ Redis ä¸ä½¿ç¨ï¼ä¸»é®éç¨ä½¿å¾ MySQL å Redis ä¸çæ°æ®äº§çä¸ä¸è´ï¼æå导è´ä¸äºç§ææ°æ®æ³æ¼å°é误çç¨æ·æä¸ã * åçæäºæ éæ¶ï¼è§ [ç¬¬å «ç« ](ch8.md)ï¼å¯è½ä¼åºç°ä¸¤ä¸ªèç¹é½ä»¥ä¸ºèªå·±æ¯ä¸»åºçæ åµãè¿ç§æ åµç§°ä¸º **èè£ï¼split brainï¼**ï¼é常å±é©ï¼å¦æ两个主åºé½å¯ä»¥æ¥ååæä½ï¼å´æ²¡æå²çªè§£å³æºå¶ï¼è¯·åé â[å¤ä¸»å¤å¶](#å¤ä¸»å¤å¶)âï¼ï¼é£ä¹æ°æ®å°±å¯è½ä¸¢å¤±ææåãä¸äºç³»ç»éåäºå®å ¨é²èæªæ½ï¼å½æ£æµå°ä¸¤ä¸ªä¸»åºèç¹åæ¶åå¨æ¶ä¼å ³éå ¶ä¸ä¸ä¸ªèç¹ [^ii]ï¼ä½è®¾è®¡ç²ç³çæºå¶å¯è½æåä¼å¯¼è´ä¸¤ä¸ªèç¹é½è¢«å ³éã14ãã [^ii]: è¿ç§æºå¶ç§°ä¸º **å±éï¼fencingï¼**ï¼æè æ´å 满ææ çæ¯è¯æ¯ï¼**çå½¼ä¹å¤´ï¼Shoot The Other Node In The Head, STONITHï¼**ãæ们å°å¨ â[é¢å¯¼è åé](ch8.md#é¢å¯¼è åé)â ä¸å¯¹å±éè¿è¡è¯¦ç»è®¨è®ºã * 主åºè¢«å®£åæ»äº¡ä¹åçæ£ç¡®è¶ æ¶åºè¯¥æä¹é ç½®ï¼å¨ä¸»åºå¤±æçæ åµä¸ï¼è¶ æ¶æ¶é´è¶é¿æå³çæ¢å¤æ¶é´ä¹è¶é¿ãä½æ¯å¦æè¶ æ¶è®¾ç½®å¤ªçï¼åå¯è½ä¼åºç°ä¸å¿ è¦çæ éåæ¢ãä¾å¦ï¼ä¸´æ¶çè´è½½å³°å¼å¯è½å¯¼è´èç¹çååºæ¶é´å¢å å°è¶ åºè¶ æ¶æ¶é´ï¼æè ç½ç»æ éä¹å¯è½å¯¼è´æ°æ®å 延è¿ãå¦æç³»ç»å·²ç»å¤äºé«è´è½½æç½ç»é®é¢çå°æ°ä¹ä¸ï¼é£ä¹ä¸å¿ è¦çæ éåæ¢å¯è½ä¼è®©æ åµåå¾æ´ç³ç³ã è¿äºé®é¢æ²¡æç®åç解å³æ¹æ¡ãå æ¤ï¼å³ä½¿è½¯ä»¶æ¯æèªå¨æ éåæ¢ï¼ä¸å°è¿ç»´å¢éè¿æ¯æ´æ¿ææå¨æ§è¡æ éåæ¢ã èç¹æ éãä¸å¯é çç½ç»ã对å¯æ¬ä¸è´æ§ãæä¹ æ§ãå¯ç¨æ§å延è¿çæè¡¡ï¼è¿äºé®é¢å®é ä¸æ¯åå¸å¼ç³»ç»ä¸çåºæ¬é®é¢ã[ç¬¬å «ç« ](ch8.md) å [第ä¹ç« ](ch9.md) å°æ´æ·±å ¥å°è®¨è®ºå®ä»¬ã ### å¤å¶æ¥å¿çå®ç° åºäºé¢å¯¼è çå¤å¶å¨åºå±æ¯å¦ä½å·¥ä½çï¼å®è·µä¸æ好å ç§ä¸åçå¤å¶æ¹å¼ï¼æ以å ç®è¦å°çä¸ä¸ã #### åºäºè¯å¥çå¤å¶ å¨æç®åçæ åµä¸ï¼ä¸»åºè®°å½ä¸å®æ§è¡çæ¯ä¸ªåå ¥è¯·æ±ï¼**è¯å¥**ï¼å³ statementï¼å¹¶å°è¯¥è¯å¥æ¥å¿åéç»ä»åºã对äºå ³ç³»æ°æ®åºæ¥è¯´ï¼è¿æå³çæ¯ä¸ª `INSERT`ã`UPDATE` æ `DELETE` è¯å¥é½è¢«è½¬åç»æ¯ä¸ªä»åºï¼æ¯ä¸ªä»åºè§£æ并æ§è¡è¯¥ SQL è¯å¥ï¼å°±åç´æ¥ä»å®¢æ·ç«¯æ¶å°ä¸æ ·ã è½ç¶å¬ä¸å»å¾åçï¼ä½æå¾å¤é®é¢ä¼æç ¸è¿ç§å¤å¶æ¹å¼ï¼ * ä»»ä½è°ç¨ **éç¡®å®æ§å½æ°ï¼nondeterministicï¼** çè¯å¥ï¼å¯è½ä¼å¨æ¯ä¸ªå¯æ¬ä¸çæä¸åçå¼ãä¾å¦ï¼ä½¿ç¨ `NOW()` è·åå½åæ¥ææ¶é´ï¼æä½¿ç¨ `RAND()` è·åä¸ä¸ªéæºæ°ã * å¦æè¯å¥ä½¿ç¨äº **èªå¢åï¼auto incrementï¼**ï¼æè ä¾èµäºæ°æ®åºä¸çç°ææ°æ®ï¼ä¾å¦ï¼`UPDATE ... WHERE <æäºæ¡ä»¶>`ï¼ï¼åå¿ é¡»å¨æ¯ä¸ªå¯æ¬ä¸æç §å®å ¨ç¸åç顺åºæ§è¡å®ä»¬ï¼å¦åå¯è½ä¼äº§çä¸åçææãå½æå¤ä¸ªå¹¶åæ§è¡çäºå¡æ¶ï¼è¿å¯è½æ为ä¸ä¸ªéå¶ã * æå¯ä½ç¨çè¯å¥ï¼ä¾å¦ï¼è§¦åå¨ãåå¨è¿ç¨ãç¨æ·å®ä¹çå½æ°ï¼å¯è½ä¼å¨æ¯ä¸ªå¯æ¬ä¸äº§çä¸åçå¯ä½ç¨ï¼é¤éå¯ä½ç¨æ¯ç»å¯¹ç¡®å®æ§çã çç¡®æåæ³ç»å¼è¿äºé®é¢ ââ ä¾å¦ï¼å½è¯å¥è¢«è®°å½æ¶ï¼ä¸»åºå¯ä»¥ç¨åºå®çè¿åå¼æ¿æ¢æä»»ä½ä¸ç¡®å®çå½æ°è°ç¨ï¼ä»¥ä¾¿ææä»åºé½è½è·å¾ç¸åçå¼ãä½æ¯ç±äºè¾¹ç¼æ åµå®å¨å¤ªå¤äºï¼ç°å¨é常ä¼éæ©å ¶ä»çå¤å¶æ¹æ³ã åºäºè¯å¥çå¤å¶å¨ 5.1 çæ¬åç MySQL ä¸è¢«ä½¿ç¨å°ãå 为å®ç¸å½ç´§åï¼ç°å¨ææ¶åä¹è¿å¨ç¨ãä½ç°å¨å¨é»è®¤æ åµä¸ï¼å¦æè¯å¥ä¸åå¨ä»»ä½ä¸ç¡®å®æ§ï¼MySQL ä¼åæ¢å°åºäºè¡çå¤å¶ï¼ç¨å讨论ï¼ãVoltDB 使ç¨äºåºäºè¯å¥çå¤å¶ï¼ä½è¦æ±äºå¡å¿ é¡»æ¯ç¡®å®æ§çï¼ä»¥æ¤æ¥ä¿è¯å®å ¨ã15ãã #### ä¼ è¾é¢åå¼æ¥å¿ï¼WALï¼ å¨ [第ä¸ç« ](ch3.md) ä¸ï¼æ们讨论äºåå¨å¼æå¦ä½å¨ç£çä¸è¡¨ç¤ºæ°æ®ï¼æ们ä¹åç°äºé常ä¼å°åæä½è¿½å å°æ¥å¿ä¸ï¼ * 对äºæ¥å¿ç»æåå¨å¼æï¼è¯·åé â[SSTables å LSM æ ](ch3.md#SSTablesåLSMæ )âï¼ï¼æ¥å¿æ¯ä¸»è¦çåå¨ä½ç½®ãæ¥å¿æ®µå¨åå°å缩ï¼å¹¶è¿è¡åå¾åæ¶ã * 对äºè¦åå个ç£çåç [B æ ](ch3.md#Bæ )ï¼æ¯æ¬¡ä¿®æ¹é½ä¼å åå ¥ **é¢åå¼æ¥å¿ï¼Write Ahead Log, WALï¼**ï¼ä»¥ä¾¿å´©æºåç´¢å¼å¯ä»¥æ¢å¤å°ä¸ä¸ªä¸è´çç¶æã å¨ä»»ä½ä¸ç§æ åµä¸ï¼è¯¥æ¥å¿é½æ¯å å«äºæææ°æ®åºåå ¥çä» è¿½å åèåºåãå¯ä»¥ä½¿ç¨å®å ¨ç¸åçæ¥å¿å¨å¦ä¸ä¸ªèç¹ä¸æ建å¯æ¬ï¼é¤äºå°æ¥å¿åå ¥ç£çä¹å¤ï¼ä¸»åºè¿å¯ä»¥éè¿ç½ç»å°å ¶åéç»ä»åºã éè¿ä½¿ç¨è¿ä¸ªæ¥å¿ï¼ä»åºå¯ä»¥æ建ä¸ä¸ªä¸ä¸»åºä¸æ¨¡ä¸æ ·çæ°æ®ç»ææ·è´ã è¿ç§å¤å¶æ¹æ³å¨ PostgreSQL å Oracle çä¸äºäº§åä¸è¢«ä½¿ç¨å°ã16ããå ¶ä¸»è¦ç¼ºç¹æ¯æ¥å¿è®°å½çæ°æ®é常åºå±ï¼WAL å å«åªäºç£çåä¸çåªäºåèåçäºæ´æ¹ãè¿ä½¿å¤å¶ä¸åå¨å¼æç´§å¯è¦åãå¦ææ°æ®åºå°å ¶åå¨æ ¼å¼ä»ä¸ä¸ªçæ¬æ´æ¹ä¸ºå¦ä¸ä¸ªçæ¬ï¼é常ä¸å¯è½å¨ä¸»åºåä»åºä¸è¿è¡ä¸åçæ¬çæ°æ®åºè½¯ä»¶ã çä¸å»è¿å¯è½åªæ¯ä¸ä¸ªå°çå®ç°ç»èï¼ä½å´å¯è½å¯¹è¿ç»´äº§ç巨大çå½±åãå¦æå¤å¶åè®®å 许ä»åºä½¿ç¨æ¯ä¸»åºæ´æ°ç软件çæ¬ï¼åå¯ä»¥å å级ä»åºï¼ç¶åæ§è¡æ éåæ¢ï¼ä½¿å级åçèç¹ä¹ä¸æ为æ°ç主åºï¼ä»èå 许æ°æ®åºè½¯ä»¶çé¶åæºå级ãå¦æå¤å¶åè®®ä¸å 许çæ¬ä¸å¹é ï¼ä¼ è¾ WAL ç»å¸¸åºç°è¿ç§æ åµï¼ï¼åæ¤ç±»å级éè¦åæºã #### é»è¾æ¥å¿å¤å¶ï¼åºäºè¡ï¼ å¦ä¸ç§æ¹æ³æ¯å¯¹å¤å¶ååå¨å¼æ使ç¨ä¸åçæ¥å¿æ ¼å¼ï¼è¿æ ·å¯ä»¥å°å¤å¶æ¥å¿ä»åå¨å¼æçå é¨å®ç°ä¸è§£è¦åºæ¥ãè¿ç§å¤å¶æ¥å¿è¢«ç§°ä¸ºé»è¾æ¥å¿ï¼logical logï¼ï¼ä»¥å°å ¶ä¸åå¨å¼æçï¼ç©çï¼æ°æ®è¡¨ç¤ºåºåå¼æ¥ã å ³ç³»æ°æ®åºçé»è¾æ¥å¿é常æ¯ä»¥è¡çç²åº¦æ¥æ述对æ°æ®åºè¡¨çåå ¥è®°å½çåºåï¼ * 对äºæå ¥çè¡ï¼æ¥å¿å å«ææåçæ°å¼ã * 对äºå é¤çè¡ï¼æ¥å¿å å«è¶³å¤çä¿¡æ¯æ¥å¯ä¸æ è¯è¢«å é¤çè¡ï¼è¿é常æ¯ä¸»é®ï¼ä½å¦æ表ä¸æ²¡æ主é®ï¼åéè¦è®°å½ææåçæ§å¼ã * 对äºæ´æ°çè¡ï¼æ¥å¿å å«è¶³å¤çä¿¡æ¯æ¥å¯ä¸æ è¯è¢«æ´æ°çè¡ï¼ä»¥åææåçæ°å¼ï¼æè³å°ææå·²æ´æ¹çåçæ°å¼ï¼ã ä¿®æ¹å¤è¡çäºå¡ä¼çæå¤æ¡è¿æ ·çæ¥å¿è®°å½ï¼åé¢è·çä¸æ¡ææäºå¡å·²ç»æ交çè®°å½ãMySQL çäºè¿å¶æ¥å¿ï¼å½é 置为使ç¨åºäºè¡çå¤å¶æ¶ï¼ä½¿ç¨äºè¿ç§æ¹æ³ã17ãã ç±äºé»è¾æ¥å¿ä¸åå¨å¼æçå é¨å®ç°æ¯è§£è¦çï¼ç³»ç»å¯ä»¥æ´å®¹æå°åå°ååå ¼å®¹ï¼ä»è使主åºåä»åºè½å¤è¿è¡ä¸åçæ¬çæ°æ®åºè½¯ä»¶ï¼æè çè³ä¸åçåå¨å¼æã 对äºå¤é¨åºç¨ç¨åºæ¥è¯´ï¼é»è¾æ¥å¿æ ¼å¼ä¹æ´å®¹æ解æãå¦æè¦å°æ°æ®åºçå 容åéå°å¤é¨ç³»ç»ï¼ä¾å¦å¤å¶å°æ°æ®ä»åºè¿è¡ç¦»çº¿åæï¼æ建ç«èªå®ä¹ç´¢å¼åç¼åã18ãï¼è¿ä¸ç¹ä¼å¾æç¨ãè¿ç§ææ¯è¢«ç§°ä¸º **æ°æ®åæ´æè·ï¼change data captureï¼**ï¼[第åä¸ç« ](ch11.md) å°éæ°è®²å°å®ã #### åºäºè§¦åå¨çå¤å¶ å°ç®å为æ¢æè¿°çå¤å¶æ¹æ³æ¯ç±æ°æ®åºç³»ç»å®ç°çï¼ä¸æ¶åä»»ä½åºç¨ç¨åºä»£ç ãå¨å¾å¤æ åµä¸ï¼è¿å°±æ¯ä½ æ³è¦çãä½å¨æäºæ åµä¸éè¦æ´å¤ççµæ´»æ§ãä¾å¦ï¼å¦æä½ åªæ³å¤å¶æ°æ®çä¸ä¸ªåéï¼æè æ³ä»ä¸ç§æ°æ®åºå¤å¶å°å¦ä¸ç§æ°æ®åºï¼æè å¦æä½ éè¦å²çªè§£å³é»è¾ï¼è¯·åé â[å¤çåå ¥å²çª](#å¤çåå ¥å²çª)âï¼ï¼åå¯è½éè¦å°å¤å¶æä½ä¸ç§»å°åºç¨ç¨åºå±ã ä¸äºå·¥å ·ï¼å¦ Oracle Golden Gateã19ãï¼å¯ä»¥éè¿è¯»åæ°æ®åºæ¥å¿ï¼ä½¿å¾å ¶ä»åºç¨ç¨åºå¯ä»¥ä½¿ç¨æ°æ®ãå¦ä¸ç§æ¹æ³æ¯ä½¿ç¨è®¸å¤å ³ç³»æ°æ®åºèªå¸¦çåè½ï¼è§¦åå¨ååå¨è¿ç¨ã 触åå¨å è®¸ä½ å°æ°æ®æ´æ¹ï¼åå ¥äºå¡ï¼åçæ¶èªå¨æ§è¡çèªå®ä¹åºç¨ç¨åºä»£ç 注åå¨æ°æ®åºç³»ç»ä¸ã触åå¨ææºä¼å°æ´æ¹è®°å½å°ä¸ä¸ªåç¬ç表ä¸ï¼ä½¿ç¨å¤é¨ç¨åºè¯»åè¿ä¸ªè¡¨ï¼åå ä¸ä¸äºå¿ è¦çä¸å¡é»è¾ï¼å°±å¯ä»¥å°æ°æ®åæ´å¤å¶å°å¦ä¸ä¸ªç³»ç»å»ãä¾å¦ï¼Databus for Oracleã20ãå Bucardo for Postgresã21ãå°±æ¯è¿æ ·å·¥ä½çã åºäºè§¦åå¨çå¤å¶é常æ¯å ¶ä»å¤å¶æ¹æ³å ·ææ´é«çå¼éï¼å¹¶ä¸æ¯æ°æ®åºå ç½®çå¤å¶æ´å®¹æåºéï¼ä¹æå¾å¤éå¶ãç¶èç±äºå ¶çµæ´»æ§ï¼å®ä»ç¶æ¯å¾æç¨çã ## å¤å¶å»¶è¿é®é¢ 容å¿èç¹æ éåªæ¯éè¦å¤å¶çä¸ä¸ªåå ãæ£å¦å¨ [第äºé¨å](part-ii.md) çä»ç»ä¸æå°çï¼å ¶å®åå è¿å æ¬å¯ä¼¸ç¼©æ§ï¼å¤çæ¯å个æºå¨æ´å¤ç请æ±ï¼å延è¿ï¼è®©å¯æ¬å¨å°çä½ç½®ä¸æ´æ¥è¿ç¨æ·ï¼ã åºäºé¢å¯¼è çå¤å¶è¦æ±ææåå ¥é½ç±å个èç¹å¤çï¼ä½åªè¯»æ¥è¯¢å¯ä»¥ç±ä»»ä½ä¸ä¸ªå¯æ¬æ¥å¤çãæ以对äºè¯»å¤åå°çåºæ¯ï¼Web ä¸ç常è§æ¨¡å¼ï¼ï¼ä¸ä¸ªæå¸å¼åçéæ©æ¯å建å¾å¤ä»åºï¼å¹¶å°è¯»è¯·æ±åæ£å°ææçä»åºä¸å»ãè¿æ ·è½åå°ä¸»åºçè´è½½ï¼å¹¶å 许ç±éè¿çå¯æ¬æ¥å¤ç读请æ±ã å¨è¿ç§è¯»ä¼¸ç¼©ï¼read-scalingï¼çä½ç³»ç»æä¸ï¼åªéæ·»å æ´å¤çä»åºï¼å°±å¯ä»¥æé«åªè¯»è¯·æ±çæå¡å®¹éãä½æ¯ï¼è¿ç§æ¹æ³å®é ä¸åªéç¨äºå¼æ¥å¤å¶ ââ å¦æå°è¯åæ¥å¤å¶å°ææä»åºï¼åå个èç¹æ éæç½ç»ä¸æå°å¯¼è´æ´ä¸ªç³»ç»é½æ æ³åå ¥ãèä¸èç¹è¶å¤è¶æå¯è½åºç°ä¸ªå«èç¹å®æºçæ åµï¼æ以å®å ¨åæ¥çé ç½®å°æ¯é常ä¸å¯é çã ä¸å¹¸çæ¯ï¼å½åºç¨ç¨åºä»å¼æ¥ä»åºè¯»åæ¶ï¼å¦æä»åºè½åï¼å®å¯è½ä¼çå°è¿æ¶çä¿¡æ¯ãè¿ä¼å¯¼è´æ°æ®åºä¸åºç°ææ¾çä¸ä¸è´ï¼åæ¶å¯¹ä¸»åºåä»åºæ§è¡ç¸åçæ¥è¯¢ï¼å¯è½å¾å°ä¸åçç»æï¼å 为并éææçåå ¥é½åæ å¨ä»åºä¸ãè¿ç§ä¸ä¸è´åªæ¯ä¸ä¸ªææ¶çç¶æ ââ å¦æåæ¢åå ¥æ°æ®åºå¹¶çå¾ ä¸æ®µæ¶é´ï¼ä»åºæç»ä¼èµ¶ä¸å¹¶ä¸ä¸»åºä¿æä¸è´ãåºäºè¿ä¸ªåå ï¼è¿ç§æåºè¢«ç§°ä¸º **æç»ä¸è´æ§ï¼eventual consistencyï¼**ã22,23ãã[^iii] [^iii]: éæ ¼ææ¯ã»ç¹éï¼Douglas Terryï¼ç人ã24ãåé äºæç»ä¸è´æ§è¿ä¸ªæ¯è¯ï¼å¹¶ç»ç± Werner Vogelsã22ãçæ¨å¹¿ï¼æ为äºè®¸å¤ NoSQL 项ç®çå£å·ãç¶èï¼æç»ä¸è´æ§å¹¶ä¸åªå±äº NoSQL æ°æ®åºï¼å ³ç³»åæ°æ®åºä¸çå¼æ¥å¤å¶ä»åºä¹æç¸åçç¹æ§ã æç»ä¸è´æ§ä¸ç âæç»â ä¸è¯ææè¿è¡äºæ¨¡ç³åï¼æ»çæ¥è¯´ï¼å¯æ¬è½åçç¨åº¦æ¯æ²¡æéå¶çãå¨æ£å¸¸çæä½ä¸ï¼**å¤å¶å»¶è¿ï¼replication lagï¼**ï¼å³åå ¥ä¸»åºå°åæ è³ä»åºä¹é´ç延è¿ï¼å¯è½ä» ä» æ¯å åä¹ä¸ç§ï¼å¨å®è·µä¸å¹¶ä¸æ¾ç¼ãä½å¦æç³»ç»å¨æ¥è¿æéçæ åµä¸è¿è¡ï¼æç½ç»ä¸åå¨é®é¢æ¶ï¼å»¶è¿å¯ä»¥è½»èæ举å°è¶ è¿å ç§ï¼çè³è¾¾å°å åéã å 为æ»åæ¶é´å¤ªé¿å¼å ¥çä¸ä¸è´æ§ï¼ä¸ä» ä» æ¯ä¸ä¸ªç论é®é¢ï¼æ´æ¯åºç¨è®¾è®¡ä¸ä¼éå°ççå®é®é¢ãæ¬èå°éç¹ä»ç»ä¸ä¸ªå¨å¤å¶å»¶è¿æ¶å¯è½åççé®é¢å®ä¾ï¼å¹¶ç®è¿°è§£å³è¿äºé®é¢çä¸äºæ¹æ³ã ### 读己ä¹å 许å¤åºç¨è®©ç¨æ·æ交ä¸äºæ°æ®ï¼ç¶åæ¥çä»ä»¬æ交çå 容ãå¯è½æ¯ç¨æ·æ°æ®åºä¸çè®°å½ï¼ä¹å¯è½æ¯å¯¹è®¨è®ºä¸»é¢çè¯è®ºï¼æå ¶ä»ç±»ä¼¼çå 容ãæ交æ°æ°æ®æ¶ï¼å¿ é¡»å°å ¶åéç»ä¸»åºï¼ä½æ¯å½ç¨æ·æ¥çæ°æ®æ¶ï¼å¯ä»¥éè¿ä»åºè¿è¡è¯»åãå¦ææ°æ®ç»å¸¸è¢«æ¥çï¼ä½åªæ¯å¶å°åå ¥ï¼è¿æ¯é常åéçã ä½å¯¹äºå¼æ¥å¤å¶ï¼é®é¢å°±æ¥äºãå¦ [å¾ 5-3](fig5-3.png) æ示ï¼å¦æç¨æ·å¨åå ¥å马ä¸å°±æ¥çæ°æ®ï¼åæ°æ°æ®å¯è½å°æªå°è¾¾å¯æ¬ã对ç¨æ·èè¨ï¼çèµ·æ¥å¥½åæ¯åæ交çæ°æ®ä¸¢å¤±äºï¼æ以ä»ä»¬ä¸é«å ´æ¯å¯ä»¥ç解çã ![](img/fig5-3.png) **å¾ 5-3 ç¨æ·åå ¥åä»æ§å¯æ¬ä¸è¯»åæ°æ®ãéè¦åå读 (read-after-write) çä¸è´æ§æ¥é²æ¢è¿ç§å¼å¸¸** å¨è¿ç§æ åµä¸ï¼æ们éè¦ **åå读ä¸è´æ§ï¼read-after-write consistencyï¼**ï¼ä¹ç§°ä¸º **读己ä¹åä¸è´æ§ï¼read-your-writes consistencyï¼**ã24ããè¿æ¯ä¸ä¸ªä¿è¯ï¼å¦æç¨æ·éæ°å 载页é¢ï¼ä»ä»¬æ»ä¼çå°ä»ä»¬èªå·±æ交çä»»ä½æ´æ°ãå®ä¸ä¼å¯¹å ¶ä»ç¨æ·çåå ¥ååºæ¿è¯ºï¼å ¶ä»ç¨æ·çæ´æ°å¯è½ç¨çæä¼çå°ãå®ä¿è¯ç¨æ·èªå·±çè¾å ¥å·²è¢«æ£ç¡®ä¿åã å¦ä½å¨åºäºé¢å¯¼è çå¤å¶ç³»ç»ä¸å®ç°åå读ä¸è´æ§ï¼æåç§å¯è½çææ¯ï¼è¿é说ä¸äºï¼ * 对äºç¨æ· **å¯è½ä¿®æ¹è¿** çå 容ï¼æ»æ¯ä»ä¸»åºè¯»åï¼è¿å°±è¦æ±å¾æåæ³ä¸éè¿å®é çæ¥è¯¢å°±å¯ä»¥ç¥éç¨æ·æ¯å¦ä¿®æ¹äºæäºä¸è¥¿ã举个ä¾åï¼ç¤¾äº¤ç½ç»ä¸çç¨æ·ä¸ªäººèµæä¿¡æ¯é常åªè½ç±ç¨æ·æ¬äººç¼è¾ï¼èä¸è½ç±å ¶ä»äººç¼è¾ãå æ¤ä¸ä¸ªç®åçè§åå°±æ¯ï¼æ»æ¯ä»ä¸»åºè¯»åç¨æ·èªå·±çæ¡£æ¡ï¼å¦æè¦è¯»åå ¶ä»ç¨æ·çæ¡£æ¡å°±å»ä»åºã * å¦æåºç¨ä¸ç大é¨åå 容é½å¯è½è¢«ç¨æ·ç¼è¾ï¼é£è¿ç§æ¹æ³å°±æ²¡ç¨äºï¼å 为大é¨åå 容é½å¿ é¡»ä»ä¸»åºè¯»åï¼è¯»ä¼¸ç¼©å°±æ²¡ææäºï¼ãå¨è¿ç§æ åµä¸å¯ä»¥ä½¿ç¨å ¶ä»æ åæ¥å³å®æ¯å¦ä»ä¸»åºè¯»åãä¾å¦å¯ä»¥è·è¸ªä¸æ¬¡æ´æ°çæ¶é´ï¼å¨ä¸æ¬¡æ´æ°åçä¸åéå ï¼ä»ä¸»åºè¯»ãè¿å¯ä»¥çæ§ä»åºçå¤å¶å»¶è¿ï¼é²æ¢åä»»ä½æ»å主åºè¶ è¿ä¸åéçä»åºååºæ¥è¯¢ã * 客æ·ç«¯å¯ä»¥è®°ä½æè¿ä¸æ¬¡åå ¥çæ¶é´æ³ï¼ç³»ç»éè¦ç¡®ä¿ä»åºå¨å¤ç该ç¨æ·ç读å请æ±æ¶ï¼è¯¥æ¶é´æ³åçåæ´é½å·²ç»ä¼ æå°äºæ¬ä»åºä¸ãå¦æå½åä»åºä¸å¤æ°ï¼åå¯ä»¥ä»å¦ä¸ä¸ªä»åºè¯»åï¼æè çå¾ ä»åºè¿½èµ¶ä¸æ¥ãè¿éçæ¶é´æ³å¯ä»¥æ¯é»è¾æ¶é´æ³ï¼è¡¨ç¤ºåå ¥é¡ºåºçä¸è¥¿ï¼ä¾å¦æ¥å¿åºåå·ï¼æå®é çç³»ç»æ¶éï¼å¨è¿ç§æ åµä¸ï¼æ¶éåæ¥åå¾è³å ³éè¦ï¼è¯·åé â[ä¸å¯é çæ¶é](ch8.md#ä¸å¯é çæ¶é)âï¼ã * å¦æä½ çå¯æ¬åå¸å¨å¤ä¸ªæ°æ®ä¸å¿ï¼ä¸ºäºå¨å°çä¸æ¥è¿ç¨æ·æè åºäºå¯ç¨æ§ç®çï¼ï¼è¿ä¼æé¢å¤çå¤ææ§ãä»»ä½éè¦ç±ä¸»åºæä¾æå¡ç请æ±é½å¿ 须路ç±å°å å«è¯¥ä¸»åºçæ°æ®ä¸å¿ã å¦ä¸ç§å¤æçæ åµåçå¨åä¸ä½ç¨æ·ä»å¤ä¸ªè®¾å¤ï¼ä¾å¦æ¡é¢æµè§å¨åç§»å¨ APPï¼è¯·æ±æå¡çæ¶åãè¿ç§æ åµä¸å¯è½å°±éè¦æä¾è·¨è®¾å¤çåå读ä¸è´æ§ï¼å¦æç¨æ·å¨ä¸ä¸ªè®¾å¤ä¸è¾å ¥äºä¸äºä¿¡æ¯ï¼ç¶åå¨å¦ä¸ä¸ªè®¾å¤ä¸æ¥çï¼ååºè¯¥çå°ä»ä»¬åè¾å ¥çä¿¡æ¯ã å¨è¿ç§æ åµä¸ï¼è¿æä¸äºéè¦èèçé®é¢ï¼ * è®°ä½ç¨æ·ä¸æ¬¡æ´æ°æ¶é´æ³çæ¹æ³åå¾æ´å å°é¾ï¼å 为ä¸ä¸ªè®¾å¤ä¸è¿è¡çç¨åºä¸ç¥éå¦ä¸ä¸ªè®¾å¤ä¸åçäºä»ä¹ãéè¦å¯¹è¿äºå æ°æ®è¿è¡ä¸å¿åçåå¨ã * å¦æå¯æ¬åå¸å¨ä¸åçæ°æ®ä¸å¿ï¼å¾é¾ä¿è¯æ¥èªä¸å设å¤çè¿æ¥ä¼è·¯ç±å°åä¸æ°æ®ä¸å¿ãï¼ä¾å¦ï¼ç¨æ·çå°å¼è®¡ç®æºä½¿ç¨å®¶åºå®½å¸¦è¿æ¥ï¼è移å¨è®¾å¤ä½¿ç¨èçªæ°æ®ç½ç»ï¼å设å¤çç½ç»è·¯ç±å¯è½å®å ¨ä¸åï¼ãå¦æä½ çæ¹æ³éè¦è¯»ä¸»åºï¼å¯è½é¦å éè¦ææ¥èªè¯¥ç¨æ·ææ设å¤ç请æ±é½è·¯ç±å°åä¸ä¸ªæ°æ®ä¸å¿ã ### åè°è¯» å¨ä»å¼æ¥ä»åºè¯»åæ¶å¯è½åççå¼å¸¸ç第äºä¸ªä¾åæ¯ç¨æ·å¯è½ä¼éå° **æ¶å åæµï¼moving backward in timeï¼**ã å¦æç¨æ·ä»ä¸åä»åºè¿è¡å¤æ¬¡è¯»åï¼å°±å¯è½åçè¿ç§æ åµãä¾å¦ï¼[å¾ 5-4](img/fig5-4.png) æ¾ç¤ºäºç¨æ· 2345 两次è¿è¡ç¸åçæ¥è¯¢ï¼é¦å æ¥è¯¢äºä¸ä¸ªå»¶è¿å¾å°çä»åºï¼ç¶åæ¯ä¸ä¸ªå»¶è¿è¾å¤§çä»åºï¼å¦æç¨æ·å·æ°ç½é¡µæ¶æ¯ä¸ªè¯·æ±é½è¢«è·¯ç±å°ä¸ä¸ªéæºçæå¡å¨ï¼è¿ç§æ åµå°±å¾æå¯è½åçï¼ã第ä¸ä¸ªæ¥è¯¢è¿åäºæè¿ç±ç¨æ· 1234 æ·»å çè¯è®ºï¼ä½æ¯ç¬¬äºä¸ªæ¥è¯¢ä¸è¿åä»»ä½ä¸è¥¿ï¼å 为æ»åçä»åºè¿æ²¡ææåå°è¯¥åå ¥å 容ãå®é ä¸å¯ä»¥è®¤ä¸ºç¬¬äºä¸ªæ¥è¯¢æ¯å¨æ¯ç¬¬ä¸ä¸ªæ¥è¯¢æ´æ©çæ¶é´ç¹ä¸è§å¯ç³»ç»ãå¦æ第ä¸ä¸ªæ¥è¯¢æ²¡æè¿åä»»ä½å 容ï¼é£é®é¢å¹¶ä¸å¤§ï¼å 为ç¨æ· 2345 å¯è½ä¸ç¥éç¨æ· 1234 æè¿æ·»å äºè¯è®ºãä½å¦æç¨æ· 2345 å çè§ç¨æ· 1234 çè¯è®ºï¼ç¶ååçå°å®æ¶å¤±ï¼è¿å°±ä¼è®©äººè§å¾é常å°æäºã ![](img/fig5-4.png) **å¾ 5-4 ç¨æ·é¦å ä»æ°å¯æ¬è¯»åï¼ç¶åä»æ§å¯æ¬è¯»åãæ¶é´çä¸å»åéäºã为äºé²æ¢è¿ç§å¼å¸¸ï¼æ们éè¦åè°ç读åã** **åè°è¯»ï¼monotonic readsï¼**ã23ãå¯ä»¥ä¿è¯è¿ç§å¼å¸¸ä¸ä¼åçãè¿æ¯ä¸ä¸ªæ¯ **强ä¸è´æ§ï¼strong consistencyï¼** æ´å¼±ï¼ä½æ¯ **æç»ä¸è´æ§ï¼eventual consistencyï¼** æ´å¼ºçä¿è¯ãå½è¯»åæ°æ®æ¶ï¼ä½ å¯è½ä¼çå°ä¸ä¸ªæ§å¼ï¼åè°è¯»ä» æå³çå¦æä¸ä¸ªç¨æ·é¡ºåºå°è¿è¡å¤æ¬¡è¯»åï¼åä»ä»¬ä¸ä¼çå°æ¶é´åéï¼ä¹å°±æ¯è¯´ï¼å¦æå·²ç»è¯»åå°è¾æ°çæ°æ®ï¼åç»ç读åä¸ä¼å¾å°æ´æ§çæ°æ®ã å®ç°åè°è¯»çä¸ç§æ¹å¼æ¯ç¡®ä¿æ¯ä¸ªç¨æ·æ»æ¯ä»åä¸ä¸ªå¯æ¬è¿è¡è¯»åï¼ä¸åçç¨æ·å¯ä»¥ä»ä¸åçå¯æ¬è¯»åï¼ãä¾å¦ï¼å¯ä»¥åºäºç¨æ· ID çæ£åæ¥éæ©å¯æ¬ï¼èä¸æ¯éæºéæ©å¯æ¬ãä½æ¯ï¼å¦æ该å¯æ¬åºç°æ éï¼ç¨æ·çæ¥è¯¢å°éè¦éæ°è·¯ç±å°å¦ä¸ä¸ªå¯æ¬ã ### ä¸è´åç¼è¯» 第ä¸ä¸ªå¤å¶å»¶è¿å¼å¸¸çä¾åè¿åäºå æå¾ãæ³è±¡ä¸ä¸ Poons å çå Cake 夫人ä¹é´ç以ä¸ç®ç对è¯ï¼ *Mr. Poons* > Mrs. Cakeï¼ä½ è½çå°å¤è¿çæªæ¥ï¼ *Mrs. Cake* > é常约åç§éï¼Mr. Poons. è¿ä¸¤å¥è¯ä¹é´æå æå ³ç³»ï¼Cake 夫人å¬å°äº Poons å ççé®é¢å¹¶åçäºè¿ä¸ªé®é¢ã ç°å¨ï¼æ³è±¡ç¬¬ä¸ä¸ªäººæ£å¨éè¿ä»åºæ¥å¬è¿ä¸ªå¯¹è¯ãCake 夫人说çå 容æ¯ä»ä¸ä¸ªå»¶è¿å¾ä½çä»åºè¯»åçï¼ä½ Poons å çæ说çå 容ï¼ä»åºç延è¿è¦å¤§çå¤ï¼è§ [å¾ 5-5](img/fig5-5.png)ï¼ãäºæ¯ï¼è¿ä¸ªè§å¯è ä¼å¬å°ä»¥ä¸å å®¹ï¼ *Mrs. Cake* > é常约åç§éï¼Mr. Poons. *Mr. Poons* > Mrs. Cakeï¼ä½ è½çå°å¤è¿çæªæ¥ï¼ 对äºè§å¯è æ¥è¯´ï¼çèµ·æ¥å¥½å Cake å¤«äººå¨ Poons å çæé®åå°±åçäºè¿ä¸ªé®é¢ãè¿ç§è¶ è½å让人å°è±¡æ·±å»ï¼ä½ä¹ä¼æ人æç³æ¶ãã25ãã ![](img/fig5-5.png) **å¾ 5-5 å¦ææäºååºçå¤å¶éåº¦æ ¢äºå ¶ä»ååºï¼é£ä¹è§å¯è å¯è½ä¼å¨çå°é®é¢ä¹åå çå°çæ¡ã** è¦é²æ¢è¿ç§å¼å¸¸ï¼éè¦å¦ä¸ç§ç±»åçä¿è¯ï¼**ä¸è´åç¼è¯»ï¼consistent prefix readsï¼**ã23ããè¿ä¸ªä¿è¯çæææ¯è¯´ï¼å¦æä¸ç³»ååå ¥ææ个顺åºåçï¼é£ä¹ä»»ä½äººè¯»åè¿äºåå ¥æ¶ï¼ä¹ä¼çè§å®ä»¬ä»¥åæ ·ç顺åºåºç°ã è¿æ¯ **ååºï¼partitionedï¼** æ **åçï¼shardedï¼** æ°æ®åºä¸çä¸ä¸ªç¹æ®é®é¢ï¼æ们å°å¨ [第å ç« ](ch6.md) ä¸è®¨è®ºååºæ°æ®åºãå¦ææ°æ®åºæ»æ¯ä»¥ç¸åç顺åºåºç¨åå ¥ï¼è读åæ»æ¯çå°ä¸è´çåç¼ï¼é£ä¹è¿ç§å¼å¸¸ä¸ä¼åçãä½æ¯å¨è®¸å¤åå¸å¼æ°æ®åºä¸ï¼ä¸åçååºç¬ç«è¿è¡ï¼å æ¤ä¸åå¨ **å ¨å±çåå ¥é¡ºåº**ï¼å½ç¨æ·ä»æ°æ®åºä¸è¯»åæ°æ®æ¶ï¼å¯è½ä¼çå°æ°æ®åºçæäºé¨åå¤äºè¾æ§çç¶æï¼èæäºåå¤äºè¾æ°çç¶æã ä¸ç§è§£å³æ¹æ¡æ¯ï¼ç¡®ä¿ä»»ä½å æç¸å ³çåå ¥é½åå ¥ç¸åçååºï¼ä½å¨ä¸äºåºç¨ä¸å¯è½æ æ³é«æå°å®æè¿ç§æä½ãè¿æä¸äºæ¾å¼è·è¸ªå æä¾èµå ³ç³»çç®æ³ï¼æ们å°å¨ â[âæ¤ååçâ çå ³ç³»å并å](#âæ¤ååçâçå ³ç³»å并å)â ä¸èä¸åå°è¿ä¸ªè¯é¢ã ### å¤å¶å»¶è¿ç解å³æ¹æ¡ å¨ä½¿ç¨æç»ä¸è´çç³»ç»æ¶ï¼å¦æå¤å¶å»¶è¿å¢å å°å åéçè³å å°æ¶ï¼ååºè¯¥èèåºç¨ç¨åºçè¡ä¸ºãå¦æçæ¡æ¯ â没é®é¢âï¼é£å¾å¥½ãä½å¦æç»æ对äºç¨æ·æ¥è¯´æ¯ä¸å¥½çä½éªï¼é£ä¹è®¾è®¡ç³»ç»æ¥æä¾æ´å¼ºçä¿è¯ï¼ä¾å¦ **åå读**ï¼æ¯å¾éè¦çãæææ¯å¼æ¥å¤å¶å´å设å¤å¶æ¯åæ¥çï¼è¿æ¯å¾å¤éº»ç¦çæ ¹æºã å¦åæè¿°ï¼åºç¨ç¨åºå¯ä»¥æä¾æ¯åºå±æ°æ®åºæ´å¼ºæåçä¿è¯ï¼ä¾å¦éè¿ä¸»åºè¿è¡æç§è¯»åãä½å¨åºç¨ç¨åºä»£ç ä¸å¤çè¿äºé®é¢æ¯å¤æçï¼å®¹æåºéã å¦æåºç¨ç¨åºå¼å人åä¸å¿ æ å¿å¾®å¦çå¤å¶é®é¢ï¼å¹¶å¯ä»¥ä¿¡èµä»ä»¬çæ°æ®åº âåäºæ£ç¡®çäºæ âï¼é£è¯¥å¤å¥½åãè¿å°±æ¯ **äºå¡ï¼transactionï¼** åå¨çåå ï¼**æ°æ®åºéè¿äºå¡æä¾å¼ºå¤§çä¿è¯**ï¼æ以åºç¨ç¨åºå¯ä»¥æ´å ç®åã åèç¹äºå¡å·²ç»åå¨äºå¾é¿æ¶é´ãç¶èå¨èµ°ååå¸å¼ï¼å¤å¶åååºï¼æ°æ®åºæ¶ï¼è®¸å¤ç³»ç»æ¾å¼äºäºå¡ï¼å£°ç§°äºå¡å¨æ§è½åå¯ç¨æ§ä¸ç代价太é«ï¼å¹¶æè¨å¨å¯ä¼¸ç¼©ç³»ç»ä¸æç»ä¸è´æ§æ¯ä¸å¯é¿å çãè¿ä¸ªåè¿°æä¸äºéçï¼ä½è¿äºç®åäºï¼æ¬ä¹¦å ¶ä½é¨åå°æåºæ´ä¸ºç»è´çè§ç¹ãæ们å°å¨ [第ä¸ç« ](ch7.md) å [第ä¹ç« ](ch9.md) åå°äºå¡çè¯é¢ï¼å¹¶å°å¨ [第ä¸é¨å](part-iii.md) 讨论ä¸äºæ¿ä»£æºå¶ã ## å¤ä¸»å¤å¶ æ¬ç« å°ç®å为æ¢ï¼æ们åªèèäºä½¿ç¨å个主åºçå¤å¶æ¶æãè½ç¶è¿æ¯ä¸ç§å¸¸è§çæ¹æ³ï¼ä½è¿æå ¶å®ä¸äºæ趣çéæ©ã åºäºé¢å¯¼è çå¤å¶æä¸ä¸ªä¸»è¦ç缺ç¹ï¼åªæä¸ä¸ªä¸»åºï¼èä¸ææçåå ¥é½å¿ é¡»éè¿å® [^iv]ãå¦æåºäºä»»ä½åå ï¼ä¾å¦å主åºä¹é´çç½ç»è¿æ¥ä¸æï¼æ æ³è¿æ¥å°ä¸»åºï¼å°±æ æ³åæ°æ®åºåå ¥ã [^iv]: å¦ææ°æ®åºè¢«ååºï¼è§ [第å ç« ](ch6.md)ï¼ï¼æ¯ä¸ªååºé½æä¸ä¸ªä¸»åºãä¸åçååºç主åºå¯è½å¨ä¸åçèç¹ä¸ï¼ä½æ¯æ¯ä¸ªååºé½å¿ é¡»æä¸ä¸ªä¸»åºã åºäºé¢å¯¼è çå¤å¶æ¨¡åçèªç¶å»¶ä¼¸æ¯å 许å¤ä¸ªèç¹æ¥ååå ¥ãå¤å¶ä»ç¶ä»¥åæ ·çæ¹å¼åçï¼å¤çåå ¥çæ¯ä¸ªèç¹é½å¿ é¡»å°è¯¥æ°æ®åæ´è½¬åç»ææå ¶ä»èç¹ãæ们å°å ¶ç§°ä¹ä¸º **å¤é¢å¯¼è é ç½®**ï¼multi-leader configurationï¼ä¹ç§°å¤ä¸»ãå¤æ´»å¤å¶ï¼å³ master-master replication æ active/active replicationï¼ãå¨è¿ç§æ åµä¸ï¼æ¯ä¸ªä¸»åºåæ¶æ¯å ¶ä»ä¸»åºçä»åºã ### å¤ä¸»å¤å¶çåºç¨åºæ¯ å¨å个æ°æ®ä¸å¿å é¨ä½¿ç¨å¤ä¸ªä¸»åºçé 置没æ太大æä¹ï¼å ä¸ºå ¶å¯¼è´çå¤ææ§å·²ç»è¶ è¿äºè½å¸¦æ¥ç好å¤ãä½å¨ä¸äºæ åµä¸ï¼è¿ç§é ç½®ä¹æ¯åççã #### è¿ç»´å¤ä¸ªæ°æ®ä¸å¿ åå¦ä½ æä¸ä¸ªæ°æ®åºï¼å¯æ¬åæ£å¨å¥½å 个ä¸åçæ°æ®ä¸å¿ï¼å¯è½ä¼ç¨æ¥å®¹å¿å个æ°æ®ä¸å¿çæ éï¼æè 为äºå¨å°çä¸æ´æ¥è¿ç¨æ·ï¼ãå¦æ使ç¨å¸¸è§çåºäºé¢å¯¼è çå¤å¶è®¾ç½®ï¼ä¸»åºå¿ é¡»ä½äºå ¶ä¸ä¸ä¸ªæ°æ®ä¸å¿ï¼ä¸ææåå ¥é½å¿ é¡»ç»è¿è¯¥æ°æ®ä¸å¿ã å¤ä¸»é ç½®ä¸å¯ä»¥å¨æ¯ä¸ªæ°æ®ä¸å¿é½æ主åºã[å¾ 5-6](img/fig5-6.png) å±ç¤ºäºè¿ä¸ªæ¶æãå¨æ¯ä¸ªæ°æ®ä¸å¿å 使ç¨å¸¸è§ç主ä»å¤å¶ï¼å¨æ°æ®ä¸å¿ä¹é´ï¼æ¯ä¸ªæ°æ®ä¸å¿ç主åºé½ä¼å°å ¶æ´æ¹å¤å¶å°å ¶ä»æ°æ®ä¸å¿ç主åºä¸ã ![](img/fig5-6.png) **å¾ 5-6 è·¨å¤ä¸ªæ°æ®ä¸å¿çå¤ä¸»å¤å¶** æ们æ¥æ¯è¾ä¸ä¸å¨è¿ç»´å¤ä¸ªæ°æ®ä¸å¿æ¶ï¼å主åå¤ä¸»çéåºæ åµï¼ * æ§è½ å¨å主é ç½®ä¸ï¼æ¯ä¸ªåå ¥é½å¿ 须穿è¿äºèç½ï¼è¿å ¥ä¸»åºæå¨çæ°æ®ä¸å¿ãè¿å¯è½ä¼å¢å åå ¥æ¶é´ï¼å¹¶å¯è½è¿èäºè®¾ç½®å¤ä¸ªæ°æ®ä¸å¿çåå¿ãå¨å¤ä¸»é ç½®ä¸ï¼æ¯ä¸ªåæä½é½å¯ä»¥å¨æ¬å°æ°æ®ä¸å¿è¿è¡å¤çï¼å¹¶ä¸å ¶ä»æ°æ®ä¸å¿å¼æ¥å¤å¶ãå æ¤ï¼æ°æ®ä¸å¿ä¹é´çç½ç»å»¶è¿å¯¹ç¨æ·æ¥è¯´æ¯éæçï¼è¿æå³çæè§å°çæ§è½å¯è½ä¼æ´å¥½ã * 容å¿æ°æ®ä¸å¿åæº å¨å主é ç½®ä¸ï¼å¦æ主åºæå¨çæ°æ®ä¸å¿åçæ éï¼æ éåæ¢å¿ 须使å¦ä¸ä¸ªæ°æ®ä¸å¿éçä»åºæ为主åºãå¨å¤ä¸»é ç½®ä¸ï¼æ¯ä¸ªæ°æ®ä¸å¿å¯ä»¥ç¬ç«äºå ¶ä»æ°æ®ä¸å¿ç»§ç»è¿è¡ï¼å¹¶ä¸å½åçæ éçæ°æ®ä¸å¿å½éæ¶ï¼å¤å¶ä¼èªå¨èµ¶ä¸ã * 容å¿ç½ç»é®é¢ æ°æ®ä¸å¿ä¹é´çéä¿¡é常穿è¿å ¬å ±äºèç½ï¼è¿å¯è½ä¸å¦æ°æ®ä¸å¿å çæ¬å°ç½ç»å¯é ãå主é 置对æ°æ®ä¸å¿ä¹é´çè¿æ¥é®é¢é常ææï¼å 为éè¿è¿ä¸ªè¿æ¥è¿è¡çåæä½æ¯åæ¥çãéç¨å¼æ¥å¤å¶åè½çå¤ä¸»é ç½®é常è½æ´å¥½å°æ¿åç½ç»é®é¢ï¼ä¸´æ¶çç½ç»ä¸æ并ä¸ä¼å¦¨ç¢æ£å¨å¤ççåå ¥ã æäºæ°æ®åºé»è®¤æ åµä¸æ¯æå¤ä¸»é ç½®ï¼ä½ä½¿ç¨å¤é¨å·¥å ·å®ç°ä¹å¾å¸¸è§ï¼ä¾å¦ç¨äº MySQL ç Tungsten Replicator ã26ãï¼ç¨äº PostgreSQL ç BDRã27ã以åç¨äº Oracle ç GoldenGate ã19ãã 尽管å¤ä¸»å¤å¶æè¿äºä¼å¿ï¼ä½ä¹æä¸ä¸ªå¾å¤§ç缺ç¹ï¼ä¸¤ä¸ªä¸åçæ°æ®ä¸å¿å¯è½ä¼åæ¶ä¿®æ¹ç¸åçæ°æ®ï¼åå²çªæ¯å¿ 须解å³çï¼å¦ [å¾ 5-6](img/fig5-6.png) ä¸ç âå²çªè§£å³ï¼conflict resolutionï¼âï¼ãæ¬ä¹¦å°å¨ â[å¤çåå ¥å²çª](#å¤çåå ¥å²çª)â ä¸è¯¦ç»è®¨è®ºè¿ä¸ªé®é¢ã ç±äºå¤ä¸»å¤å¶å¨è®¸å¤æ°æ®åºä¸é½å±äºæ¹è£ çåè½ï¼æ以常常åå¨å¾®å¦çé 置缺é·ï¼ä¸ç»å¸¸ä¸å ¶ä»æ°æ®åºåè½ä¹é´åºç°æå¤çååºãæ¯å¦èªå¢ä¸»é®ã触åå¨ãå®æ´æ§çº¦æçé½å¯è½ä¼æ麻ç¦ãå æ¤ï¼å¤ä¸»å¤å¶å¾å¾è¢«è®¤ä¸ºæ¯å±é©çé¢åï¼åºå°½å¯è½é¿å ã28ãã #### éè¦ç¦»çº¿æä½ç客æ·ç«¯ å¤ä¸»å¤å¶çå¦ä¸ç§éç¨åºæ¯æ¯ï¼åºç¨ç¨åºå¨æç½ä¹åä»ç¶éè¦ç»§ç»å·¥ä½ã ä¾å¦ï¼èèææºï¼ç¬è®°æ¬çµèåå ¶ä»è®¾å¤ä¸çæ¥ååºç¨ãæ 论设å¤ç®åæ¯å¦æäºèç½è¿æ¥ï¼ä½ éè¦è½éæ¶æ¥çä½ çä¼è®®ï¼ååºè¯»å请æ±ï¼ï¼è¾å ¥æ°çä¼è®®ï¼ååºåå ¥è¯·æ±ï¼ãå¦æå¨ç¦»çº¿ç¶æä¸è¿è¡ä»»ä½æ´æ¹ï¼å设å¤ä¸æ¬¡ä¸çº¿æ¶ï¼éè¦ä¸æå¡å¨åå ¶ä»è®¾å¤åæ¥ã å¨è¿ç§æ åµä¸ï¼æ¯ä¸ªè®¾å¤é½æä¸ä¸ªå å½ä¸»åºçæ¬å°æ°æ®åºï¼å®æ¥åå请æ±ï¼ï¼å¹¶ä¸å¨ææ设å¤ä¸çæ¥åå¯æ¬ä¹é´åæ¥æ¶ï¼åå¨å¼æ¥çå¤ä¸»å¤å¶è¿ç¨ãå¤å¶å»¶è¿å¯è½æ¯å å°æ¶çè³å 天ï¼å ·ä½åå³äºä½æ¶å¯ä»¥è®¿é®äºèç½ã ä»æ¶æçè§åº¦æ¥çï¼è¿ç§è®¾ç½®å®é ä¸ä¸æ°æ®ä¸å¿ä¹é´çå¤ä¸»å¤å¶ç±»ä¼¼ï¼æ¯ä¸ªè®¾å¤é½æ¯ä¸ä¸ª âæ°æ®ä¸å¿âï¼èå®ä»¬ä¹é´çç½ç»è¿æ¥æ¯æ度ä¸å¯é çãä»åå²ä¸åç±»æ¥ååæ¥åè½çç ´çå®ç°å¯ä»¥çåºï¼æ³æå¤ä¸»å¤å¶ç¨å¥½æ¯å¤ä¹å°é¾çä¸ä»¶äºã æä¸äºå·¥å ·æ¨å¨ä½¿è¿ç§å¤ä¸»é ç½®æ´å®¹æãä¾å¦ï¼CouchDB å°±æ¯ä¸ºè¿ç§æä½æ¨¡å¼è设计çã29ãã #### ååç¼è¾ å®æ¶åä½ç¼è¾åºç¨ç¨åºå 许å¤ä¸ªäººåæ¶ç¼è¾ææ¡£ãä¾å¦ï¼Etherpad ã30ãå Google Docs ã31ãå 许å¤äººåæ¶ç¼è¾ææ¬ææ¡£æçµåè¡¨æ ¼ï¼è¯¥ç®æ³å¨ â[èªå¨å²çªè§£å³](#èªå¨å²çªè§£å³)â ä¸ç®è¦è®¨è®ºï¼ãæ们é常ä¸ä¼å°åä½å¼ç¼è¾è§ä¸ºæ°æ®åºå¤å¶é®é¢ï¼ä½å®ä¸åé¢æå°ç离线ç¼è¾ç¨ä¾æ许å¤ç¸ä¼¼ä¹å¤ãå½ä¸ä¸ªç¨æ·ç¼è¾ææ¡£æ¶ï¼æåçæ´æ¹å°ç«å³åºç¨å°å ¶æ¬å°å¯æ¬ï¼Web æµè§å¨æ客æ·ç«¯åºç¨ç¨åºä¸çææ¡£ç¶æï¼ï¼å¹¶å¼æ¥å¤å¶å°æå¡å¨åç¼è¾åä¸ææ¡£çä»»ä½å ¶ä»ç¨æ·ã å¦æè¦ä¿è¯ä¸ä¼åçç¼è¾å²çªï¼ååºç¨ç¨åºå¿ é¡»å åå¾ææ¡£çéå®ï¼ç¶åç¨æ·æè½å¯¹å ¶è¿è¡ç¼è¾ãå¦æå¦ä¸ä¸ªç¨æ·æ³è¦ç¼è¾åä¸ä¸ªææ¡£ï¼ä»ä»¬é¦å å¿ é¡»çå°ç¬¬ä¸ä¸ªç¨æ·æ交修æ¹å¹¶éæ¾éå®ãè¿ç§åä½æ¨¡å¼ç¸å½äºä¸»ä»å¤å¶æ¨¡åä¸å¨ä¸»èç¹ä¸æ§è¡äºå¡æä½ã ä½æ¯ï¼ä¸ºäºå éåä½ï¼ä½ å¯è½å¸æå°æ´æ¹çåä½è®¾ç½®å¾é常å°ï¼ä¾å¦å次æé®ï¼ï¼å¹¶é¿å éå®ãè¿ç§æ¹æ³å 许å¤ä¸ªç¨æ·åæ¶è¿è¡ç¼è¾ï¼ä½åæ¶ä¹å¸¦æ¥äºå¤ä¸»å¤å¶çææææï¼å æ¬éè¦è§£å³å²çªã32ãã ### å¤çåå ¥å²çª å¤ä¸»å¤å¶çæ大é®é¢æ¯å¯è½åçåå²çªï¼è¿æå³çéè¦è§£å³å²çªã ä¾å¦ï¼èèä¸ä¸ªç±ä¸¤ä¸ªç¨æ·åæ¶ç¼è¾çç»´åºé¡µé¢ï¼å¦ [å¾ 5-7](img/fig5-7.png) æ示ãç¨æ· 1 å°é¡µé¢çæ é¢ä» A æ´æ¹ä¸º Bï¼å¹¶ä¸ç¨æ· 2 åæ¶å°æ é¢ä» A æ´æ¹ä¸º Cãæ¯ä¸ªç¨æ·çæ´æ¹å·²æååºç¨å°å ¶æ¬å°ä¸»åºãä½å½å¼æ¥å¤å¶æ¶ï¼ä¼åç°å²çªã33ããå主æ°æ®åºä¸ä¸ä¼åºç°æ¤é®é¢ã ![](img/fig5-7.png) **å¾ 5-7 两个主åºåæ¶æ´æ°åä¸è®°å½å¼èµ·çåå ¥å²çª** #### åæ¥ä¸å¼æ¥å²çªæ£æµ å¨å主æ°æ®åºä¸ï¼ç¬¬äºä¸ªåå ¥å°è¢«é»å¡å¹¶çå¾ ç¬¬ä¸ä¸ªåå ¥å®æï¼æè ä¸æ¢ç¬¬äºä¸ªåå ¥äºå¡å¹¶å¼ºå¶ç¨æ·éè¯ãå¦ä¸æ¹é¢ï¼å¨å¤ä¸»é ç½®ä¸ï¼ä¸¤ä¸ªåå ¥é½æ¯æåçï¼å¨ç¨åçæ个æ¶é´ç¹æè½å¼æ¥å°æ£æµå°å²çªãé£æ¶åæ¥è¦æ±ç¨æ·è§£å³å²çªå¯è½ä¸ºæ¶å·²æã ååä¸ï¼å¯ä»¥ä½¿å²çªæ£æµåæ¥ - å³çå¾ åå ¥è¢«å¤å¶å°ææå¯æ¬ï¼ç¶åååè¯ç¨æ·åå ¥æåãä½æ¯ï¼éè¿è¿æ ·åï¼ä½ å°å¤±å»å¤ä¸»å¤å¶ç主è¦ä¼ç¹ï¼å 许æ¯ä¸ªå¯æ¬ç¬ç«å°æ¥ååå ¥ãå¦æä½ æ³è¦åæ¥å²çªæ£æµï¼é£ä¹ä½ å¯è½ä¸å¦ç´æ¥ä½¿ç¨å主å¤å¶ã #### é¿å å²çª å¤çå²çªçæç®åççç¥å°±æ¯é¿å å®ä»¬ï¼å¦æåºç¨ç¨åºå¯ä»¥ç¡®ä¿ç¹å®è®°å½çææåå ¥é½éè¿åä¸ä¸ªä¸»åºï¼é£ä¹å²çªå°±ä¸ä¼åçãç±äºè®¸å¤çå¤ä¸»å¤å¶å®ç°å¨å¤çå²çªæ¶å¤çå¾ç¸å½ä¸å¥½ï¼é¿å å²çªæ¯ä¸ä¸ªç»å¸¸è¢«æ¨èçæ¹æ³ã34ãã ä¾å¦ï¼å¨ä¸ä¸ªç¨æ·å¯ä»¥ç¼è¾èªå·±æ°æ®çåºç¨ç¨åºä¸ï¼å¯ä»¥ç¡®ä¿æ¥èªç¹å®ç¨æ·ç请æ±å§ç»è·¯ç±å°åä¸æ°æ®ä¸å¿ï¼å¹¶ä½¿ç¨è¯¥æ°æ®ä¸å¿ç主åºè¿è¡è¯»åãä¸åçç¨æ·å¯è½æä¸åç â主â æ°æ®ä¸å¿ï¼å¯è½æ ¹æ®ç¨æ·çå°çä½ç½®éæ©ï¼ï¼ä½ä»ä»»ä½ä¸ä½ç¨æ·çè§åº¦æ¥çï¼æ¬è´¨ä¸å°±æ¯å主é ç½®äºã ä½æ¯ï¼ææ¶ä½ å¯è½éè¦æ´æ¹è¢«æå®çä¸»åº ââ å¯è½æ¯å 为æ个æ°æ®ä¸å¿åºç°æ éï¼ä½ éè¦å°æµééæ°è·¯ç±å°å¦ä¸ä¸ªæ°æ®ä¸å¿ï¼æè å¯è½æ¯å 为ç¨æ·å·²ç»è¿ç§»å°å¦ä¸ä¸ªä½ç½®ï¼ç°å¨æ´æ¥è¿å ¶å®çæ°æ®ä¸å¿ãå¨è¿ç§æ åµä¸ï¼å²çªé¿å å°å¤±æï¼ä½ å¿ é¡»å¤çä¸å主åºåæ¶åå ¥çå¯è½æ§ã #### æ¶æè³ä¸è´çç¶æ å主æ°æ®åºæ顺åºè¿è¡åæä½ï¼å¦æåä¸ä¸ªå段æå¤ä¸ªæ´æ°ï¼åæåä¸ä¸ªåæä½å°å³å®è¯¥å段çæç»å¼ã å¨å¤ä¸»é ç½®ä¸ï¼æ²¡ææç¡®çåå ¥é¡ºåºï¼æ以æç»å¼åºè¯¥æ¯ä»ä¹å¹¶ä¸æ¸ æ¥ãå¨ [å¾ 5-7](img/fig5-7.png) ä¸ï¼å¨ä¸»åº 1 ä¸æ é¢é¦å æ´æ°ä¸º B èåæ´æ°ä¸º Cï¼å¨ä¸»åº 2 ä¸ï¼é¦å æ´æ°ä¸º Cï¼ç¶åæ´æ°ä¸º Bã两ç§é¡ºåºé½ä¸æ¯å¦ä¸ç§âæ´æ£ç¡®âã å¦ææ¯ä¸ªå¯æ¬åªæ¯æç §å®çå°åå ¥ç顺åºåå ¥ï¼é£ä¹æ°æ®åºæç»å°å¤äºä¸ä¸è´çç¶æï¼æç»å¼å°æ¯å¨ä¸»åº 1 ç C åä¸»åº 2 ç Bãè¿æ¯ä¸å¯æ¥åçï¼æ¯ä¸ªå¤å¶æ¹æ¡é½å¿ 须确ä¿æ°æ®æç»å¨ææå¯æ¬ä¸é½æ¯ç¸åçãå æ¤ï¼æ°æ®åºå¿ 须以ä¸ç§ **æ¶æï¼convergentï¼** çæ¹å¼è§£å³å²çªï¼è¿æå³çææå¯æ¬å¿ é¡»å¨ææåæ´å¤å¶å®ææ¶æ¶æè³ä¸ä¸ªç¸åçæç»å¼ã å®ç°å²çªå并解å³æå¤ç§éå¾ï¼ * ç»æ¯ä¸ªåå ¥ä¸ä¸ªå¯ä¸ç IDï¼ä¾å¦æ¶é´æ³ãé¿éæºæ°ãUUID æè é®åå¼çåå¸ï¼ï¼æéæé« ID çåå ¥ä½ä¸ºèå©è ï¼å¹¶ä¸¢å¼å ¶ä»åå ¥ãå¦æ使ç¨æ¶é´æ³ï¼è¿ç§ææ¯è¢«ç§°ä¸º **æååå ¥èå©ï¼LWW, last write winsï¼**ãè½ç¶è¿ç§æ¹æ³å¾æµè¡ï¼ä½æ¯å¾å®¹æé ææ°æ®ä¸¢å¤±ã35ããæ们å°å¨æ¬ç« æ«å°¾ç [æ£æµå¹¶ååå ¥](#æ£æµå¹¶ååå ¥) ä¸èæ´è¯¦ç»å°è®¨è®º LWWã * 为æ¯ä¸ªå¯æ¬åé ä¸ä¸ªå¯ä¸ç IDï¼ID ç¼å·æ´é«çåå ¥å ·ææ´é«çä¼å 级ãè¿ç§æ¹æ³ä¹æå³çæ°æ®ä¸¢å¤±ã * 以æç§æ¹å¼å°è¿äºå¼å并å¨ä¸èµ· - ä¾å¦ï¼æåæ¯é¡ºåºæåºï¼ç¶åè¿æ¥å®ä»¬ï¼å¨ [å¾ 5-7](img/fig5-7.png) ä¸ï¼å并çæ é¢å¯è½ç±»ä¼¼äº âB/Câï¼ã * ç¨ä¸ç§å¯ä¿çææä¿¡æ¯çæ¾å¼æ°æ®ç»ææ¥è®°å½å²çªï¼å¹¶ç¼å解å³å²çªçåºç¨ç¨åºä»£ç ï¼ä¹è®¸éè¿æ示ç¨æ·çæ¹å¼ï¼ã #### èªå®ä¹å²çªè§£å³é»è¾ 解å³å²çªçæåéçæ¹æ³å¯è½åå³äºåºç¨ç¨åºï¼å¤§å¤æ°å¤ä¸»å¤å¶å·¥å ·å 许使ç¨åºç¨ç¨åºä»£ç ç¼åå²çªè§£å³é»è¾ã该代ç å¯ä»¥å¨åå ¥æ读åæ¶æ§è¡ï¼ * åæ¶æ§è¡ åªè¦æ°æ®åºç³»ç»æ£æµå°å¤å¶æ´æ¹æ¥å¿ä¸åå¨å²çªï¼å°±ä¼è°ç¨å²çªå¤çç¨åºãä¾å¦ï¼Bucardo å è®¸ä½ ä¸ºæ¤ç¼åä¸æ®µ Perl 代ç ãè¿ä¸ªå¤çç¨åºé常ä¸è½æ示ç¨æ· ââ å®å¨åå°è¿ç¨ä¸è¿è¡ï¼å¹¶ä¸å¿ 须快éæ§è¡ã * 读æ¶æ§è¡ å½æ£æµå°å²çªæ¶ï¼ææå²çªåå ¥è¢«åå¨ãä¸ä¸æ¬¡è¯»åæ°æ®æ¶ï¼ä¼å°è¿äºå¤ä¸ªçæ¬çæ°æ®è¿åç»åºç¨ç¨åºãåºç¨ç¨åºå¯ä»¥æ示ç¨æ·æèªå¨è§£å³å²çªï¼å¹¶å°ç»æååæ°æ®åºãä¾å¦ CouchDB 就以è¿ç§æ¹å¼å·¥ä½ã 请注æï¼å²çªè§£å³é常éç¨äºåè¡è®°å½æå个ææ¡£çå±é¢ï¼èä¸æ¯æ´ä¸ªäºå¡ã36ããå æ¤ï¼å¦æä½ æä¸ä¸ªäºå¡ä¼ååæ§å°è¿è¡å 次ä¸åçåå ¥ï¼è¯·åé [第ä¸ç« ](ch7.md)ï¼ï¼å¯¹äºå²çªè§£å³èè¨ï¼æ¯ä¸ªåå ¥ä»éåå¼åç¬èèã > #### èªå¨å²çªè§£å³ > > å²çªè§£å³è§åå¯è½å¾å®¹æåå¾è¶æ¥è¶å¤æï¼èªå®ä¹ä»£ç å¯è½ä¹å¾å®¹æåºéãäºé©¬éæ¯ä¸ä¸ªç»å¸¸è¢«å¼ç¨çä¾åï¼ç±äºå²çªè§£å³å¤çç¨åºè产çäºä»¤äººæå¤çææï¼ä¸æ®µæ¶é´ä»¥æ¥ï¼è´ç©è½¦ä¸çå²çªè§£å³é»è¾å°ä¿çæ·»å å°è´ç©è½¦çç©åï¼ä½ä¸å æ¬ä»è´ç©è½¦ä¸ç§»é¤çç©åãå æ¤ï¼é¡¾å®¢ææ¶ä¼çå°ç©åéæ°åºç°å¨ä»ä»¬çè´ç©è½¦ä¸ï¼å³ä½¿ä»ä»¬ä¹åå·²ç»è¢«ç§»èµ°ã37ãã > > å·²ç»æä¸äºæ趣çç 究æ¥èªå¨è§£å³ç±äºæ°æ®ä¿®æ¹å¼èµ·çå²çªãæå 项ç 究å¼å¾ä¸æï¼ > > * **æ å²çªå¤å¶æ°æ®ç±»åï¼Conflict-free replicated datatypesï¼CRDTï¼**ã32,38ãæ¯å¯ä»¥ç±å¤ä¸ªç¨æ·åæ¶ç¼è¾çéåãæ å°ãæåºå表ã计æ°å¨çä¸ç³»åæ°æ®ç»æï¼å®ä»¬ä»¥åççæ¹å¼èªå¨è§£å³å²çªãä¸äº CRDT å·²ç»å¨ Riak 2.0 ä¸å®ç°ã39,40ãã > * **å¯å并çæä¹ æ°æ®ç»æï¼Mergeable persistent data structuresï¼**ã41ãæ¾å¼è·è¸ªåå²è®°å½ï¼ç±»ä¼¼äº Git çæ¬æ§å¶ç³»ç»ï¼å¹¶ä½¿ç¨ä¸åå并åè½ï¼è CRDT 使ç¨ååå并ï¼ã > * **æä½è½¬æ¢ï¼operational transformationï¼**[42] æ¯ Etherpad ã30ãå Google Docs ã31ãçååç¼è¾åºç¨èåçå²çªè§£å³ç®æ³ãå®æ¯ä¸ä¸ºæåºå表ç并åç¼è¾è设计çï¼ä¾å¦ææææ¬ææ¡£çå符å表ã > > è¿äºç®æ³å¨æ°æ®åºä¸çå®ç°è¿å¾å¹´è½»ï¼ä½å¾å¯è½å°æ¥å®ä»¬ä¼è¢«éæå°æ´å¤çå¤å¶æ°æ®ç³»ç»ä¸ãèªå¨å²çªè§£å³æ¹æ¡å¯ä»¥ä½¿åºç¨ç¨åºå¤çå¤ä¸»æ°æ®åæ¥æ´ä¸ºç®åã #### ä»ä¹æ¯å²çªï¼ æäºå²çªæ¯æ¾èæè§çãå¨ [å¾ 5-7](img/fig5-7.png) çä¾åä¸ï¼ä¸¤ä¸ªåæä½å¹¶åå°ä¿®æ¹äºåä¸æ¡è®°å½ä¸çåä¸ä¸ªå段ï¼å¹¶å°å ¶è®¾ç½®ä¸ºä¸¤ä¸ªä¸åçå¼ã毫æ çé®è¿æ¯ä¸ä¸ªå²çªã å ¶ä»ç±»åçå²çªå¯è½æ´ä¸ºå¾®å¦èé¾ä»¥åç°ãä¾å¦ï¼èèä¸ä¸ªä¼è®®å®¤é¢è®¢ç³»ç»ï¼å®è®°å½è°è®¢äºåªä¸ªæ¶é´æ®µçåªä¸ªæ¿é´ãåºç¨ç¨åºéè¦ç¡®ä¿æ¯ä¸ªæ¿é´å¨ä»»ææ¶å»é½åªè½è¢«ä¸ç»äººè¿è¡é¢å®ï¼å³ä¸å¾æç¸åæ¿é´çéå é¢è®¢ï¼ãå¨è¿ç§æ åµä¸ï¼å¦æ为åä¸ä¸ªæ¿é´åæ¶å建两个ä¸åçé¢è®¢ï¼åå¯è½ä¼åçå²çªãå³ä½¿åºç¨ç¨åºå¨å 许ç¨æ·è¿è¡é¢è®¢ä¹åå æ£æ¥ä¼è®®å®¤çå¯ç¨æ§ï¼å¦æ两次é¢è®¢æ¯ç±ä¸¤ä¸ªä¸åç主åºè¿è¡çï¼åä»ç¶å¯è½ä¼æå²çªã è½ç¶ç°å¨è¿æ²¡æä¸ä¸ªç°æççæ¡ï¼ä½å¨æ¥ä¸æ¥çç« èä¸ï¼æ们å°æ´å¥½å°äºè§£è¿ä¸ªé®é¢ãæ们å°å¨ [第ä¸ç« ](ch7.md) ä¸çå°æ´å¤çå²çªç¤ºä¾ï¼å¨ [第åäºç« ](ch12.md) ä¸æ们å°è®¨è®ºç¨äºæ£æµå解å³å¤å¶ç³»ç»ä¸å²çªçå¯ä¼¸ç¼©æ¹æ³ã ### å¤ä¸»å¤å¶ææ **å¤å¶ææ**ï¼replication topologyï¼ç¨æ¥æè¿°åå ¥æä½ä»ä¸ä¸ªèç¹ä¼ æå°å¦ä¸ä¸ªèç¹çéä¿¡è·¯å¾ãå¦æä½ æ两个主åºï¼å¦ [å¾ 5-7](img/fig5-7.png) æ示ï¼åªæä¸ä¸ªåççææç»æï¼ä¸»åº 1 å¿ é¡»æå®ææçåå ¥é½åéå°ä¸»åº 2ï¼åä¹äº¦ç¶ãå½æ两个以ä¸ç主åºï¼å¤ç§ä¸åçææé½æ¯å¯è½çã[å¾ 5-8](img/fig5-8.png) 举ä¾è¯´æäºä¸äºä¾åã ![](img/fig5-8.png) **å¾ 5-8 ä¸ç§å¯ä»¥å¨å¤ä¸»å¤å¶ä¸ä½¿ç¨çææ示ä¾ã** æ常è§çæææ¯å ¨é¨å°å ¨é¨ï¼all-to-allï¼å¦ [å¾ 5-8 (c)](img/fig5-8.png)ï¼ï¼å ¶ä¸æ¯ä¸ªä¸»åºé½å°å ¶åå ¥åéç»å ¶ä»ææç主åºãç¶èï¼ä¸äºæ´åéçææä¹ä¼è¢«ä½¿ç¨å°ï¼ä¾å¦ï¼é»è®¤æ åµä¸ MySQL ä» æ¯æ **ç¯å½¢ææï¼circular topologyï¼**ã34ãï¼å ¶ä¸æ¯ä¸ªèç¹é½ä»ä¸ä¸ªèç¹æ¥æ¶åå ¥ï¼å¹¶å°è¿äºåå ¥ï¼å ä¸èªå·±çåå ¥ï¼è½¬åç»å¦ä¸ä¸ªèç¹ãå¦ä¸ç§æµè¡çææç»æå ·ææå½¢çå½¢ç¶ [^v]ï¼ä¸ä¸ªæå®çæ ¹èç¹å°åå ¥è½¬åç»ææå ¶ä»èç¹ãæå½¢ææå¯ä»¥æ¨å¹¿å°æ ã [^v]: ä¸è¦ä¸æå模å¼æ··æ·ï¼è¯·åé â[æååéªè±åï¼åæç模å¼](ch3.md#æååéªè±åï¼åæç模å¼)âï¼ï¼å ¶ä¸æè¿°äºæ°æ®æ¨¡åçç»æï¼èä¸æ¯èç¹ä¹é´çéä¿¡ææã å¨ç¯å½¢åæå½¢ææä¸ï¼åå ¥å¯è½éè¦å¨å°è¾¾ææå¯æ¬ä¹åéè¿å¤ä¸ªèç¹ãå æ¤ï¼èç¹éè¦è½¬åä»å ¶ä»èç¹æ¶å°çæ°æ®æ´æ¹ã为äºé²æ¢æ éå¤å¶å¾ªç¯ï¼æ¯ä¸ªèç¹è¢«èµäºä¸ä¸ªå¯ä¸çæ è¯ç¬¦ï¼å¹¶ä¸å¨å¤å¶æ¥å¿ä¸ï¼æ¯æ¬¡åå ¥é½ä¼ä½¿ç¨å ¶ç»è¿çææèç¹çæ è¯ç¬¦è¿è¡æ è®°ã43ããå½ä¸ä¸ªèç¹æ¶å°ç¨èªå·±çæ è¯ç¬¦æ è®°çæ°æ®æ´æ¹æ¶ï¼è¯¥æ°æ®æ´æ¹å°è¢«å¿½ç¥ï¼å 为èç¹ç¥éå®å·²ç»è¢«å¤çè¿ã ç¯å½¢åæå½¢ææçé®é¢æ¯ï¼å¦æåªæä¸ä¸ªèç¹åçæ éï¼åå¯è½ä¼ä¸æå ¶ä»èç¹ä¹é´çå¤å¶æ¶æ¯æµï¼å¯¼è´å®ä»¬æ æ³éä¿¡ï¼é¤éèç¹è¢«ä¿®å¤ãææç»æå¯ä»¥éæ°é 置为跳è¿åçæ éçèç¹ï¼ä½å¨å¤§å¤æ°é¨ç½²ä¸ï¼è¿ç§éæ°é ç½®å¿ é¡»æå¨å®æãæ´å¯éè¿æ¥çææç»æï¼ä¾å¦å ¨é¨å°å ¨é¨ï¼ç容éæ§æ´å¥½ï¼å 为å®å 许æ¶æ¯æ²¿çä¸åçè·¯å¾ä¼ æï¼å¯ä»¥é¿å åç¹æ éã å¦ä¸æ¹é¢ï¼å ¨é¨å°å ¨é¨çææä¹å¯è½æé®é¢ãç¹å«æ¯ï¼ä¸äºç½ç»é¾æ¥å¯è½æ¯å ¶ä»ç½ç»é¾æ¥æ´å¿«ï¼ä¾å¦ç±äºç½ç»æ¥å¡ï¼ï¼ç»ææ¯ä¸äºå¤å¶æ¶æ¯å¯è½ âè¶ è¶â å ¶ä»å¤å¶æ¶æ¯ï¼å¦ [å¾ 5-9](img/fig5-9.png) æ示ã ![](img/fig5-9.png) **å¾ 5-9 使ç¨å¤ä¸»å¤å¶æ¶ï¼åå ¥å¯è½ä¼ä»¥é误ç顺åºå°è¾¾æäºå¯æ¬ã** å¨ [å¾ 5-9](img/fig5-9.png) ä¸ï¼å®¢æ·ç«¯ A åä¸»åº 1 ç表ä¸æå ¥ä¸è¡ï¼å®¢æ·ç«¯ B å¨ä¸»åº 3 ä¸æ´æ°è¯¥è¡ãç¶èï¼ä¸»åº 2 å¯ä»¥ä»¥ä¸åç顺åºæ¥æ¶åå ¥ï¼å®å¯è½å æ¥æ¶å°æ´æ°ï¼ä»å®çè§åº¦æ¥çï¼æ¯å¯¹æ°æ®åºä¸ä¸åå¨çè¡çæ´æ°ï¼ï¼ç¨åææ¥æ¶å°ç¸åºçæå ¥ï¼å ¶åºè¯¥å¨æ´æ°ä¹åï¼ã è¿æ¯ä¸ä¸ªå æå ³ç³»çé®é¢ï¼ç±»ä¼¼äºæä»¬å¨ â[ä¸è´åç¼è¯»](#ä¸è´åç¼è¯»)â ä¸çå°çï¼æ´æ°åå³äºå åçæå ¥ï¼æ以æ们éè¦ç¡®ä¿ææèç¹å å¤çæå ¥ï¼ç¶ååå¤çæ´æ°ãä» ä» å¨æ¯ä¸æ¬¡åå ¥æ¶æ·»å ä¸ä¸ªæ¶é´æ³æ¯ä¸å¤çï¼å 为æ¶éä¸å¯è½è¢«å åå°åæ¥ï¼æä»¥ä¸»åº 2 å°±æ æ³æ£ç¡®å°å¯¹è¿äºäºä»¶è¿è¡æåºï¼è§ [ç¬¬å «ç« ](ch8.md)ï¼ã è¦æ£ç¡®æåºè¿äºäºä»¶ï¼å¯ä»¥ä½¿ç¨ä¸ç§ç§°ä¸º **çæ¬åéï¼version vectorsï¼** çææ¯ï¼æ¬ç« ç¨åå°è®¨è®ºè¿ç§ææ¯ï¼è¯·åé â[æ£æµå¹¶ååå ¥](#æ£æµå¹¶ååå ¥)âï¼ãç¶èï¼è®¸å¤å¤ä¸»å¤å¶ç³»ç»ä¸çå²çªæ£æµææ¯å®ç°å¾å¹¶ä¸å¥½ãä¾å¦ï¼å¨æ°åæ¬ææ¶ï¼PostgreSQL BDR ä¸æä¾åå ¥çå ææåºã27ãï¼è Tungsten Replicator for MySQL çè³é½ä¸åæ£æµå²çªã34ãã å¦æä½ æ£å¨ä½¿ç¨åºäºå¤ä¸»å¤å¶çç³»ç»ï¼é£ä¹ä½ åºè¯¥å¤äºè§£è¿äºé®é¢ï¼ä»ç»é 读ææ¡£ï¼å¹¶å½»åºæµè¯ä½ çæ°æ®åºï¼ä»¥ç¡®ä¿å®ç¡®å®æä¾äºä½ æ³è¦çä¿è¯ã ## æ 主å¤å¶ æ们å¨æ¬ç« å°ç®å为æ¢æ讨论çå¤å¶æ¹æ³ ââ å主å¤å¶ãå¤ä¸»å¤å¶ ââ é½æ¯è¿æ ·çæ³æ³ï¼å®¢æ·ç«¯åä¸ä¸ªä¸»åºåéå请æ±ï¼èæ°æ®åºç³»ç»è´è´£å°åå ¥å¤å¶å°å ¶ä»å¯æ¬ã主åºå³å®åå ¥ç顺åºï¼èä»åºæç¸å顺åºåºç¨ä¸»åºçåå ¥ã ä¸äºæ°æ®åå¨ç³»ç»éç¨ä¸åçæ¹æ³ï¼æ¾å¼ä¸»åºçæ¦å¿µï¼å¹¶å 许任ä½å¯æ¬ç´æ¥æ¥åæ¥èªå®¢æ·ç«¯çåå ¥ãææ©çä¸äºçå¤å¶æ°æ®ç³»ç»æ¯ **æ 主çï¼leaderlessï¼**ã1,44ãï¼ä½æ¯å¨å ³ç³»æ°æ®åºä¸»å¯¼çæ¶ä»£ï¼è¿ä¸ªæ³æ³å ä¹å·²è¢«å¿å´ãå¨äºé©¬éå°å ¶ç¨äºå ¶å é¨ç Dynamo ç³»ç» [^vi] ä¹åï¼å®åä¸æ¬¡æ为æ°æ®åºçä¸ç§æ¶å°æ¶æã37ããRiakï¼Cassandra å Voldemort æ¯å Dynamo å¯åçæ 主å¤å¶æ¨¡åçå¼æºæ°æ®åå¨ï¼æ以è¿ç±»æ°æ®åºä¹è¢«ç§°ä¸º *Dynamo é£æ ¼*ã [^vi]: Dynamo ä¸éç¨äº Amazon 以å¤çç¨æ·ã令人å°æçæ¯ï¼AWS æä¾äºä¸ä¸ªå为 DynamoDB çæ管æ°æ®åºäº§åï¼å®ä½¿ç¨äºå®å ¨ä¸åçä½ç³»ç»æï¼å®åºäºå主å¤å¶ã å¨ä¸äºæ 主å¤å¶çå®ç°ä¸ï¼å®¢æ·ç«¯ç´æ¥å°åå ¥åéå°å 个å¯æ¬ä¸ï¼èå¦ä¸äºæ åµä¸ï¼ç±ä¸ä¸ª **åè°è ï¼coordinatorï¼** èç¹ä»£è¡¨å®¢æ·ç«¯è¿è¡åå ¥ãä½ä¸ä¸»åºæ°æ®åºä¸åï¼åè°è ä¸æ§è¡ç¹å®çåå ¥é¡ºåºãæ们å°ä¼çå°ï¼è¿ç§è®¾è®¡ä¸çå·®å¼å¯¹æ°æ®åºç使ç¨æ¹å¼æçæ·±è¿çå½±åã ### å½èç¹æ éæ¶åå ¥æ°æ®åº åè®¾ä½ æä¸ä¸ªå¸¦æä¸ä¸ªå¯æ¬çæ°æ®åºï¼èå ¶ä¸ä¸ä¸ªå¯æ¬ç®åä¸å¯ç¨ï¼æ许æ£å¨éæ°å¯å¨ä»¥å®è£ ç³»ç»æ´æ°ãå¨åºäºé¢å¯¼è çé ç½®ä¸ï¼å¦æè¦ç»§ç»å¤çåå ¥ï¼åå¯è½éè¦æ§è¡æ éåæ¢ï¼è¯·åé ã[å¤çèç¹å®æº](#å¤çèç¹å®æº)ãï¼ã å¦ä¸æ¹é¢ï¼å¨æ 主é ç½®ä¸ï¼ä¸åå¨æ é转移ã[å¾ 5-10](img/fig5-10.png) æ¼ç¤ºäºä¼åçäºä»ä¹äºæ ï¼å®¢æ·ç«¯ï¼ç¨æ· 1234ï¼å¹¶è¡åéåå ¥å°ææä¸ä¸ªå¯æ¬ï¼å¹¶ä¸ä¸¤ä¸ªå¯ç¨å¯æ¬æ¥ååå ¥ï¼ä½æ¯ä¸å¯ç¨å¯æ¬éè¿äºå®ãå设ä¸ä¸ªå¯æ¬ä¸ç两个æ¿è®¤åå ¥æ¯è¶³å¤çï¼å¨ç¨æ· 1234 å·²ç»æ¶å°ä¸¤ä¸ªç¡®å®çååºä¹åï¼æ们认为åå ¥æåã客æ·ç®åå°å¿½ç¥äºå ¶ä¸ä¸ä¸ªå¯æ¬éè¿äºåå ¥çäºå®ã ![](img/fig5-10.png) **å¾ 5-10 æ³å®åå ¥ï¼æ³å®è¯»åï¼å¹¶å¨èç¹ä¸æå读修å¤ã** ç°å¨æ³è±¡ä¸ä¸ï¼ä¸å¯ç¨çèç¹éæ°èæºï¼å®¢æ·ç«¯å¼å§è¯»åå®ãèç¹å ³éæé´åççä»»ä½åå ¥é½ä¸å¨è¯¥èç¹ä¸ãå æ¤ï¼å¦æä½ ä»è¯¥èç¹è¯»åæ°æ®ï¼åå¯è½ä¼ä»ååºä¸æ¿å°éæ§çï¼è¿æ¶çï¼å¼ã 为äºè§£å³è¿ä¸ªé®é¢ï¼å½ä¸ä¸ªå®¢æ·ç«¯ä»æ°æ®åºä¸è¯»åæ°æ®æ¶ï¼å®ä¸ä» ä» æå®ç请æ±åéå°ä¸ä¸ªå¯æ¬ï¼è¯»è¯·æ±å°è¢«å¹¶è¡å°åéå°å¤ä¸ªèç¹ã客æ·å¯è½ä¼ä»ä¸åçèç¹è·å¾ä¸åçååºï¼å³æ¥èªä¸ä¸ªèç¹çææ°å¼åæ¥èªå¦ä¸ä¸ªèç¹çéæ§å¼ãçæ¬å·å°è¢«ç¨äºç¡®å®åªä¸ªå¼æ¯æ´æ°çï¼è¯·åé â[æ£æµå¹¶ååå ¥](#æ£æµå¹¶ååå ¥)âï¼ã #### 读修å¤ååçµ å¤å¶æ¹æ¡åºç¡®ä¿æç»å°æææ°æ®å¤å¶å°æ¯ä¸ªå¯æ¬ãå¨ä¸ä¸ªä¸å¯ç¨çèç¹éæ°èæºä¹åï¼å®å¦ä½èµ¶ä¸å®éè¿çåå ¥ï¼ å¨ Dynamo é£æ ¼çæ°æ®åå¨ä¸ç»å¸¸ä½¿ç¨ä¸¤ç§æºå¶ï¼ * 读修å¤ï¼Read repairï¼ å½å®¢æ·ç«¯å¹¶è¡è¯»åå¤ä¸ªèç¹æ¶ï¼å®å¯ä»¥æ£æµå°ä»»ä½éæ§çååºãä¾å¦ï¼å¨ [å¾ 5-10](img/fig5-10.png) ä¸ï¼ç¨æ· 2345 è·å¾äºæ¥èªå¯æ¬ 3 ççæ¬ 6 å¼åæ¥èªå¯æ¬ 1 å 2 ççæ¬ 7 å¼ã客æ·ç«¯åç°å¯æ¬ 3 å ·æéæ§å¼ï¼å¹¶å°æ°å¼ååå°è¯¥å¯æ¬ãè¿ç§æ¹æ³éç¨äºè¯»é¢ç¹çå¼ã * åçµè¿ç¨ï¼Anti-entropy processï¼ æ¤å¤ï¼ä¸äºæ°æ®åå¨å ·æåå°è¿ç¨ï¼è¯¥è¿ç¨ä¸ææ¥æ¾å¯æ¬ä¹é´çæ°æ®å·®å¼ï¼å¹¶å°ä»»ä½ç¼ºå°çæ°æ®ä»ä¸ä¸ªå¯æ¬å¤å¶å°å¦ä¸ä¸ªå¯æ¬ãä¸åºäºé¢å¯¼è çå¤å¶ä¸çå¤å¶æ¥å¿ä¸åï¼æ¤åçµè¿ç¨ä¸ä¼ä»¥ä»»ä½ç¹å®ç顺åºå¤å¶åå ¥ï¼å¹¶ä¸å¨å¤å¶æ°æ®ä¹åå¯è½ä¼ææ¾èç延è¿ã 并ä¸æ¯ææçç³»ç»é½å®ç°äºè¿ä¸¤ç§æºå¶ï¼ä¾å¦ï¼Voldemort ç®å没æåçµè¿ç¨ã请注æï¼å¦æ没æåçµè¿ç¨ï¼å¾å°è¢«è¯»åçå¼å¯è½ä¼ä»æäºå¯æ¬ä¸ä¸¢å¤±ï¼ä»èéä½äºæä¹ æ§ï¼å 为åªæå¨åºç¨ç¨åºè¯»åå¼æ¶ææ§è¡è¯»ä¿®å¤ã #### 读åçæ³å®äººæ° å¨ [å¾ 5-10](img/fig5-10.png) ç示ä¾ä¸ï¼æ们认为å³ä½¿ä» å¨ä¸ä¸ªå¯æ¬ä¸ç两个ä¸è¿è¡å¤çï¼åå ¥ä»ç¶æ¯æåçãå¦æä¸ä¸ªå¯æ¬ä¸åªæä¸ä¸ªæ¥åäºåå ¥ï¼ä¼ææ ·ï¼ä»¥æ¤ç±»æ¨ï¼ç©¶ç«å¤å°ä¸ªå¯æ¬å®ææå¯ä»¥è®¤ä¸ºåå ¥æåï¼ å¦ææ们ç¥éï¼æ¯ä¸ªæåçåæä½æå³çå¨ä¸ä¸ªå¯æ¬ä¸è³å°æ两个åºç°ï¼è¿æå³çè³å¤æä¸ä¸ªå¯æ¬å¯è½æ¯éæ§çãå æ¤ï¼å¦ææ们ä»è³å°ä¸¤ä¸ªå¯æ¬è¯»åï¼æ们å¯ä»¥ç¡®å®è³å°æä¸ä¸ªæ¯ææ°çãå¦æ第ä¸ä¸ªå¯æ¬åæºæååºé度ç¼æ ¢ï¼å读åä»å¯ä»¥ç»§ç»è¿åææ°å¼ã æ´ä¸è¬å°è¯´ï¼å¦ææ n 个å¯æ¬ï¼æ¯ä¸ªåå ¥å¿ é¡»ç± w 个èç¹ç¡®è®¤æè½è¢«è®¤ä¸ºæ¯æåçï¼å¹¶ä¸æä»¬å¿ é¡»è³å°ä¸ºæ¯ä¸ªè¯»åæ¥è¯¢ r 个èç¹ãï¼å¨æ们çä¾åä¸ï¼$n = 3ï¼w = 2ï¼r = 2$ï¼ãåªè¦ $w + r > n$ï¼æ们å¯ä»¥é¢æå¨è¯»åæ¶è½è·å¾ææ°çå¼ï¼å 为 r 个读åä¸è³å°æä¸ä¸ªèç¹æ¯ææ°çãéµå¾ªè¿äº r å¼å w å¼ç读å称为 **æ³å®äººæ°ï¼quorumï¼**[^vii] ç读ååã44ããä½ å¯ä»¥è®¤ä¸ºï¼r å w æ¯ææ读åæéçæä½ç¥¨æ°ã [^vii]: ææ¶åè¿ç§æ³å®äººæ°è¢«ç§°ä¸ºä¸¥æ ¼çæ³å®äººæ°ï¼å ¶ç¸å¯¹ â宽æ¾çæ³å®äººæ°â èè¨ï¼è§ â[宽æ¾çæ³å®äººæ°ä¸æ示移交](#宽æ¾çæ³å®äººæ°ä¸æ示移交)âï¼ å¨ Dynamo é£æ ¼çæ°æ®åºä¸ï¼åæ° nãw å r é常æ¯å¯é ç½®çãä¸ä¸ªå¸¸è§çéæ©æ¯ä½¿ n 为å¥æ°ï¼é常为 3 æ 5ï¼å¹¶è®¾ç½® $w = r = (n + 1) / 2$ï¼åä¸åæ´ï¼ãä½æ¯ä½ å¯ä»¥æ ¹æ®éè¦æ´æ¹æ°åãä¾å¦ï¼åå ¥æ¬¡æ°è¾å°ä¸è¯»å次æ°è¾å¤çå·¥ä½è´è½½å¯ä»¥ä»è®¾ç½® $w = n$ å $r = 1$ä¸åçãè¿ä¼ä½¿å¾è¯»åé度æ´å¿«ï¼ä½ç¼ºç¹æ¯åªè¦æä¸ä¸ªä¸å¯ç¨çèç¹å°±ä¼å¯¼è´ææçæ°æ®åºåå ¥é½å¤±è´¥ã > é群ä¸å¯è½æå¤äº n 个çèç¹ï¼é群çæºå¨æ°å¯è½å¤äºå¯æ¬æ°ç®ï¼ãä½æ¯ä»»ä½ç»å®çå¼åªè½åå¨å¨ n 个èç¹ä¸ãè¿å 许对æ°æ®éè¿è¡ååºï¼ä»èå¯ä»¥æ¯ææ¯å个èç¹çåå¨è½åæ´å¤§çæ°æ®éãæ们å°å¨ [第å ç« ](ch6.md) 继ç»è®¨è®ºååºã æ³å®äººæ°æ¡ä»¶ $w + r > n$ å 许系ç»å®¹å¿ä¸å¯ç¨çèç¹ï¼å¦ä¸æç¤ºï¼ * å¦æ $w < n$ï¼å½èç¹ä¸å¯ç¨æ¶ï¼æ们ä»ç¶å¯ä»¥å¤çåå ¥ã * å¦æ $r < n$ï¼å½èç¹ä¸å¯ç¨æ¶ï¼æ们ä»ç¶å¯ä»¥å¤ç读åã * å¯¹äº $n = 3ï¼w = 2ï¼r = 2$ï¼æ们å¯ä»¥å®¹å¿ä¸ä¸ªä¸å¯ç¨çèç¹ã * å¯¹äº $n = 5ï¼w = 3ï¼r = 3$ï¼æ们å¯ä»¥å®¹å¿ä¸¤ä¸ªä¸å¯ç¨çèç¹ãè¿ä¸ªæ¡ä¾å¦ [å¾ 5-11](img/fig5-11.png) æ示ã * é常ï¼è¯»åååå ¥æä½å§ç»å¹¶è¡åéå°ææ n 个å¯æ¬ãåæ° w å r å³å®æ们çå¾ å¤å°ä¸ªèç¹ï¼å³å¨æ们认为读æåæåä¹åï¼æå¤å°ä¸ªèç¹éè¦æ¥åæåã ![](img/fig5-11.png) **å¾ 5-11 å¦æ $w + r > n$ï¼è¯»å r 个å¯æ¬ï¼è³å°æä¸ä¸ªå¯æ¬å¿ ç¶å å«äºæè¿çæååå ¥ã** å¦æå¯ç¨çèç¹å°äºæéç w æ rï¼ååå ¥æ读åå°è¿åé误ãèç¹å¯è½ç±äºå¤ç§åå èä¸å¯ç¨ï¼æ¯å¦ï¼èç¹å ³éï¼å¼å¸¸å´©æºï¼çµæºå ³éï¼ãæä½æ§è¡è¿ç¨ä¸çé误ï¼ç±äºç£ç已满èæ æ³åå ¥ï¼ã客æ·ç«¯åæå¡å¨èç¹ä¹é´çç½ç»ä¸ææä»»ä½å ¶ä»åå ãæ们åªéè¦å ³å¿èç¹æ¯å¦è¿åäºæåçååºï¼èä¸éè¦åºåä¸åç±»åçé误ã ### æ³å®äººæ°ä¸è´æ§çå±éæ§ å¦æä½ æ n 个å¯æ¬ï¼å¹¶ä¸ä½ éæ©äºæ»¡è¶³ $w + r > n$ ç w å rï¼ä½ é常å¯ä»¥æææ¯æ¬¡è¯»åé½è½è¿åæè¿åå ¥çå¼ãæ åµå°±æ¯è¿æ ·ï¼å ä¸ºä½ åå ¥çèç¹éååä½ è¯»åçèç¹éåå¿ ç¶æéå ãä¹å°±æ¯è¯´ï¼ä½ 读åçèç¹ä¸å¿ ç¶è³å°æä¸ä¸ªèç¹å ·æææ°å¼ï¼å¦ [å¾ 5-11](img/fig5-11.png) æ示ï¼ã é常ï¼r å w 被é为å¤æ°ï¼è¶ è¿ $n/2$ ï¼èç¹ï¼å 为è¿ç¡®ä¿äº $w + r > n$ï¼åæ¶ä»ç¶å®¹å¿å¤è¾¾ $n/2$ 个èç¹æ éãä½æ¯ï¼æ³å®äººæ°ä¸ä¸å®å¿ é¡»æ¯å¤§å¤æ°ï¼éè¦çæ¯è¯»å使ç¨çèç¹è³å°æä¸ä¸ªèç¹ç交éãå ¶ä»æ³å®äººæ°çé ç½®æ¯å¯è½çï¼è¿ä½¿å¾åå¸å¼ç®æ³ç设计æä¸å®ççµæ´»æ§ã45ãã ä½ ä¹å¯ä»¥å° w å r 设置为è¾å°çæ°åï¼ä»¥ä½¿ $w + r ⤠n$ï¼å³æ³å®æ¡ä»¶ä¸æ»¡è¶³ï¼ãå¨è¿ç§æ åµä¸ï¼è¯»åååå ¥æä½ä»å°è¢«åéå° n 个èç¹ï¼ä½æä½æååªéè¦å°éçæåååºã è¾å°ç w å r æ´æå¯è½ä¼è¯»åå°éæ§çæ°æ®ï¼å ä¸ºä½ ç读åæ´æå¯è½æªå å«å ·æææ°å¼çèç¹ãå¦ä¸æ¹é¢ï¼è¿ç§é ç½®å 许æ´ä½ç延è¿åæ´é«çå¯ç¨æ§ï¼å¦æåå¨ç½ç»ä¸æï¼å¹¶ä¸è®¸å¤å¯æ¬åå¾æ æ³è®¿é®ï¼åææ´å¤§çæºä¼å¯ä»¥ç»§ç»å¤ç读åååå ¥ãåªæå½å¯è¾¾å¯æ¬çæ°éä½äº w æ r æ¶ï¼æ°æ®åºæåå¾ä¸å¯åå ¥æ读åã ä½æ¯ï¼å³ä½¿å¨ $w + r > n$ çæ åµä¸ï¼ä¹å¯è½åå¨è¿åéæ§å¼çè¾¹ç¼æ åµãè¿åå³äºå®ç°ï¼ä½å¯è½çæ åµå æ¬ï¼ * å¦æ使ç¨å®½æ¾çæ³å®äººæ°ï¼è§ â[宽æ¾çæ³å®äººæ°ä¸æ示移交](#宽æ¾çæ³å®äººæ°ä¸æ示移交)âï¼ï¼w 个åå ¥å r 个读åæå¯è½è½å¨å®å ¨ä¸åçèç¹ä¸ï¼å æ¤ r èç¹å w ä¹é´ä¸åä¿è¯æéå èç¹ã46ãã * å¦æ两个åå ¥åæ¶åçï¼ä¸æ¸ æ¥åªä¸ä¸ªå åçãå¨è¿ç§æ åµä¸ï¼å¯ä¸å®å ¨ç解å³æ¹æ¡æ¯å并并ååå ¥ï¼è¯·åé â[å¤çåå ¥å²çª](#å¤çåå ¥å²çª)âï¼ãå¦ææ ¹æ®æ¶é´æ³ï¼æååå ¥èå©ï¼æéåºä¸ä¸ªèè ï¼ååå ¥å¯è½ç±äºæ¶éåå·®ã35ãè丢失ãæ们å°å¨ â[æ£æµå¹¶ååå ¥](#æ£æµå¹¶ååå ¥)â 继ç»è®¨è®ºæ¤è¯é¢ã * å¦æåæä½ä¸è¯»æä½åæ¶åçï¼åæä½å¯è½ä» åæ å¨æäºå¯æ¬ä¸ãå¨è¿ç§æ åµä¸ï¼ä¸ç¡®å®è¯»åè¿åçæ¯æ§å¼è¿æ¯æ°å¼ã * å¦æåæä½å¨æäºå¯æ¬ä¸æåï¼èå¨å ¶ä»èç¹ä¸å¤±è´¥ï¼ä¾å¦ï¼å 为æäºèç¹ä¸çç£ç已满ï¼ï¼å¨å°äº w 个å¯æ¬ä¸åå ¥æåãæ以æ´ä½å¤å®åå ¥å¤±è´¥ï¼ä½æ´ä½åå ¥å¤±è´¥å¹¶æ²¡æå¨åå ¥æåçå¯æ¬ä¸åæ»ãè¿æå³çä¸ä¸ªåå ¥è½ç¶æ¥å失败ï¼åç»ç读åä»ç¶å¯è½ä¼è¯»åè¿æ¬¡å¤±è´¥åå ¥çå¼ã47ãã * å¦ææºå¸¦æ°å¼çèç¹åçæ éï¼éè¦ä»å ¶ä»å¸¦ææ§å¼çå¯æ¬è¿è¡æ¢å¤ï¼ååå¨æ°å¼çå¯æ¬æ°å¯è½ä¼ä½äº wï¼ä»èæç ´æ³å®äººæ°æ¡ä»¶ã * å³ä½¿ä¸åå·¥ä½æ£å¸¸ï¼ææ¶ä¹ä¼ä¸å¹¸å°åºç°å ³äº **æ¶åºï¼timingï¼** çè¾¹ç¼æ åµï¼æ们å°å¨ â[线æ§ä¸è´æ§åæ³å®äººæ°](ch9.md#线æ§ä¸è´æ§åæ³å®äººæ°)â ä¸çå°è¿ç¹ã å æ¤ï¼å°½ç®¡æ³å®äººæ°ä¼¼ä¹ä¿è¯è¯»åè¿åææ°çåå ¥å¼ï¼ä½å¨å®è·µä¸å¹¶ä¸é£ä¹ç®åãDynamo é£æ ¼çæ°æ®åºé常é对å¯ä»¥å¿åæç»ä¸è´æ§çç¨ä¾è¿è¡ä¼åãä½ å¯ä»¥éè¿åæ° w å r æ¥è°æ´è¯»åå°éæ§å¼çæ¦çï¼ä½æå®ä»¬å½æç»å¯¹çä¿è¯æ¯ä¸ææºçã å°¤å ¶æ¯ï¼å 为é常å¾ä¸å° â[å¤å¶å»¶è¿é®é¢](#å¤å¶å»¶è¿é®é¢)â ä¸è®¨è®ºçé£äºä¿è¯ï¼è¯»å·±ä¹åï¼åè°è¯»ï¼ä¸è´åç¼è¯»ï¼ï¼åé¢æå°çå¼å¸¸å¯è½ä¼åçå¨åºç¨ç¨åºä¸ãæ´å¼ºæåçä¿è¯é常éè¦ **äºå¡** æ **å ±è¯**ãæ们å°å¨ [第ä¸ç« ](ch7.md) å [第ä¹ç« ](ch9.md) åå°è¿äºè¯é¢ã #### çæ§éæ§åº¦ ä»è¿ç»´çè§åº¦æ¥çï¼çè§ä½ çæ°æ®åºæ¯å¦è¿åææ°çç»ææ¯å¾éè¦çãå³ä½¿åºç¨å¯ä»¥å®¹å¿éæ§ç读åï¼ä½ ä¹éè¦äºè§£å¤å¶çå¥åº·ç¶åµãå¦ææ¾èè½åï¼å®åºè¯¥æéä½ ä»¥ä¾¿ä½ å¯ä»¥è°æ¥åå ï¼ä¾å¦ç½ç»ä¸çé®é¢æè¿è½½çèç¹ï¼ã 对äºåºäºé¢å¯¼è çå¤å¶ï¼æ°æ®åºé常ä¼æä¾å¤å¶å»¶è¿çæµéå¼ï¼ä½ å¯ä»¥å°å ¶æä¾ç»çè§ç³»ç»ãè¿ä¹æ以è½åå°ï¼æ¯å 为åå ¥æ¯æç §ç¸åç顺åºåºç¨äºä¸»åºåä»åºï¼å¹¶ä¸æ¯ä¸ªèç¹å¯¹åºäºå¤å¶æ¥å¿ä¸çä¸ä¸ªä½ç½®ï¼å·²ç»å¨æ¬å°åºç¨çåå ¥æ°éï¼ãéè¿ä»ä¸»åºçå½åä½ç½®ä¸åå»ä»åºçå½åä½ç½®ï¼ä½ å¯ä»¥æµéå¤å¶å»¶è¿çç¨åº¦ã ç¶èï¼å¨æ 主å¤å¶çç³»ç»ä¸ï¼æ²¡æåºå®çåå ¥é¡ºåºï¼è¿ä½¿å¾çæ§åå¾æ´å å°é¾ãèä¸ï¼å¦ææ°æ®åºåªä½¿ç¨è¯»ä¿®å¤ï¼æ²¡æåçµè¿ç¨ï¼ï¼é£ä¹å¯¹äºä¸ä¸ªå¼å¯è½ä¼æå¤éæ§å ¶å®æ¯æ²¡æéå¶ç - å¦æä¸ä¸ªå¼å¾å°è¢«è¯»åï¼é£ä¹ç±ä¸ä¸ªéæ§å¯æ¬è¿åçå¼å¯è½æ¯å¤èçã å·²ç»æä¸äºå ³äºè¡¡éæ 主å¤å¶æ°æ®åºä¸çå¤å¶éæ§åº¦çç 究ï¼å¹¶æ ¹æ®åæ° nãw å r æ¥é¢æµéæ§è¯»åçé¢æç¾åæ¯ã48ããä¸å¹¸çæ¯ï¼è¿è¿ä¸æ¯å¾å¸¸è§çåæ³ï¼ä½æ¯å°éæ§æµéå¼å å«å¨æ°æ®åºçæ å度ééä¸æ¯ä¸ä»¶å¥½äºãè½ç¶æç»ä¸è´æ§æ¯ä¸ç§ææ模ç³çä¿è¯ï¼ä½æ¯ä»å¯æä½æ§è§åº¦æ¥è¯´ï¼è½å¤éå âæç»â ä¹æ¯å¾éè¦çã ### 宽æ¾çæ³å®äººæ°ä¸æ示移交 åçé ç½®çæ³å®äººæ°å¯ä»¥ä½¿æ°æ®åºæ éæ éåæ¢å³å¯å®¹å¿ä¸ªå«èç¹çæ éãå®ä¹å¯ä»¥å®¹å¿ä¸ªå«èç¹åæ ¢ï¼å 为请æ±ä¸å¿ çå¾ ææ n 个èç¹ååº ââ å½ w æ r 个èç¹ååºæ¶å®ä»¬å°±å¯ä»¥è¿åã对äºéè¦é«å¯ç¨ãä½å»¶æ¶ãä¸è½å¤å®¹å¿å¶å°è¯»å°éæ§å¼çåºç¨åºæ¯æ¥è¯´ï¼è¿äºç¹æ§ä½¿æ 主å¤å¶çæ°æ®åºå¾æå¸å¼åã ç¶èï¼æ³å®äººæ°ï¼å¦è¿ä»ä¸ºæ¢ææè¿°çï¼å¹¶ä¸åå®ä»¬å¯è½çé£æ ·å ·æ容éæ§ãç½ç»ä¸æå¯ä»¥å¾å®¹æå°å°å®¢æ·ç«¯ä»å¤§éçæ°æ®åºèç¹ä¸åæãè½ç¶è¿äºèç¹æ¯æ´»ççï¼èå ¶ä»å®¢æ·ç«¯å¯è½ä¹è½å¤è¿æ¥å°å®ä»¬ï¼ä½æ¯ä»æ°æ®åºèç¹åæç客æ·ç«¯æ¥çï¼å®ä»¬ä¹å¯è½å·²ç»æ»äº¡ãå¨è¿ç§æ åµä¸ï¼å©ä½çå¯ç¨èç¹å¯è½ä¼å°äº w æ rï¼å æ¤å®¢æ·ç«¯ä¸åè½è¾¾å°æ³å®äººæ°ã å¨ä¸ä¸ªå¤§åçé群ä¸ï¼èç¹æ°éææ¾å¤äº n 个ï¼ï¼ç½ç»ä¸ææé´å®¢æ·ç«¯å¯è½ä»è½è¿æ¥å°ä¸äºæ°æ®åºèç¹ï¼ä½åä¸è¶³ä»¥ç»æä¸ä¸ªç¹å®çæ³å®äººæ°ãå¨è¿ç§æ åµä¸ï¼æ°æ®åºè®¾è®¡äººåéè¦æè¡¡ä¸ä¸ï¼ * 对äºæææ æ³è¾¾å° w æ r 个èç¹æ³å®äººæ°ç请æ±ï¼æ¯å¦è¿åé误æ¯æ´å¥½çï¼ * æè æ们æ¯å¦åºè¯¥æ¥ååå ¥ï¼ç¶åå°å®ä»¬åå ¥ä¸äºå¯è¾¾çèç¹ï¼ä½ä¸å¨è¿äºå¼é常æåå¨ç n 个èç¹ä¸ï¼ åè 被认为æ¯ä¸ä¸ª **宽æ¾çæ³å®äººæ°ï¼sloppy quorumï¼**ã37ãï¼åå读ä»ç¶éè¦ w å r 个æåçååºï¼ä½è¿äºååºå¯è½æ¥èªä¸å¨æå®ç n 个 â主â èç¹ä¸çå ¶å®èç¹ã就好æ¯è¯´ï¼å¦æä½ æèªå·±éå¨æ¿åå¤é¢äºï¼ä½ å¯è½ä¼å»æ²å¼é»å± çé¨ï¼é®æ¯å¦å¯ä»¥ææ¶åå¨ä»ä»¬çæ²åä¸ã ä¸æ¦ç½ç»ä¸æå¾å°è§£å³ï¼ä¸ä¸ªèç¹ä»£è¡¨å¦ä¸ä¸ªèç¹ä¸´æ¶æ¥åçä»»ä½åå ¥é½å°è¢«åéå°éå½ç â主â èç¹ãè¿å°±æ¯æè°ç **æ示移交ï¼hinted handoffï¼**ï¼ä¸æ¦ä½ å次æ¾å°ä½ çæ¿åçé¥åï¼ä½ çé»å± å¯ä»¥ç¤¼è²å°è¦æ±ä½ 离å¼æ²åå家ï¼ã 宽æ¾çæ³å®äººæ°å¯¹åå ¥å¯ç¨æ§çæé«ç¹å«æç¨ï¼åªè¦æä»»ä½ w 个èç¹å¯ç¨ï¼æ°æ®åºå°±å¯ä»¥æ¥ååå ¥ãç¶èï¼è¿æå³çå³ä½¿å½ $w + r > n$ æ¶ï¼ä¹ä¸è½ç¡®ä¿è¯»åå°æ个é®çææ°å¼ï¼å 为ææ°çå¼å¯è½å·²ç»ä¸´æ¶åå ¥äº n ä¹å¤çæäºèç¹ã47ãã å æ¤ï¼å¨ä¼ ç»æä¹ä¸ï¼å®½æ¾çæ³å®äººæ°å®é ä¸å¹¶ä¸æ¯æ³å®äººæ°ãå®åªæ¯ä¸ä¸ªæä¹ æ§çä¿è¯ï¼å³æ°æ®å·²åå¨å¨æå¤ç w 个èç¹ãä½ä¸è½ä¿è¯ r 个èç¹ç读åè½çå°å®ï¼é¤éæ示移交已ç»å®æã å¨ææ常è§ç Dynamo å®ç°ä¸ï¼å®½æ¾çæ³å®äººæ°æ¯å¯éçãå¨ Riak ä¸ï¼å®ä»¬é»è®¤æ¯å¯ç¨çï¼èå¨ Cassandra å Voldemort ä¸å®ä»¬é»è®¤æ¯ç¦ç¨çã46,49,50ãã #### è¿ç»´å¤ä¸ªæ°æ®ä¸å¿ æ们å å讨论äºè·¨æ°æ®ä¸å¿å¤å¶ï¼ä½ä¸ºå¤ä¸»å¤å¶çç¨ä¾ï¼è¯·åé â[å¤ä¸»å¤å¶](#å¤ä¸»å¤å¶)âï¼ãå ¶å®æ 主å¤å¶ä¹éç¨äºå¤æ°æ®ä¸å¿æä½ï¼æ¢ç¶å®æ¨å¨å®¹å¿å²çªç并ååå ¥ãç½ç»ä¸æå延è¿å°å³°ã Cassandra å Voldemort å¨æ£å¸¸çæ 主模åä¸å®ç°äºä»ä»¬çå¤æ°æ®ä¸å¿æ¯æï¼å¯æ¬çæ°é n å æ¬æææ°æ®ä¸å¿çèç¹ï¼ä½ å¯ä»¥å¨é ç½®ä¸æå®æ¯ä¸ªæ°æ®ä¸å¿ææ¥æçå¯æ¬çæ°éãæ 论æ°æ®ä¸å¿å¦ä½ï¼æ¯ä¸ªæ¥èªå®¢æ·ç«¯çåå ¥é½ä¼åéå°ææå¯æ¬ï¼ä½å®¢æ·ç«¯é常åªçå¾ æ¥èªå ¶æ¬å°æ°æ®ä¸å¿å çæ³å®èç¹ç确认ï¼ä»èä¸ä¼åå°è·¨æ°æ®ä¸å¿é¾è·¯å»¶è¿åä¸æçå½±åãå¯¹å ¶ä»æ°æ®ä¸å¿çé«å»¶è¿åå ¥é常被é 置为å¼æ¥æ§è¡ï¼å°½ç®¡è¯¥é ç½®ä»æä¸å®ççµæ´»æ§ã50,51ãã Riak å°å®¢æ·ç«¯åæ°æ®åºèç¹ä¹é´çææéä¿¡ä¿æå¨ä¸ä¸ªæ¬å°çæ°æ®ä¸å¿ï¼å æ¤ n æè¿°äºä¸ä¸ªæ°æ®ä¸å¿å çå¯æ¬æ°éãæ°æ®åºé群ä¹é´çè·¨æ°æ®ä¸å¿å¤å¶å¨åå°å¼æ¥åçï¼å ¶é£æ ¼ç±»ä¼¼äºå¤ä¸»å¤å¶ã52ãã ### æ£æµå¹¶ååå ¥ Dynamo é£æ ¼çæ°æ®åºå 许å¤ä¸ªå®¢æ·ç«¯åæ¶åå ¥ç¸åçé®ï¼Keyï¼ï¼è¿æå³çå³ä½¿ä½¿ç¨ä¸¥æ ¼çæ³å®äººæ°ä¹ä¼åçå²çªãè¿ç§æ åµä¸å¤ä¸»å¤å¶ç¸ä¼¼ï¼è¯·åé â[å¤çåå ¥å²çª](#å¤çåå ¥å²çª)âï¼ï¼ä½å¨ Dynamo é£æ ¼çæ°æ®åºä¸ï¼å¨ **读修å¤** æ **æ示移交** æé´ä¹å¯è½ä¼äº§çå²çªã å ¶é®é¢å¨äºï¼ç±äºå¯åçç½ç»å»¶è¿åé¨åèç¹çæ éï¼äºä»¶å¯è½ä»¥ä¸åç顺åºå°è¾¾ä¸åçèç¹ãä¾å¦ï¼[å¾ 5-12](img/fig5-12.png) æ¾ç¤ºäºä¸¤ä¸ªå®¢æ·æº A å B åæ¶åå ¥ä¸èç¹æ°æ®åå¨ä¸çé® Xï¼ * èç¹ 1 æ¥æ¶æ¥èª A çåå ¥ï¼ä½ç±äºææ¶ä¸æï¼æªæ¥æ¶å°æ¥èª B çåå ¥ã * èç¹ 2 é¦å æ¥æ¶æ¥èª A çåå ¥ï¼ç¶åæ¥æ¶æ¥èª B çåå ¥ã * èç¹ 3 é¦å æ¥æ¶æ¥èª B çåå ¥ï¼ç¶åä» A åå ¥ã ![](img/fig5-12.png) **å¾ 5-12 并ååå ¥ Dynamo é£æ ¼çæ°æ®åå¨ï¼æ²¡ææç¡®å®ä¹ç顺åºã** å¦ææ¯ä¸ªèç¹åªè¦æ¥æ¶å°æ¥èªå®¢æ·ç«¯çåå ¥è¯·æ±å°±ç®åå°è¦åæ个é®å¼ï¼é£ä¹èç¹å°±ä¼æ°¸ä¹ å°ä¸ä¸è´ï¼å¦ [å¾ 5-12](img/fig5-12.png) ä¸çæç»è·å请æ±æ示ï¼èç¹ 2 认为 X çæç»å¼æ¯ Bï¼èå ¶ä»èç¹è®¤ä¸ºå¼æ¯ A ã 为äºæç»è¾¾æä¸è´ï¼å¯æ¬åºè¯¥è¶äºç¸åçå¼ãå¦ä½åå°è¿ä¸ç¹ï¼æ人å¯è½å¸æå¤å¶çæ°æ®åºè½å¤èªå¨å¤çï¼ä½ä¸å¹¸çæ¯ï¼å¤§å¤æ°çå®ç°é½å¾ç³ç³ï¼å¦æä½ æ³é¿å 丢失æ°æ®ï¼ä½ ï¼åºç¨ç¨åºå¼å人åï¼éè¦ç¥éå¾å¤æå ³æ°æ®åºå²çªå¤ççå é¨ä¿¡æ¯ã å¨ â[å¤çåå ¥å²çª](#å¤çåå ¥å²çª)â ä¸èä¸å·²ç»ç®è¦ä»ç»äºä¸äºè§£å³å²çªçææ¯ãå¨æ»ç»æ¬ç« ä¹åï¼è®©æ们æ¥æ´è¯¦ç»å°æ¢è®¨è¿ä¸ªé®é¢ã #### æååå ¥èå©ï¼ä¸¢å¼å¹¶ååå ¥ï¼ å®ç°æç»æ¶æçä¸ç§æ¹æ³æ¯å£°ææ¯ä¸ªå¯æ¬åªéè¦åå¨ **âæè¿â** çå¼ï¼å¹¶å 许 **âæ´æ§â** çå¼è¢«è¦çåæå¼ãç¶åï¼åªè¦æ们æä¸ç§æç¡®çæ¹å¼æ¥ç¡®å®åªä¸ªåæ¯ âæè¿çâï¼å¹¶ä¸æ¯ä¸ªåå ¥æç»é½è¢«å¤å¶å°æ¯ä¸ªå¯æ¬ï¼é£ä¹å¤å¶æç»ä¼æ¶æå°ç¸åçå¼ã æ£å¦ **âæè¿â** çå¼å·æ表æçï¼è¿ä¸ªæ³æ³å ¶å®é¢å ·è¯¯å¯¼æ§ãå¨ [å¾ 5-12](img/fig5-12.png) çä¾åä¸ï¼å½å®¢æ·ç«¯åæ°æ®åºèç¹åéåå ¥è¯·æ±æ¶ï¼ä¸¤ä¸ªå®¢æ·ç«¯é½ä¸ç¥éå¦ä¸ä¸ªå®¢æ·ç«¯ï¼å æ¤ä¸æ¸ æ¥åªä¸ä¸ªå åé请æ±ãäºå®ä¸ï¼è¯´è¿ä¸¤ç§æ åµè°å åé请æ±æ¯æ²¡ææä¹çï¼æ¢ç¶æ们说åå ¥æ¯ **并åï¼concurrentï¼** çï¼é£ä¹å®ä»¬ç顺åºå°±æ¯ä¸ç¡®å®çã å³ä½¿åå ¥æ²¡æèªç¶çæåºï¼æ们ä¹å¯ä»¥å¼ºå¶è¿è¡æåºãä¾å¦ï¼å¯ä»¥ä¸ºæ¯ä¸ªåå ¥éå ä¸ä¸ªæ¶é´æ³ï¼ç¶åæéæ大çæ¶é´æ³ä½ä¸º **âæè¿çâ**ï¼å¹¶ä¸¢å¼å ·æè¾æ©æ¶é´æ³çä»»ä½åå ¥ãè¿ç§å²çªè§£å³ç®æ³è¢«ç§°ä¸º **æååå ¥èå©ï¼LWW, last write winsï¼**ï¼æ¯ Cassandra å¯ä¸æ¯æçå²çªè§£å³æ¹æ³ã53ãï¼ä¹æ¯ Riak ä¸çä¸ä¸ªå¯éç¹å¾ã35ãã LWW å®ç°äºæç»æ¶æçç®æ ï¼ä½ä»¥ **æä¹ æ§** 为代价ï¼å¦æåä¸ä¸ªé®æå¤ä¸ªå¹¶ååå ¥ï¼å³ä½¿å®ä»¬åé¦ç»å®¢æ·ç«¯çç»æé½æ¯æåçï¼å 为å®ä»¬è¢«åå ¥ w 个å¯æ¬ï¼ï¼ä¹åªæä¸ä¸ªåå ¥å°è¢«ä¿çï¼èå ¶ä»åå ¥å°è¢«é»é»å°ä¸¢å¼ãæ¤å¤ï¼LWW çè³å¯è½ä¼ä¸¢å¼ä¸æ¯å¹¶åçåå ¥ï¼æ们å°å¨ â[æåºäºä»¶çæ¶é´æ³](ch8.md#æåºäºä»¶çæ¶é´æ³)â ä¸è¿è¡è®¨è®ºã å¨ç±»ä¼¼ç¼åçä¸äºæ åµä¸ï¼åå ¥ä¸¢å¤±å¯è½æ¯å¯ä»¥æ¥åçãä½å¦ææ°æ®ä¸¢å¤±ä¸å¯æ¥åï¼LWW æ¯è§£å³å²çªçä¸ä¸ªå¾ççéæ©ã å¨æ°æ®åºä¸ä½¿ç¨ LWW çå¯ä¸å®å ¨æ¹æ³æ¯ç¡®ä¿ä¸ä¸ªé®åªåå ¥ä¸æ¬¡ï¼ç¶åè§ä¸ºä¸å¯åï¼ä»èé¿å 对åä¸ä¸ªé®è¿è¡å¹¶åæ´æ°ãä¾å¦ï¼Cassandra æ¨è使ç¨çæ¹æ³æ¯ä½¿ç¨ UUID ä½ä¸ºé®ï¼ä»è为æ¯ä¸ªåæä½æä¾ä¸ä¸ªå¯ä¸çé®ã53ãã #### âæ¤ååçâçå ³ç³»å并å æ们å¦ä½å¤æ两个æä½æ¯å¦æ¯å¹¶åçï¼ä¸ºäºå»ºç«ä¸ä¸ªç´è§ï¼è®©æ们ççä¸äºä¾åï¼ * å¨ [å¾ 5-9](fig5-9.png) ä¸ï¼ä¸¤ä¸ªåå ¥ä¸æ¯å¹¶åçï¼A çæå ¥åçå¨ B çéå¢ä¹åï¼å 为 B éå¢çå¼æ¯ A æå ¥çå¼ãæ¢å¥è¯è¯´ï¼B çæä½å»ºç«å¨ A çæä½ä¸ï¼æ以 B çæä½å¿ é¡»ååçãæ们ä¹å¯ä»¥è¯´ B **å æä¾èµï¼causally dependentï¼** äº Aã * å¦ä¸æ¹é¢ï¼[å¾ 5-12](fig5-12.png) ä¸ç两个åå ¥æ¯å¹¶åçï¼å½æ¯ä¸ªå®¢æ·ç«¯å¯å¨æä½æ¶ï¼å®ä¸ç¥éå¦ä¸ä¸ªå®¢æ·ç«¯ä¹æ£å¨å¯¹åæ ·çé®æ§è¡æä½ãå æ¤ï¼æä½ä¹é´ä¸åå¨å æå ³ç³»ã å¦ææä½ B äºè§£æä½ Aï¼æè ä¾èµäº Aï¼æè 以æç§æ¹å¼æ建äºæä½ A ä¹ä¸ï¼åæä½ A å¨æä½ B ä¹ååçï¼happens beforeï¼ãä¸ä¸ªæä½æ¯å¦å¨å¦ä¸ä¸ªæä½ä¹ååçæ¯å®ä¹å¹¶åå«ä¹çå ³é®ãäºå®ä¸ï¼æ们å¯ä»¥ç®åå°è¯´ï¼å¦æ两个æä½ä¸çä»»ä½ä¸ä¸ªé½ä¸å¨å¦ä¸ä¸ªä¹ååçï¼å³ï¼ä¸¤ä¸ªæä½é½ä¸ç¥é对æ¹ï¼ï¼é£ä¹è¿ä¸¤ä¸ªæä½æ¯å¹¶åçã54ãã å æ¤ï¼åªè¦æ两个æä½ A å Bï¼å°±æä¸ç§å¯è½æ§ï¼A å¨ B ä¹ååçï¼æè B å¨ A ä¹ååçï¼æè A å B 并åãæ们éè¦çæ¯ä¸ä¸ªç®æ³æ¥åè¯æ们两个æä½æ¯å¦æ¯å¹¶åçãå¦æä¸ä¸ªæä½åçå¨å¦ä¸ä¸ªæä½ä¹åï¼ååé¢çæä½åºè¯¥è¦çåé¢çæä½ï¼ä½æ¯å¦æè¿äºæä½æ¯å¹¶åçï¼ååå¨éè¦è§£å³çå²çªã > #### 并åæ§ãæ¶é´åç¸å¯¹æ§ > > å¦æ两个æä½ **âåæ¶â** åçï¼ä¼¼ä¹åºè¯¥ç§°ä¸ºå¹¶å ââ ä½äºå®ä¸ï¼å®ä»¬å¨åé¢æ¶é´ä¸éå ä¸å¦å¹¶ä¸éè¦ãç±äºåå¸å¼ç³»ç»ä¸çæ¶éé®é¢ï¼ç°å®ä¸æ¯å¾é¾å¤æ两个äºä»¶æ¯å¦æ¯ **åæ¶** åççï¼è¿ä¸ªé®é¢æ们å°å¨ [ç¬¬å «ç« ](ch8.md) ä¸è¯¦ç»è®¨è®ºã > > 为äºå®ä¹å¹¶åæ§ï¼ç¡®åçæ¶é´å¹¶ä¸éè¦ï¼å¦æ两个æä½é½æè¯ä¸å°å¯¹æ¹çåå¨ï¼å°±ç§°è¿ä¸¤ä¸ªæä½ **并å**ï¼èä¸ç®¡å®ä»¬å®é åççç©çæ¶é´ã人们ææ¶æè¿ä¸ªåçåç©çå¦ä¸ççä¹ç¸å¯¹è®ºè系起æ¥ã54ãï¼è¯¥ç论å¼å ¥äºä¿¡æ¯ä¸è½æ¯å éæ´å¿«çææ³ãå æ¤ï¼å¦æ两个äºä»¶åççæ¶é´å·®å°äºå éè¿å®ä»¬ä¹é´çè·ç¦»æéè¦çæ¶é´ï¼é£ä¹è¿ä¸¤ä¸ªäºä»¶ä¸å¯è½ç¸äºå½±åã > > å¨è®¡ç®æºç³»ç»ä¸ï¼å³ä½¿å éååä¸å 许ä¸ä¸ªæä½å½±åå¦ä¸ä¸ªæä½ï¼ä½ä¸¤ä¸ªæä½ä¹å¯è½æ¯ **并åç**ãä¾å¦ï¼å¦æç½ç»ç¼æ ¢æä¸æï¼ä¸¤ä¸ªæä½é´å¯è½ä¼åºç°ä¸æ®µæ¶é´é´éï¼ä½ä»ç¶æ¯å¹¶åçï¼å 为ç½ç»é®é¢é»æ¢ä¸ä¸ªæä½æè¯å°å¦ä¸ä¸ªæä½çåå¨ã #### æè·"æ¤ååç"å ³ç³» æ们æ¥çä¸ä¸ªç®æ³ï¼å®å¯ä»¥ç¡®å®ä¸¤ä¸ªæä½æ¯å¦ä¸ºå¹¶åçï¼è¿æ¯ä¸ä¸ªå¨å¦ä¸ä¸ªä¹åãç®åèµ·è§ï¼æ们ä»ä¸ä¸ªåªæä¸ä¸ªå¯æ¬çæ°æ®åºå¼å§ãä¸æ¦æ们ç¥éäºå¦ä½å¨å个å¯æ¬ä¸å®æè¿é¡¹å·¥ä½ï¼æ们å¯ä»¥å°è¯¥æ¹æ³æ¨å¹¿å°å ·æå¤ä¸ªå¯æ¬çæ 主æ°æ®åºã [å¾ 5-13](img/fig5-13.png) æ¾ç¤ºäºä¸¤ä¸ªå®¢æ·ç«¯åæ¶ååä¸è´ç©è½¦æ·»å 项ç®ãï¼å¦æè¿æ ·çä¾åè®©ä½ è§å¾æ 趣ï¼é£ä¹å¯ä»¥æ³è±¡ä¸ä¸ä¸¤ä¸ªç©ºä¸äº¤é管å¶ååæ¶æé£æºæ·»å å°ä»ä»¬æ£å¨è·è¸ªçåºåãï¼æåï¼è´ç©è½¦æ¯ç©ºçãç¶å客æ·ç«¯åæ°æ®åºååºäºæ¬¡åå ¥ï¼ 1. 客æ·ç«¯ 1 å°ç奶å å ¥è´ç©è½¦ãè¿æ¯è¯¥é®ç第ä¸æ¬¡åå ¥ï¼æå¡å¨æååå¨äºå®å¹¶ä¸ºå ¶åé çæ¬å· 1ï¼æåå°å¼ä¸çæ¬å·ä¸èµ·åéç»å®¢æ·ç«¯ã 2. 客æ·ç«¯ 2 å°é¸¡èå å ¥è´ç©è½¦ï¼ä¸ç¥é客æ·ç«¯ 1 åæ¶æ·»å äºç奶ï¼å®¢æ·ç«¯ 2 认为å®ç鸡èæ¯è´ç©è½¦ä¸çå¯ä¸ç©åï¼ãæå¡å¨ä¸ºæ¤åå ¥åé çæ¬å· 2ï¼å¹¶å°é¸¡èåç奶åå¨ä¸ºä¸¤ä¸ªåç¬çå¼ãç¶åå®å°è¿ä¸¤ä¸ªå¼ **é½** è¿åç»å®¢æ·ç«¯ 2 ï¼å¹¶éä¸çæ¬å· 2ã 3. 客æ·ç«¯ 1 ä¸ç¥é客æ·ç«¯ 2 çåå ¥ï¼æ³è¦å°é¢ç²å å ¥è´ç©è½¦ï¼å æ¤è®¤ä¸ºå½åçè´ç©è½¦å 容åºè¯¥æ¯ [ç奶ï¼é¢ç²]ãå®å°æ¤å¼ä¸æå¡å¨å åå客æ·ç«¯ 1 æä¾ççæ¬å· 1 ä¸èµ·åéå°æå¡å¨ãæå¡å¨å¯ä»¥ä»çæ¬å·ä¸ç¥é [ç奶ï¼é¢ç²] çåå ¥åä»£äº [ç奶] çå åå¼ï¼ä½ä¸ [鸡è] çå¼æ¯ **并å** çãå æ¤ï¼æå¡å¨å°çæ¬å· 3 åé ç» [ç奶ï¼é¢ç²]ï¼è¦ççæ¬ 1 çå¼ [ç奶]ï¼ä½ä¿ççæ¬ 2 çå¼ [鸡è]ï¼å¹¶å°ææçå¼è¿åç»å®¢æ·ç«¯ 1ã 4. åæ¶ï¼å®¢æ·ç«¯ 2 æ³è¦å å ¥ç«è ¿ï¼ä¸ç¥é客æ·ç«¯ 1 ååå äºé¢ç²ã客æ·ç«¯ 2 å¨æè¿ä¸æ¬¡ååºä¸ä»æå¡å¨æ¶å°äºä¸¤ä¸ªå¼ [ç奶] å [鸡è]ï¼æ以客æ·ç«¯ 2 ç°å¨å并è¿äºå¼ï¼å¹¶æ·»å ç«è ¿å½¢æä¸ä¸ªæ°çå¼ [鸡èï¼ç奶ï¼ç«è ¿]ãå®å°è¿ä¸ªå¼åéå°æå¡å¨ï¼å¸¦çä¹åççæ¬å· 2 ãæå¡å¨æ£æµå°æ°å¼ä¼è¦ççæ¬ 2 çå¼ [鸡è]ï¼ä½æ°å¼ä¹ä¼ä¸çæ¬ 3 çå¼ [ç奶ï¼é¢ç²] **并å**ï¼æ以å©ä¸ç两个å¼æ¯çæ¬ 3 ç [ç奶ï¼é¢ç²]ï¼åçæ¬ 4 ç [鸡èï¼ç奶ï¼ç«è ¿]ã 5. æåï¼å®¢æ·ç«¯ 1 æ³è¦å å¹æ ¹ãå®ä¹åä»æå¡å¨æ¥æ¶å°äºçæ¬ 3 ç [ç奶ï¼é¢ç²] å [鸡è]ï¼æ以å®å并è¿äºï¼æ·»å å¹æ ¹ï¼å¹¶å°æç»å¼ [ç奶ï¼é¢ç²ï¼é¸¡èï¼å¹æ ¹] è¿åçæ¬å· 3 åå¾æå¡å¨ãè¿ä¼è¦ççæ¬ 3 çå¼ [ç奶ï¼é¢ç²]ï¼è¯·æ³¨æ [鸡è] å·²ç»å¨ä¸ä¸æ¥è¢«è¦çï¼ï¼ä½ä¸çæ¬ 4 çå¼ [鸡èï¼ç奶ï¼ç«è ¿] 并åï¼æ以æå¡å¨å°ä¿çè¿ä¸¤ä¸ªå¹¶åå¼ã ![](img/fig5-13.png) **å¾ 5-13 å¨åæ¶ç¼è¾è´ç©è½¦æ¶æè·ä¸¤ä¸ªå®¢æ·ç«¯ä¹é´çå æå ³ç³»ã** [å¾ 5-13](img/fig5-13.png) ä¸çæä½ä¹é´çæ°æ®æµå¦ [å¾ 5-14](img/fig5-14.png) æ示ãç®å¤´è¡¨ç¤ºåªä¸ªæä½åçå¨å ¶ä»æä½ä¹åï¼æå³çåé¢çæä½ç¥éæä¾èµäºè¾æ©çæä½ãå¨è¿ä¸ªä¾åä¸ï¼å®¢æ·ç«¯æ°¸è¿ä¸ä¼å®å ¨æ¿å°æå¡å¨ä¸çææ°æ°æ®ï¼å 为æ»æ¯æå¦ä¸ä¸ªæä½åæ¶è¿è¡ãä½æ¯æ§çæ¬çå¼æç»ä¼è¢«è¦çï¼å¹¶ä¸ä¸ä¼ä¸¢å¤±ä»»ä½åå ¥ã ![](img/fig5-14.png) **å¾ 5-14 å¾ 5-13 ä¸çå æä¾èµå ³ç³»å¾ã** 请注æï¼æå¡å¨å¯ä»¥åªéè¿æ¥ççæ¬å·æ¥ç¡®å®ä¸¤ä¸ªæä½æ¯å¦æ¯å¹¶åç ââ å®ä¸éè¦å¯¹å¼æ¬èº«è¿è¡è§£éï¼å æ¤è¯¥å¼å¯ä»¥æ¯ä»»ä½æ°æ®ç»æï¼ã该ç®æ³çå·¥ä½åçå¦ä¸ï¼ * æå¡å¨ä¸ºæ¯ä¸ªé®ç»´æ¤ä¸ä¸ªçæ¬å·ï¼æ¯æ¬¡åå ¥è¯¥é®æ¶é½éå¢çæ¬å·ï¼å¹¶å°æ°çæ¬å·ä¸åå ¥çå¼ä¸èµ·åå¨ã * å½å®¢æ·ç«¯è¯»åé®æ¶ï¼æå¡å¨å°è¿åæææªè¦ççå¼ä»¥åææ°ççæ¬å·ã客æ·ç«¯å¨åå ¥åå¿ é¡»å 读åã * å½å®¢æ·ç«¯åå ¥é®æ¶ï¼å¿ é¡»å å«ä¹å读åççæ¬å·ï¼å¹¶ä¸å¿ é¡»å°ä¹å读åçææå¼å并å¨ä¸èµ·ï¼é对åå ¥è¯·æ±çååºå¯ä»¥å读å请æ±ä¸æ ·ï¼è¿åææå½åå¼ï¼è¿ä½¿å¾æ们å¯ä»¥åè´ç©è½¦ç¤ºä¾é£æ ·å°å¤ä¸ªåå ¥ä¸²èèµ·æ¥ï¼ã * å½æå¡å¨æ¥æ¶å°å ·æç¹å®çæ¬å·çåå ¥æ¶ï¼å®å¯ä»¥è¦ç该çæ¬å·ææ´ä½çæ¬çææå¼ï¼å 为å®ç¥éå®ä»¬å·²ç»è¢«å并å°æ°çå¼ä¸ï¼ï¼ä½æ¯å®å¿ é¡»ç¨æ´é«ççæ¬å·æ¥ä¿åææå¼ï¼å 为è¿äºå¼ä¸æ£å¨è¿è¡çå ¶å®åå ¥æ¯å¹¶åçï¼ã å½ä¸ä¸ªåå ¥å å«åä¸æ¬¡è¯»åççæ¬å·æ¶ï¼å®ä¼åè¯æ们çåå ¥æ¯åºäºä¹åçåªä¸ç§ç¶æãå¦æå¨ä¸å å«çæ¬å·çæ åµä¸è¿è¡åæä½ï¼åä¸ææå ¶ä»åæä½å¹¶åï¼å æ¤å®ä¸ä¼è¦çä»»ä½å 容 ââ åªä¼å¨éåç读åä¸ä½ä¸ºå ¶ä¸ä¸ä¸ªå¼è¿åã #### å并并ååå ¥çå¼ è¿ç§ç®æ³å¯ä»¥ç¡®ä¿æ²¡ææ°æ®è¢«æ 声å°ä¸¢å¼ï¼ä½ä¸å¹¸çæ¯ï¼å®¢æ·ç«¯éè¦åä¸äºé¢å¤çå·¥ä½ï¼å®¢æ·ç«¯éåå¿ é¡»å并并ååå ¥çå¼ãRiak 称è¿äºå¹¶åå¼ä¸º **å å¼ï¼siblingsï¼**ã å并并åå¼ï¼æ¬è´¨ä¸æ¯ä¸å¤ä¸»å¤å¶ä¸çå²çªè§£å³é®é¢ç¸åï¼æ们å å讨论è¿ï¼è¯·åé â[å¤çåå ¥å²çª](#å¤çåå ¥å²çª)âï¼ãä¸ä¸ªç®åçæ¹æ³æ¯æ ¹æ®çæ¬å·ææ¶é´æ³ï¼æååå ¥èå©ï¼æ¥éæ©ä¸ä¸ªå¼ï¼ä½è¿æå³ç丢失æ°æ®ãæ以ï¼ä½ å¯è½éè¦å¨åºç¨ç¨åºä»£ç ä¸é¢å¤åäºæ´èªæçäºæ ã 以è´ç©è½¦ä¸ºä¾ï¼ä¸ç§åççå并å¼çæ¹æ³å°±æ¯å并éãå¨ [å¾ 5-14](img/fig5-14.png) ä¸ï¼æåç两个å å¼æ¯ [ç奶ï¼é¢ç²ï¼é¸¡èï¼å¹æ ¹] å [鸡èï¼ç奶ï¼ç«è ¿]ã注æç奶å鸡èè½ç¶åæ¶åºç°å¨ä¸¤ä¸ªå¹¶åå¼éï¼ä½ä»ä»¬æ¯ä¸ªåªè¢«åè¿ä¸æ¬¡ãå并çå¼å¯ä»¥æ¯ [ç奶ï¼é¢ç²ï¼é¸¡èï¼å¹æ ¹ï¼ç«è ¿]ï¼ä¸åæéå¤äºã ç¶èï¼å¦æä½ æ³è®©äººä»¬ä¹å¯ä»¥ä»ä»ä»¬çè´ç©è½¦ä¸ **移é¤** ä¸è¥¿ï¼èä¸æ¯ä» ä» æ·»å ä¸è¥¿ï¼é£ä¹æ并åå¼å并éå¯è½ä¸ä¼äº§çæ£ç¡®çç»æï¼å¦æä½ å并äºä¸¤ä¸ªå®¢æ·ç«¯çè´ç©è½¦ï¼å¹¶ä¸åªå¨å ¶ä¸ä¸ä¸ªå®¢æ·ç«¯éé¢ç§»é¤äºä¸ä¸ªé¡¹ç®ï¼é£ä¹è¢«ç§»é¤ç项ç®å°ä¼éæ°åºç°å¨è¿ä¸¤ä¸ªå®¢æ·ç«¯ç交éç»æä¸ã37ãã为äºé²æ¢è¿ä¸ªé®é¢ï¼è¦ç§»é¤ä¸ä¸ªé¡¹ç®æ¶ä¸è½ç®åå°ç´æ¥ä»æ°æ®åºä¸å é¤ï¼ç¸åï¼ç³»ç»å¿ é¡»çä¸ä¸ä¸ªå ·æéå½çæ¬å·çæ è®°ï¼ä»¥å¨å å¼å并æ¶è¡¨æ该项ç®å·²è¢«ç§»é¤ãè¿ç§å é¤æ 记被称为 **å¢ç¢ï¼tombstoneï¼**ï¼æ们ä¸ä¸æ¬¡çå°å¢ç¢æ¯å¨ â[æ£åç´¢å¼â](ch3.md#æ£åç´¢å¼) ç« èçæ¥å¿å缩é¨åï¼ã å 为å¨åºç¨ç¨åºä»£ç ä¸åå å¼å并æ¯å¤æä¸å®¹æåºéçï¼æ以æä¸äºæ°æ®ç»æ被设计åºæ¥ç¨äºèªå¨æ§è¡è¿ç§å并ï¼æ¯å¦å¨ â[èªå¨å²çªè§£å³](#èªå¨å²çªè§£å³)â ä¸è®¨è®ºè¿çé£äºã举ä¾æ¥è¯´ï¼Riak çæ°æ®ç±»åå°±æ¯æ使ç¨ç§°ä¸º CRDT ã38,39,55ãçè½ä»¥åçæ¹å¼èªå¨è¿è¡å å¼å并çæ°æ®ç»æ家æï¼å æ¬å¯¹ä¿çå é¤çæ¯æã #### çæ¬åé [å¾ 5-13](img/fig5-13.png) ä¸ç示ä¾åªä½¿ç¨äºä¸ä¸ªå¯æ¬ãå½æå¤ä¸ªå¯æ¬ä½å没æ主åºæ¶ï¼ç®æ³è¯¥å¦ä½ä¿®æ¹ï¼ [å¾ 5-13](img/fig5-13.png) 使ç¨å个çæ¬å·æ¥æè·æä½ä¹é´çä¾èµå ³ç³»ï¼ä½æ¯å½å¤ä¸ªå¯æ¬å¹¶åæ¥ååå ¥æ¶ï¼è¿æ¯ä¸å¤çãç¸åï¼é¤äºå¯¹æ¯ä¸ªé®ï¼æ们è¿éè¦å¯¹ **æ¯ä¸ªå¯æ¬** 使ç¨çæ¬å·ãæ¯ä¸ªå¯æ¬å¨å¤çåå ¥æ¶å¢å èªå·±ççæ¬å·ï¼å¹¶ä¸è·è¸ªä»å ¶ä»å¯æ¬ä¸çå°ççæ¬å·ãè¿ä¸ªä¿¡æ¯æåºäºè¦è¦çåªäºå¹¶åå¼ï¼ä»¥åè¦ä¿çåªäºå¹¶åå¼æå å¼å¼ã ææå¯æ¬ççæ¬å·éå称为 **çæ¬åéï¼version vectorï¼**ã56ããè¿ä¸ªæ³æ³çä¸äºåä½æ£å¨è¢«ä½¿ç¨ï¼ä½ææ趣çå¯è½æ¯å¨ Riak 2.0 ã58,59ãä¸ä½¿ç¨ç **è线çæ¬åéï¼dotted version vectorï¼**ã57ããæ们ä¸ä¼æ·±å ¥ç»èï¼ä½æ¯å®çå·¥ä½æ¹å¼ä¸æ们å¨è´ç©è½¦ç¤ºä¾ä¸çå°çé常ç¸ä¼¼ã ä¸ [å¾ 5-13](img/fig5-13.png) ä¸ççæ¬å·ä¸æ ·ï¼å½è¯»åå¼æ¶ï¼çæ¬åéä¼ä»æ°æ®åºå¯æ¬åéå°å®¢æ·ç«¯ï¼å¹¶ä¸éååå ¥å¼æ¶éè¦å°å ¶åéåæ°æ®åºãï¼Riak å°çæ¬åéç¼ç 为ä¸ä¸ªå符串ï¼å¹¶ç§°å ¶ä¸º **å æä¸ä¸æ**ï¼å³ causal contextï¼ãçæ¬åéå 许æ°æ®åºåºåè¦çåå ¥å并ååå ¥ã å¦å¤ï¼å°±åå¨å个å¯æ¬ä¸çæ åµä¸æ ·ï¼åºç¨ç¨åºå¯è½éè¦å并并åå¼ãçæ¬åéç»æè½å¤ç¡®ä¿ä»ä¸ä¸ªå¯æ¬è¯»å并éåååå°å¦ä¸ä¸ªå¯æ¬æ¯å®å ¨çãè¿æ ·åè½ç¶å¯è½ä¼å¨å ¶ä»å¯æ¬ä¸é¢å建æ°æ®ï¼ä½åªè¦è½æ£ç¡®å并就ä¸ä¼ä¸¢å¤±æ°æ®ã > #### çæ¬åéååéæ¶é > > çæ¬åéææ¶ä¹è¢«ç§°ä¸ºåéæ¶éï¼å³ä½¿å®ä»¬ä¸å®å ¨ç¸åãå ¶ä¸çå·®å«å¾å¾®å¦ ââ ç»è请åé åèèµæã57,60,61ããç®èè¨ä¹ï¼å¨æ¯è¾å¯æ¬çç¶ææ¶ï¼çæ¬åéææ¯æ£ç¡®çæ°æ®ç»æã ## æ¬ç« å°ç» å¨æ¬ç« ä¸ï¼æ们èå¯äºå¤å¶çé®é¢ãå¤å¶å¯ä»¥ç¨äºå 个ç®çï¼ * é«å¯ç¨æ§ å³ä½¿å¨ä¸å°æºå¨ï¼æå¤å°æºå¨ï¼ææ´ä¸ªæ°æ®ä¸å¿ï¼åæºçæ åµä¸ä¹è½ä¿æç³»ç»æ£å¸¸è¿è¡ * æå¼è¿æ¥çæä½ å 许åºç¨ç¨åºå¨ç½ç»ä¸ææ¶ç»§ç»å·¥ä½ * å»¶è¿ å°æ°æ®æ¾ç½®å¨å°çä¸è·ç¦»ç¨æ·è¾è¿çå°æ¹ï¼ä»¥ä¾¿ç¨æ·è½å¤æ´å¿«å°ä¸å ¶äº¤äº * å¯ä¼¸ç¼©æ§ éè¿å¨å¯æ¬ä¸è¯»ï¼è½å¤å¤çæ¯åæºæ´å¤§ç读åé 尽管æ¯ä¸ä¸ªç®åçç®æ - å¨å å°æºå¨ä¸ä¿çç¸åæ°æ®çå¯æ¬ï¼ä½å¤å¶å´æ¯ä¸ä¸ªé常æ£æçé®é¢ãå®éè¦ä»ç»èè并ååææå¯è½åºéçäºæ ï¼å¹¶å¤çè¿äºæ éçåæãè³å°ï¼æ们éè¦å¤çä¸å¯ç¨çèç¹åç½ç»ä¸æï¼è¿è¿ä¸å æ¬æ´éè½çæ éï¼ä¾å¦ç±äºè½¯ä»¶é误导è´çéé»æ°æ®æåï¼ã æ们讨论äºå¤å¶çä¸ç§ä¸»è¦æ¹æ³ï¼ * å主å¤å¶ 客æ·ç«¯å°ææåå ¥æä½åéå°å个èç¹ï¼ä¸»åºï¼ï¼è¯¥èç¹å°æ°æ®æ´æ¹äºä»¶æµåéå°å ¶ä»å¯æ¬ï¼ä»åºï¼ã读åå¯ä»¥å¨ä»»ä½å¯æ¬ä¸æ§è¡ï¼ä½ä»åºç读åç»æå¯è½æ¯éæ§çã * å¤ä¸»å¤å¶ 客æ·ç«¯å°æ¯ä¸ªåå ¥åéå°å 个主åºèç¹ä¹ä¸ï¼å ¶ä¸ä»»ä½ä¸ä¸ªä¸»åºé½å¯ä»¥æ¥ååå ¥ã主åºå°æ°æ®æ´æ¹äºä»¶æµåéç»å½¼æ¤ä»¥åä»»ä½ä»åºèç¹ã * æ 主å¤å¶ 客æ·ç«¯å°æ¯ä¸ªåå ¥åéå°å 个èç¹ï¼å¹¶ä»å¤ä¸ªèç¹å¹¶è¡è¯»åï¼ä»¥æ£æµåçº æ£å ·æéæ§æ°æ®çèç¹ã æ¯ç§æ¹æ³é½æä¼ç¹å缺ç¹ãå主å¤å¶æ¯é常æµè¡çï¼å 为å®å¾å®¹æç解ï¼ä¸éè¦æ å¿å²çªè§£å³ãå¨åºç°æ éèç¹ãç½ç»ä¸æå延è¿å³°å¼çæ åµä¸ï¼å¤ä¸»å¤å¶åæ 主å¤å¶å¯ä»¥æ´å å¥å£®ï¼å ¶ä»£ä»·æ¯é¾ä»¥æ¨ç并ä¸ä» æä¾é常弱çä¸è´æ§ä¿è¯ã å¤å¶å¯ä»¥æ¯åæ¥çï¼ä¹å¯ä»¥æ¯å¼æ¥çï¼è¿å¨åçæ éæ¶å¯¹ç³»ç»è¡ä¸ºææ·±è¿çå½±åã尽管å¨ç³»ç»è¿è¡å¹³ç¨³æ¶å¼æ¥å¤å¶é度å¾å¿«ï¼ä½æ¯è¦å¼æ¸ æ¥å¨å¤å¶å»¶è¿å¢å åæå¡å¨æ éæ¶ä¼åçä»ä¹ï¼è¿ä¸ç¹å¾éè¦ãå¦æ主åºå¤±è´¥åä½ å°ä¸ä¸ªå¼æ¥æ´æ°çä»åºæå为æ°ç主åºï¼é£ä¹æè¿æ交çæ°æ®å¯è½ä¼ä¸¢å¤±ã æ们ç 究äºä¸äºå¯è½ç±å¤å¶å»¶è¿å¼èµ·çå¥æªæåºï¼æ们ä¹è®¨è®ºäºä¸äºæå©äºå³å®åºç¨ç¨åºå¨å¤å¶å»¶è¿æ¶çè¡ä¸ºçä¸è´æ§æ¨¡åï¼ * åå读ä¸è´æ§ ç¨æ·åºè¯¥æ»æ¯è½çå°èªå·±æ交çæ°æ®ã * åè°è¯» ç¨æ·å¨çå°æ个æ¶é´ç¹çæ°æ®åï¼ä»ä»¬ä¸åºè¯¥åçå°è¯¥æ°æ®å¨æ´æ©æ¶é´ç¹çæ åµã * ä¸è´åç¼è¯» ç¨æ·åºè¯¥çå°æ°æ®å¤äºä¸ç§å ·æå ææä¹çç¶æï¼ä¾å¦ï¼ææ£ç¡®ç顺åºçå°ä¸ä¸ªé®é¢å对åºçåçã æåï¼æ们讨论äºå¤ä¸»å¤å¶åæ 主å¤å¶æ¹æ³æåºæç并åé®é¢ï¼å 为ä»ä»¬å 许å¤ä¸ªåå ¥å¹¶ååçï¼è¿å¯è½ä¼å¯¼è´å²çªãæ们ç 究äºä¸ä¸ªæ°æ®åºå¯ä»¥ä½¿ç¨çç®æ³æ¥ç¡®å®ä¸ä¸ªæä½æ¯å¦åçå¨å¦ä¸ä¸ªæä½ä¹åï¼æè å®ä»¬æ¯å¦å¹¶ååçãæ们è¿è°å°äºéè¿å并并åæ´æ°æ¥è§£å³å²çªçæ¹æ³ã å¨ä¸ä¸ç« ä¸ï¼æ们å°ç»§ç»èå¯æ°æ®åå¸å¨å¤å°æºå¨é´çå¦ä¸ç§ä¸åäº **å¤å¶** çå½¢å¼ï¼å°å¤§æ°æ®éåå²æ **ååº**ã ## åèæç® 1. Bruce G. Lindsay, Patricia Griffiths Selinger, C. Galtieri, et al.: â[Notes on Distributed Databases](http://domino.research.ibm.com/library/cyberdig.nsf/papers/A776EC17FC2FCE73852579F100578964/$File/RJ2571.pdf),â IBM Research, Research Report RJ2571(33471), July 1979. 1. â[Oracle Active Data Guard Real-Time Data Protection and Availability](http://www.oracle.com/technetwork/database/availability/active-data-guard-wp-12c-1896127.pdf),â Oracle White Paper, June 2013. 1. â[AlwaysOn Availability Groups](http://msdn.microsoft.com/en-us/library/hh510230.aspx),â in *SQL Server Books Online*, Microsoft, 2012. 1. Lin Qiao, Kapil Surlaker, Shirshanka Das, et al.: â[On Brewing Fresh Espresso: LinkedInâs Distributed Data Serving Platform](http://www.slideshare.net/amywtang/espresso-20952131),â at *ACM International Conference on Management of Data* (SIGMOD), June 2013. 1. Jun Rao: â[Intra-Cluster Replication for Apache Kafka](http://www.slideshare.net/junrao/kafka-replication-apachecon2013),â at *ApacheCon North America*, February 2013. 1. â[Highly Available Queues](https://www.rabbitmq.com/ha.html),â in *RabbitMQ Server Documentation*, Pivotal Software, Inc., 2014. 1. Yoshinori Matsunobu: â[Semi-Synchronous Replication at Facebook](http://yoshinorimatsunobu.blogspot.co.uk/2014/04/semi-synchronous-replication-at-facebook.html),â *yoshinorimatsunobu.blogspot.co.uk*, April 1, 2014. 1. Robbert van Renesse and Fred B. Schneider: â[Chain Replication for Supporting High Throughput and Availability](http://static.usenix.org/legacy/events/osdi04/tech/full_papers/renesse/renesse.pdf),â at *6th USENIX Symposium on Operating System Design and Implementation* (OSDI), December 2004. 1. Jeff Terrace and Michael J. Freedman: â[Object Storage on CRAQ: High-Throughput Chain Replication for Read-Mostly Workloads](https://www.usenix.org/legacy/event/usenix09/tech/full_papers/terrace/terrace.pdf),â at *USENIX Annual Technical Conference* (ATC), June 2009. 1. Brad Calder, Ju Wang, Aaron Ogus, et al.: â[Windows Azure Storage: A Highly Available Cloud Storage Service with Strong Consistency](http://sigops.org/sosp/sosp11/current/2011-Cascais/printable/11-calder.pdf),â at *23rd ACM Symposium on Operating Systems Principles* (SOSP), October 2011. 1. Andrew Wang: â[Windows Azure Storage](http://umbrant.com/blog/2016/windows_azure_storage.html),â *umbrant.com*, February 4, 2016. 1. â[Percona Xtrabackup - Documentation](https://www.percona.com/doc/percona-xtrabackup/2.1/index.html),â Percona LLC, 2014. 1. Jesse Newland: â[GitHub Availability This Week](https://github.com/blog/1261-github-availability-this-week),â *github.com*, September 14, 2012. 1. Mark Imbriaco: â[Downtime Last Saturday](https://github.com/blog/1364-downtime-last-saturday),â *github.com*, December 26, 2012. 1. John Hugg: â[âAll inâ with Determinism for Performance and Testing in Distributed Systems](https://www.youtube.com/watch?v=gJRj3vJL4wE),â at *Strange Loop*, September 2015. 1. Amit Kapila: â[WAL Internals of PostgreSQL](http://www.pgcon.org/2012/schedule/attachments/258_212_Internals%20Of%20PostgreSQL%20Wal.pdf),â at *PostgreSQL Conference* (PGCon), May 2012. 1. [*MySQL Internals Manual*](http://dev.mysql.com/doc/internals/en/index.html). Oracle, 2014. 1. Yogeshwer Sharma, Philippe Ajoux, Petchean Ang, et al.: â[Wormhole: Reliable Pub-Sub to Support Geo-Replicated Internet Services](https://www.usenix.org/system/files/conference/nsdi15/nsdi15-paper-sharma.pdf),â at *12th USENIX Symposium on Networked Systems Design and Implementation* (NSDI), May 2015. 1. â[Oracle GoldenGate 12c: Real-Time Access to Real-Time Information](http://www.oracle.com/us/products/middleware/data-integration/oracle-goldengate-realtime-access-2031152.pdf),â Oracle White Paper, October 2013. 1. Shirshanka Das, Chavdar Botev, Kapil Surlaker, et al.: â[All Aboard the Databus!](http://www.socc2012.org/s18-das.pdf),â at *ACM Symposium on Cloud Computing* (SoCC), October 2012. 1. Greg Sabino Mullane: â[Version 5 of Bucardo Database Replication System](http://blog.endpoint.com/2014/06/bucardo-5-multimaster-postgres-released.html),â *blog.endpoint.com*, June 23, 2014. 1. Werner Vogels: â[Eventually Consistent](http://queue.acm.org/detail.cfm?id=1466448),â *ACM Queue*, volume 6, number 6, pages 14â19, October 2008. [doi:10.1145/1466443.1466448](http://dx.doi.org/10.1145/1466443.1466448) 1. Douglas B. Terry: â[Replicated Data Consistency Explained Through Baseball](http://research.microsoft.com/pubs/157411/ConsistencyAndBaseballReport.pdf),â Microsoft Research, Technical Report MSR-TR-2011-137, October 2011. 1. Douglas B. Terry, Alan J. Demers, Karin Petersen, et al.: â[Session Guarantees for Weakly Consistent Replicated Data](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.71.2269&rep=rep1&type=pdf),â at *3rd International Conference on Parallel and Distributed Information Systems* (PDIS), September 1994. [doi:10.1109/PDIS.1994.331722](http://dx.doi.org/10.1109/PDIS.1994.331722) 1. Terry Pratchett: *Reaper Man: A Discworld Novel*. Victor Gollancz, 1991. ISBN: 978-0-575-04979-6 1. â[Tungsten Replicator](http://tungsten-replicator.org/),â Continuent, Inc., 2014. 1. â[BDR 0.10.0 Documentation](http://bdr-project.org/docs/next/index.html),â The PostgreSQL Global Development Group, *bdr-project.org*, 2015. 1. Robert Hodges: â[If You *Must* Deploy Multi-Master Replication, Read This First](http://scale-out-blog.blogspot.co.uk/2012/04/if-you-must-deploy-multi-master.html),â *scale-out-blog.blogspot.co.uk*, March 30, 2012. 1. J. Chris Anderson, Jan Lehnardt, and Noah Slater: *CouchDB: The Definitive Guide*. O'Reilly Media, 2010. ISBN: 978-0-596-15589-6 1. AppJet, Inc.: â[Etherpad and EasySync Technical Manual](https://github.com/ether/etherpad-lite/blob/e2ce9dc/doc/easysync/easysync-full-description.pdf),â *github.com*, March 26, 2011. 1. John Day-Richter: â[Whatâs Different About the New Google Docs: Making Collaboration Fast](http://googledrive.blogspot.com/2010/09/whats-different-about-new-google-docs.html),â *googledrive.blogspot.com*, 23 September 2010. 1. Martin Kleppmann and Alastair R. Beresford: â[A Conflict-Free Replicated JSON Datatype](http://arxiv.org/abs/1608.03960),â arXiv:1608.03960, August 13, 2016. 1. Frazer Clement: â[Eventual Consistency â Detecting Conflicts](http://messagepassing.blogspot.co.uk/2011/10/eventual-consistency-detecting.html),â *messagepassing.blogspot.co.uk*, October 20, 2011. 1. Robert Hodges: â[State of the Art for MySQL Multi-Master Replication](https://www.percona.com/live/mysql-conference-2013/sessions/state-art-mysql-multi-master-replication),â at *Percona Live: MySQL Conference & Expo*, April 2013. 1. John Daily: â[Clocks Are Bad, or, Welcome to the Wonderful World of Distributed Systems](http://basho.com/clocks-are-bad-or-welcome-to-distributed-systems/),â *basho.com*, November 12, 2013. 1. Riley Berton: â[Is Bi-Directional Replication (BDR) in Postgres Transactional?](http://sdf.org/~riley/blog/2016/01/04/is-bi-directional-replication-bdr-in-postgres-transactional/),â *sdf.org*, January 4, 2016. 1. Giuseppe DeCandia, Deniz Hastorun, Madan Jampani, et al.: â[Dynamo: Amazon's Highly Available Key-Value Store](http://www.allthingsdistributed.com/files/amazon-dynamo-sosp2007.pdf),â at *21st ACM Symposium on Operating Systems Principles* (SOSP), October 2007. 1. Marc Shapiro, Nuno Preguiça, Carlos Baquero, and Marek Zawirski: â[A Comprehensive Study of Convergent and Commutative Replicated Data Types](http://hal.inria.fr/inria-00555588/),â INRIA Research Report no. 7506, January 2011. 1. Sam Elliott: â[CRDTs: An UPDATE (or Maybe Just a PUT)](https://speakerdeck.com/lenary/crdts-an-update-or-just-a-put),â at *RICON West*, October 2013. 1. Russell Brown: â[A Bluffers Guide to CRDTs in Riak](https://gist.github.com/russelldb/f92f44bdfb619e089a4d),â *gist.github.com*, October 28, 2013. 1. Benjamin Farinier, Thomas Gazagnaire, and Anil Madhavapeddy: â[Mergeable Persistent Data Structures](http://gazagnaire.org/pub/FGM15.pdf),â at *26es Journées Francophones des Langages Applicatifs* (JFLA), January 2015. 1. Chengzheng Sun and Clarence Ellis: â[Operational Transformation in Real-Time Group Editors: Issues, Algorithms, and Achievements](http://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.53.933&rep=rep1&type=pdf),â at *ACM Conference on Computer Supported Cooperative Work* (CSCW), November 1998. 1. Lars Hofhansl: â[HBASE-7709: Infinite Loop Possible in Master/Master Replication](https://issues.apache.org/jira/browse/HBASE-7709),â *issues.apache.org*, January 29, 2013. 1. David K. Gifford: â[Weighted Voting for Replicated Data](http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.84.7698),â at *7th ACM Symposium on Operating Systems Principles* (SOSP), December 1979. [doi:10.1145/800215.806583](http://dx.doi.org/10.1145/800215.806583) 1. Heidi Howard, Dahlia Malkhi, and Alexander Spiegelman: â[Flexible Paxos: Quorum Intersection Revisited](https://arxiv.org/abs/1608.06696),â *arXiv:1608.06696*, August 24, 2016. 1. Joseph Blomstedt: â[Re: Absolute Consistency](http://lists.basho.com/pipermail/riak-users_lists.basho.com/2012-January/007157.html),â email to *riak-users* mailing list, *lists.basho.com*, January 11, 2012. 1. Joseph Blomstedt: â[Bringing Consistency to Riak](https://vimeo.com/51973001),â at *RICON West*, October 2012. 1. Peter Bailis, Shivaram Venkataraman, Michael J. Franklin, et al.: â[Quantifying Eventual Consistency with PBS](http://www.bailis.org/papers/pbs-cacm2014.pdf),â *Communications of the ACM*, volume 57, number 8, pages 93â102, August 2014. [doi:10.1145/2632792](http://dx.doi.org/10.1145/2632792) 1. Jonathan Ellis: â[Modern Hinted Handoff](http://www.datastax.com/dev/blog/modern-hinted-handoff),â *datastax.com*, December 11, 2012. 1. â[Project Voldemort Wiki](https://github.com/voldemort/voldemort/wiki),â *github.com*, 2013. 1. â[Apache Cassandra 2.0 Documentation](http://www.datastax.com/documentation/cassandra/2.0/index.html),â DataStax, Inc., 2014. 1. â[Riak Enterprise: Multi-Datacenter Replication](http://basho.com/assets/MultiDatacenter_Replication.pdf).â Technical whitepaper, Basho Technologies, Inc., September 2014. 1. Jonathan Ellis: â[Why Cassandra Doesn't Need Vector Clocks](http://www.datastax.com/dev/blog/why-cassandra-doesnt-need-vector-clocks),â *datastax.com*, September 2, 2013. 1. Leslie Lamport: â[Time, Clocks, and the Ordering of Events in a Distributed System](http://research.microsoft.com/en-US/um/people/Lamport/pubs/time-clocks.pdf),â *Communications of the ACM*, volume 21, number 7, pages 558â565, July 1978. [doi:10.1145/359545.359563](http://dx.doi.org/10.1145/359545.359563) 1. Joel Jacobson: â[Riak 2.0: Data Types](http://blog.joeljacobson.com/riak-2-0-data-types/),â *blog.joeljacobson.com*, March 23, 2014. 1. D. Stott Parker Jr., Gerald J. Popek, Gerard Rudisin, et al.: â[Detection of Mutual Inconsistency in Distributed Systems](http://zoo.cs.yale.edu/classes/cs426/2013/bib/parker83detection.pdf),â *IEEE Transactions on Software Engineering*, volume 9, number 3, pages 240â247, May 1983. [doi:10.1109/TSE.1983.236733](http://dx.doi.org/10.1109/TSE.1983.236733) 1. Nuno Preguiça, Carlos Baquero, Paulo Sérgio Almeida, et al.: â[Dotted Version Vectors: Logical Clocks for Optimistic Replication](http://arxiv.org/pdf/1011.5808v1.pdf),â arXiv:1011.5808, November 26, 2010. 1. Sean Cribbs: â[A Brief History of Time in Riak](https://www.youtube.com/watch?v=HHkKPdOi-ZU),â at *RICON*, October 2014. 1. Russell Brown: â[Vector Clocks Revisited Part 2: Dotted Version Vectors](http://basho.com/posts/technical/vector-clocks-revisited-part-2-dotted-version-vectors/),â *basho.com*, November 10, 2015. 1. Carlos Baquero: â[Version Vectors Are Not Vector Clocks](https://haslab.wordpress.com/2011/07/08/version-vectors-are-not-vector-clocks/),â *haslab.wordpress.com*, July 8, 2011. 1. Reinhard Schwarz and Friedemann Mattern: â[Detecting Causal Relationships in Distributed Computations: In Search of the Holy Grail](http://dcg.ethz.ch/lectures/hs08/seminar/papers/mattern4.pdf),â *Distributed Computing*, volume 7, number 3, pages 149â174, March 1994. [doi:10.1007/BF02277859](http://dx.doi.org/10.1007/BF02277859) -------- | ä¸ä¸ç« | ç®å½ | ä¸ä¸ç« | | :--------------------------------: | :-----------------------------: | :--------------------: | | [第äºé¨åï¼åå¸å¼æ°æ®](part-ii.md) | [设计æ°æ®å¯éååºç¨](README.md) | [第å ç« ï¼ååº](ch6.md) |