> * åæå°åï¼[github.com/donnemartin/system-design-primer](https://github.com/donnemartin/system-design-primer)
> * è¯æåºèªï¼[æéç¿»è¯è®¡å](https://github.com/xitu/gold-miner)
> * è¯è
ï¼[XatMassacrE](https://github.com/XatMassacrE)ã[L9m](https://github.com/L9m)ã[Airmacho](https://github.com/Airmacho)ã[xiaoyusilen](https://github.com/xiaoyusilen)ã[jifaxu](https://github.com/jifaxu)ã[æ ¹å·ä¸](https://github.com/sqrthree)
> * è¿ä¸ª [é¾æ¥](https://github.com/xitu/system-design-primer/compare/master...donnemartin:master) ç¨æ¥æ¥çæ¬ç¿»è¯ä¸è±æçæ¯å¦æå·®å«ï¼å¦æä½ æ²¡æçå° README.md åçååï¼é£å°±æå³çè¿ä»½ç¿»è¯ææ¡£æ¯ææ°çï¼ã
*[English](README.md) â [æ¥æ¬èª](README-ja.md) â [ç®ä½ä¸æ](README-zh-Hans.md) â [ç¹é«ä¸æ](README-zh-TW.md) | [اÙعÙرÙبÙÙÙÙØ©â](https://github.com/donnemartin/system-design-primer/issues/170) â [বাà¦à¦²à¦¾](https://github.com/donnemartin/system-design-primer/issues/220) â [Português do Brasil](https://github.com/donnemartin/system-design-primer/issues/40) â [Deutsch](https://github.com/donnemartin/system-design-primer/issues/186) â [ελληνικά](https://github.com/donnemartin/system-design-primer/issues/130) â [×¢×ר×ת](https://github.com/donnemartin/system-design-primer/issues/272) â [Italiano](https://github.com/donnemartin/system-design-primer/issues/104) â [íêµì´](https://github.com/donnemartin/system-design-primer/issues/102) â [ÙارسÛ](https://github.com/donnemartin/system-design-primer/issues/110) â [Polski](https://github.com/donnemartin/system-design-primer/issues/68) â [ÑÑÑÑкий ÑзÑк](https://github.com/donnemartin/system-design-primer/issues/87) â [Español](https://github.com/donnemartin/system-design-primer/issues/136) â [ภาษาà¹à¸à¸¢](https://github.com/donnemartin/system-design-primer/issues/187) â [Türkçe](https://github.com/donnemartin/system-design-primer/issues/39) â [tiếng Viá»t](https://github.com/donnemartin/system-design-primer/issues/127) â [Français](https://github.com/donnemartin/system-design-primer/issues/250) | [Add Translation](https://github.com/donnemartin/system-design-primer/issues/28)*
# ç³»ç»è®¾è®¡å
¥é¨
## ç®ç
> å¦ä¹ å¦ä½è®¾è®¡å¤§åç³»ç»ã
>
> 为系ç»è®¾è®¡çé¢è¯ååå¤ã
### å¦ä¹ å¦ä½è®¾è®¡å¤§åç³»ç»
å¦ä¹ å¦ä½è®¾è®¡å¯æ©å±çç³»ç»å°ä¼æå©äºä½ æ为ä¸ä¸ªæ´å¥½çå·¥ç¨å¸ã
ç³»ç»è®¾è®¡æ¯ä¸ä¸ªå¾å®½æ³çè¯é¢ãå¨äºèç½ä¸ï¼**å
³äºç³»ç»è®¾è®¡ååçèµæºä¹æ¯å¤å¦çæ¯ã**
è¿ä¸ªä»åºå°±æ¯è¿äºèµæºç**ç»ç»æ¶é**ï¼å®å¯ä»¥å¸®å©ä½ å¦ä¹ å¦ä½æ建å¯æ©å±çç³»ç»ã
### ä»å¼æºç¤¾åºå¦ä¹
è¿æ¯ä¸ä¸ªä¸ææ´æ°çå¼æºé¡¹ç®çåæççæ¬ã
欢è¿[è´¡ç®](#è´¡ç®)ï¼
### 为系ç»è®¾è®¡çé¢è¯ååå¤
å¨å¾å¤ç§æå
¬å¸ä¸ï¼é¤äºä»£ç é¢è¯ï¼ç³»ç»è®¾è®¡ä¹æ¯**ææ¯é¢è¯è¿ç¨**ä¸çä¸ä¸ª**å¿
è¦ç¯è**ã
**å®è·µå¸¸è§çç³»ç»è®¾è®¡é¢è¯é¢**并ä¸æä½ ççæ¡å**ä¾åç解ç**è¿è¡**对ç
§**ï¼è®¨è®ºï¼ä»£ç åå¾è¡¨ã
é¢è¯åå¤çå
¶ä»ä¸»é¢ï¼
* [å¦ä¹ æå¼](#å¦ä¹ æå¼)
* [å¦ä½å¤çä¸ä¸ªç³»ç»è®¾è®¡çé¢è¯é¢](#å¦ä½å¤çä¸ä¸ªç³»ç»è®¾è®¡çé¢è¯é¢)
* [ç³»ç»è®¾è®¡çé¢è¯é¢ï¼**å«è§£ç**](#ç³»ç»è®¾è®¡çé¢è¯é¢å解ç)
* [é¢å对象设计çé¢è¯é¢ï¼**å«è§£ç**](#é¢å对象设计çé¢è¯é®é¢å解ç)
* [å
¶å®çç³»ç»è®¾è®¡é¢è¯é¢](#å
¶å®çç³»ç»è®¾è®¡é¢è¯é¢)
## æ½è®¤å¡
è¿éæä¾ç[æ½è®¤å¡å ](https://apps.ankiweb.net/)使ç¨é´ééå¤çæ¹æ³ï¼å¸®å©ä½ è®°å¿å
³é®çç³»ç»è®¾è®¡æ¦å¿µã
* [ç³»ç»è®¾è®¡çå¡å ](resources/flash_cards/System%20Design.apkg)
* [ç³»ç»è®¾è®¡çç»ä¹ å¡å ](resources/flash_cards/System%20Design%20Exercises.apkg)
* [é¢å对象设计çç»ä¹ å¡å ](resources/flash_cards/OO%20Design.apkg)
éæ¶éå°é½å¯ä½¿ç¨ã
### 代ç èµæºï¼äºå¨å¼ç¼ç¨ææ
ä½ æ£å¨å¯»æ¾èµæºä»¥åå¤[**ç¼ç¨é¢è¯**](https://github.com/donnemartin/interactive-coding-challenges)åï¼
请æ¥çæ们çå§å¦¹ä»åº[**äºå¨å¼ç¼ç¨ææ**](https://github.com/donnemartin/interactive-coding-challenges)ï¼å
¶ä¸å
å«äºä¸ä¸ªé¢å¤çæ½è®¤å¡å ï¼
* [代ç å¡å ](https://github.com/donnemartin/interactive-coding-challenges/tree/master/anki_cards/Coding.apkg)
## è´¡ç®
> ä»ç¤¾åºä¸å¦ä¹ ã
欢è¿æ交 PR æä¾å¸®å©ï¼
* ä¿®å¤é误
* å®åç« è
* æ·»å ç« è
* [帮å©ç¿»è¯](https://github.com/donnemartin/system-design-primer/issues/28)
ä¸äºè¿éè¦å®åçå
容æ¾å¨äº[æ£å¨å®åä¸](#æ£å¨å®åä¸)ã
请æ¥ç[è´¡ç®æå](CONTRIBUTING.md)ã
## ç³»ç»è®¾è®¡ä¸»é¢çç´¢å¼
> åç§ç³»ç»è®¾è®¡ä¸»é¢çæè¦ï¼å
æ¬ä¼ç¹å缺ç¹ã**æ¯ä¸ä¸ªä¸»é¢é½é¢ä¸´çåèåæè¡¡**ã
>
> æ¯ä¸ªç« èé½å
å«çæ´å¤çèµæºçé¾æ¥ã
* [ç³»ç»è®¾è®¡ä¸»é¢ï¼ä»è¿éå¼å§](#ç³»ç»è®¾è®¡ä¸»é¢ä»è¿éå¼å§)
* [第ä¸æ¥ï¼å顾å¯æ©å±æ§çè§é¢è®²åº§](#第ä¸æ¥å顾å¯æ©å±æ§scalabilityçè§é¢è®²åº§)
* [第äºæ¥ï¼å顾å¯æ©å±æ§çæç« ](#第äºæ¥å顾å¯æ©å±æ§æç« )
* [æ¥ä¸æ¥çæ¥éª¤](#æ¥ä¸æ¥çæ¥éª¤)
* [æ§è½ä¸æå±æ§](#æ§è½ä¸å¯æ©å±æ§)
* [延è¿ä¸ååé](#延è¿ä¸ååé)
* [å¯ç¨æ§ä¸ä¸è´æ§](#å¯ç¨æ§ä¸ä¸è´æ§)
* [CAP ç论](#cap-ç论)
* [CP - ä¸è´æ§åååºå®¹éæ§](#cp--ä¸è´æ§åååºå®¹éæ§)
* [AP - å¯ç¨æ§åååºå®¹éæ§](#ap--å¯ç¨æ§ä¸ååºå®¹éæ§)
* [ä¸è´æ¨¡å¼](#ä¸è´æ§æ¨¡å¼)
* [å¼±ä¸è´æ§](#å¼±ä¸è´æ§)
* [æç»ä¸è´æ§](#æç»ä¸è´æ§)
* [强ä¸è´æ§](#强ä¸è´æ§)
* [å¯ç¨æ¨¡å¼](#å¯ç¨æ§æ¨¡å¼)
* [æ
éåæ¢](#æ
éåæ¢)
* [å¤å¶](#å¤å¶)
* [ååç³»ç»](#ååç³»ç»)
* [CDN](#å
容ååç½ç»cdn)
* [CDN æ¨é](#cdn-æ¨épush)
* [CDN æå](#cdn-æåpull)
* [è´è½½åè¡¡å¨](#è´è½½åè¡¡å¨)
* [å·¥ä½å°å¤ç¨åæ¢ï¼Active-passiveï¼](#å·¥ä½å°å¤ç¨åæ¢active-passive)
* [åå·¥ä½åæ¢ï¼Active-activeï¼](#åå·¥ä½åæ¢active-active)
* [åå±è´è½½åè¡¡](#åå±è´è½½åè¡¡)
* [ä¸å±è´è½½åè¡¡](#ä¸å±è´è½½åè¡¡å¨)
* [æ°´å¹³æ©å±](#æ°´å¹³æ©å±)
* [åå代çï¼web æå¡å¨ï¼](#åå代çweb-æå¡å¨)
* [è´è½½åè¡¡ä¸åå代ç](#è´è½½åè¡¡å¨ä¸åå代ç)
* [åºç¨å±](#åºç¨å±)
* [å¾®æå¡](#å¾®æå¡)
* [æå¡åç°](#æå¡åç°)
* [æ°æ®åº](#æ°æ®åº)
* [å
³ç³»åæ°æ®åºç®¡çç³»ç»ï¼RDBMSï¼](#å
³ç³»åæ°æ®åºç®¡çç³»ç»rdbms)
* [Master-slave å¤å¶é](#主ä»å¤å¶)
* [Master-master å¤å¶é](#主主å¤å¶)
* [èå](#èå)
* [åç](#åç)
* [éè§èå](#éè§èå)
* [SQL è°ä¼](#sql-è°ä¼)
* [NoSQL](#nosql)
* [Key-value åå¨](#é®-å¼åå¨)
* [ææ¡£åå¨](#æ档类ååå¨)
* [宽ååå¨](#åååå¨)
* [å¾æ°æ®åº](#å¾æ°æ®åº)
* [SQL è¿æ¯ NoSQL](#sql-è¿æ¯-nosql)
* [ç¼å](#ç¼å)
* [客æ·ç«¯ç¼å](#客æ·ç«¯ç¼å)
* [CDN ç¼å](#cdn-ç¼å)
* [Web æå¡å¨ç¼å](#web-æå¡å¨ç¼å)
* [æ°æ®åºç¼å](#æ°æ®åºç¼å)
* [åºç¨ç¼å](#åºç¨ç¼å)
* [æ°æ®åºæ¥è¯¢çº§å«çç¼å](#æ°æ®åºæ¥è¯¢çº§å«çç¼å)
* [对象级å«çç¼å](#对象级å«çç¼å)
* [ä½æ¶æ´æ°ç¼å](#ä½æ¶æ´æ°ç¼å)
* [ç¼å模å¼](#ç¼å模å¼)
* [ç´å模å¼](#ç´å模å¼)
* [åå模å¼](#åå模å¼)
* [å·æ°](#å·æ°)
* [å¼æ¥](#å¼æ¥)
* [æ¶æ¯éå](#æ¶æ¯éå)
* [ä»»å¡éå](#ä»»å¡éå)
* [èåæºå¶](#èå)
* [é讯](#é讯)
* [ä¼ è¾æ§å¶åè®®ï¼TCPï¼](#ä¼ è¾æ§å¶åè®®tcp)
* [ç¨æ·æ°æ®æ¥åè®®ï¼UDPï¼](#ç¨æ·æ°æ®æ¥åè®®udp)
* [è¿ç¨æ§å¶è°ç¨åè®®ï¼RPCï¼](#è¿ç¨è¿ç¨è°ç¨åè®®rpc)
* [表述æ§ç¶æ转移ï¼RESTï¼](#表述æ§ç¶æ转移rest)
* [å®å
¨](#å®å
¨)
* [éå½](#éå½)
* [2 ç次æ¹è¡¨](#2-ç次æ¹è¡¨)
* [æ¯ä¸ªç¨åºåé½åºè¯¥ç¥éç延è¿æ°](#æ¯ä¸ªç¨åºåé½åºè¯¥ç¥éç延è¿æ°)
* [å
¶å®çç³»ç»è®¾è®¡é¢è¯é¢](#å
¶å®çç³»ç»è®¾è®¡é¢è¯é¢)
* [çå®æ¶æ](#çå®æ¶æ)
* [å
¬å¸çç³»ç»æ¶æ](#å
¬å¸çç³»ç»æ¶æ)
* [å
¬å¸å·¥ç¨å客](#å
¬å¸å·¥ç¨å客)
* [æ£å¨å®åä¸](#æ£å¨å®åä¸)
* [è´è°¢](#è´è°¢)
* [èç³»æ¹å¼](#èç³»æ¹å¼)
* [许å¯](#许å¯)
## å¦ä¹ æå¼
> åºäºä½ é¢è¯çæ¶é´çº¿ï¼çãä¸ãé¿ï¼å»å¤ä¹ é£äºæ¨èç主é¢ã
![Imgur](images/OfVllex.png)
**é®ï¼å¯¹äºé¢è¯æ¥è¯´ï¼æéè¦ç¥éè¿éçææç¥è¯ç¹åï¼**
**çï¼ä¸ï¼å¦æåªæ¯ä¸ºäºåå¤é¢è¯çè¯ï¼ä½ 并ä¸éè¦ç¥éææçç¥è¯ç¹ã**
å¨ä¸åºé¢è¯ä¸ä½ ä¼è¢«é®å°ä»ä¹åå³äºä¸é¢è¿äºå ç´ ï¼
* ä½ çç»éª
* ä½ çææ¯èæ¯
* ä½ é¢è¯çèä½
* ä½ é¢è¯çå
¬å¸
* è¿æ°
é£äºæç»éªçåé人é常ä¼è¢«ææäºè§£æ´å¤çç³»ç»è®¾è®¡çç¥è¯ãæ¶æå¸æè
å¢éè´è´£äººåä¼è¢«ææäºè§£æ´å¤é¤äºä¸ªäººè´¡ç®ä¹å¤çç¥è¯ã顶级çç§æå
¬å¸é常ä¹ä¼æä¸æ¬¡æè
æ´å¤çç³»ç»è®¾è®¡é¢è¯ã
é¢è¯ä¼å¾å®½æ³çå±å¼å¹¶å¨å 个é¢åæ·±å
¥ãè¿ä¼å¸®å©ä½ äºè§£ä¸äºå
³äºç³»ç»è®¾è®¡çä¸åç主é¢ãåºäºä½ çæ¶é´çº¿ï¼ç»éªï¼é¢è¯çèä½åé¢è¯çå
¬å¸å¯¹ä¸é¢çæ导ååºéå½çè°æ´ã
* **çæ** - 以系ç»è®¾è®¡ä¸»é¢ç**广度**为ç®æ ãéè¿è§£å³**ä¸äº**é¢è¯é¢æ¥ç»ä¹ ã
* **ä¸æ** - 以系ç»è®¾è®¡ä¸»é¢ç**广度**å**å级深度**为ç®æ ãéè¿è§£å³**å¾å¤**é¢è¯é¢æ¥ç»ä¹ ã
* **é¿æ** - 以系ç»è®¾è®¡ä¸»é¢ç**广度**å**é«çº§æ·±åº¦**为ç®æ ãéè¿è§£å³**大é¨å**é¢è¯é¢æ¥ç»ä¹ ã
| | çæ | ä¸æ | é¿æ |
| ---------------------------------------- | ---- | ---- | ---- |
| é
读 [ç³»ç»è®¾è®¡ä¸»é¢](#ç³»ç»è®¾è®¡ä¸»é¢çç´¢å¼) 以è·å¾ä¸ä¸ªå
³äºç³»ç»å¦ä½å·¥ä½ç宽æ³çè®¤è¯ | :+1: | :+1: | :+1: |
| é
读ä¸äºä½ è¦é¢è¯ç[å
¬å¸å·¥ç¨å客](#å
¬å¸å·¥ç¨å客)çæç« | :+1: | :+1: | :+1: |
| é
读 [çå®æ¶æ](#çå®æ¶æ) | :+1: | :+1: | :+1: |
| å¤ä¹ [å¦ä½å¤çä¸ä¸ªç³»ç»è®¾è®¡é¢è¯é¢](#å¦ä½å¤çä¸ä¸ªç³»ç»è®¾è®¡é¢è¯é¢) | :+1: | :+1: | :+1: |
| å®æ [ç³»ç»è®¾è®¡çé¢è¯é¢å解ç](#ç³»ç»è®¾è®¡çé¢è¯é¢å解ç) | ä¸äº | å¾å¤ | 大é¨å |
| å®æ [é¢å对象设计çé¢è¯é¢å解ç](#é¢å对象设计çé¢è¯é®é¢å解ç) | ä¸äº | å¾å¤ | 大é¨å |
| å¤ä¹ [å
¶å®çç³»ç»è®¾è®¡é¢è¯é¢](#å
¶å®çç³»ç»è®¾è®¡é¢è¯é¢) | ä¸äº | å¾å¤ | 大é¨å |
## å¦ä½å¤çä¸ä¸ªç³»ç»è®¾è®¡çé¢è¯é¢
ç³»ç»è®¾è®¡é¢è¯æ¯ä¸ä¸ª**å¼æ¾å¼ç对è¯**ãä»ä»¬ææä½ å»ä¸»å¯¼è¿ä¸ªå¯¹è¯ã
ä½ å¯ä»¥ä½¿ç¨ä¸é¢çæ¥éª¤æ¥æå¼è®¨è®ºã为äºå·©åºè¿ä¸ªè¿ç¨ï¼è¯·ä½¿ç¨ä¸é¢çæ¥éª¤å®æ[ç³»ç»è®¾è®¡çé¢è¯é¢å解ç](#ç³»ç»è®¾è®¡çé¢è¯é¢å解ç)è¿ä¸ªç« èã
### 第ä¸æ¥ï¼æ述使ç¨åºæ¯ï¼çº¦æåå设
æææéè¦çä¸è¥¿èéå¨ä¸èµ·ï¼å®¡è§é®é¢ãä¸åçæé®ï¼ä»¥è³äºæ们å¯ä»¥æ确使ç¨åºæ¯å约æã讨论å设ã
* è°ä¼ä½¿ç¨å®ï¼
* ä»ä»¬ä¼ææ ·ä½¿ç¨å®ï¼
* æå¤å°ç¨æ·ï¼
* ç³»ç»çä½ç¨æ¯ä»ä¹ï¼
* ç³»ç»çè¾å
¥è¾åºåå«æ¯ä»ä¹ï¼
* æ们å¸æå¤çå¤å°æ°æ®ï¼
* æ们å¸ææ¯ç§éå¤çå¤å°è¯·æ±ï¼
* æ们å¸æç读åæ¯çï¼
### 第äºæ¥ï¼åé ä¸ä¸ªé«å±çº§ç设计
使ç¨ææéè¦çç»ä»¶æ¥æç»åºä¸ä¸ªé«å±çº§ç设计ã
* ç»åºä¸»è¦çç»ä»¶åè¿æ¥
* è¯æä½ çæ³æ³
### 第ä¸æ¥ï¼è®¾è®¡æ ¸å¿ç»ä»¶
对æ¯ä¸ä¸ªæ ¸å¿ç»ä»¶è¿è¡è¯¦ç»æ·±å
¥çåæã举ä¾æ¥è¯´ï¼å¦æä½ è¢«é®å°[设计ä¸ä¸ª url 缩åæå¡](solutions/system_design/pastebin/README.md)ï¼å¼å§è®¨è®ºï¼
* çæ并å¨åä¸ä¸ªå®æ´ url ç hash
* [MD5](solutions/system_design/pastebin/README.md) å [Base62](solutions/system_design/pastebin/README.md)
* Hash 碰æ
* SQL è¿æ¯ NoSQL
* æ°æ®åºæ¨¡å
* å°ä¸ä¸ª hashed url ç¿»è¯æå®æ´ç url
* æ°æ®åºæ¥æ¾
* API åé¢å对象设计
### 第åæ¥ï¼æ©å±è®¾è®¡
确认åå¤çç¶é¢ä»¥åä¸äºéå¶ã举ä¾æ¥è¯´å°±æ¯ä½ éè¦ä¸é¢çè¿äºæ¥å®ææ©å±æ§çè®®é¢åï¼
* è´è½½åè¡¡
* æ°´å¹³æ©å±
* ç¼å
* æ°æ®åºåç
论述å¯è½ç解å³åæ³å代价ãæ¯ä»¶äºæ
éè¦åèãå¯ä»¥ä½¿ç¨[å¯æ©å±ç³»ç»ç设计åå](#ç³»ç»è®¾è®¡ä¸»é¢çç´¢å¼)æ¥å¤çç¶é¢ã
### é¢ä¼°è®¡ç®é
ä½ æ许ä¼è¢«è¦æ±éè¿æç®è¿è¡ä¸äºä¼°ç®ã[éå½](#éå½)æ¶åå°çæ¯ä¸é¢çè¿äºèµæºï¼
* [使ç¨é¢ä¼°è®¡ç®é](http://highscalability.com/blog/2011/1/26/google-pro-tip-use-back-of-the-envelope-calculations-to-choo.html)
* [2 ç次æ¹è¡¨](#2-ç次æ¹è¡¨)
* [æ¯ä¸ªç¨åºåé½åºè¯¥ç¥éç延è¿æ°](#æ¯ä¸ªç¨åºåé½åºè¯¥ç¥éç延è¿æ°)
### ç¸å
³èµæºå延伸é
读
æ¥çä¸é¢çé¾æ¥ä»¥è·å¾æ们ææçæ´å¥½çæ³æ³ï¼
* [ææ ·éè¿ä¸ä¸ªç³»ç»è®¾è®¡çé¢è¯](https://www.palantir.com/2011/10/how-to-rock-a-systems-design-interview/)
* [ç³»ç»è®¾è®¡çé¢è¯](http://www.hiredintech.com/system-design)
* [ç³»ç»æ¶æä¸è®¾è®¡çé¢è¯ç®ä»](https://www.youtube.com/watch?v=ZgdS0EUmn70)
## ç³»ç»è®¾è®¡çé¢è¯é¢å解ç
> æ®éçç³»ç»è®¾è®¡é¢è¯é¢åç¸å
³äºä¾ç论述ï¼ä»£ç åå¾è¡¨ã
>
> ä¸å
容æå
³ç解çå¨ `solutions/` æ件夹ä¸ã
| é®é¢ | |
| ---------------------------------------- | ---------------------------------------- |
| 设计 Pastebin.com (æè
Bit.ly) | [解ç](solutions/system_design/pastebin/README-zh-Hans.md) |
| 设计 Twitter æ¶é´çº¿åæç´¢ (æè
Facebook feed åæç´¢) | [解ç](solutions/system_design/twitter/README.md) |
| 设计ä¸ä¸ªç½é¡µç¬è« | [解ç](solutions/system_design/web_crawler/README.md) |
| 设计 Mint.com | [解ç](solutions/system_design/mint/README.md) |
| 为ä¸ä¸ªç¤¾äº¤ç½ç»è®¾è®¡æ°æ®ç»æ | [解ç](solutions/system_design/social_graph/README.md) |
| 为æç´¢å¼æ设计ä¸ä¸ª key-value å¨å | [解ç](solutions/system_design/query_cache/README.md) |
| éè¿åç±»ç¹æ§è®¾è®¡ Amazon çéå®æå | [解ç](solutions/system_design/sales_rank/README.md) |
| å¨ AWS ä¸è®¾è®¡ä¸ä¸ªç¾ä¸ç¨æ·çº§å«çç³»ç» | [解ç](solutions/system_design/scaling_aws/README.md) |
| æ·»å ä¸ä¸ªç³»ç»è®¾è®¡é®é¢ | [è´¡ç®](#è´¡ç®) |
### 设计 Pastebin.com (æè
Bit.ly)
[æ¥çå®è·µä¸è§£ç](solutions/system_design/pastebin/README.md)
![Imgur](images/4edXG0T.png)
### 设计 Twitter æ¶é´çº¿åæç´¢ (æè
Facebook feed åæç´¢)
[æ¥çå®è·µä¸è§£ç](solutions/system_design/twitter/README.md)
![Imgur](images/jrUBAF7.png)
### 设计ä¸ä¸ªç½é¡µç¬è«
[æ¥çå®è·µä¸è§£ç](solutions/system_design/web_crawler/README.md)
![Imgur](images/bWxPtQA.png)
### 设计 Mint.com
[æ¥çå®è·µä¸è§£ç](solutions/system_design/mint/README.md)
![Imgur](images/V5q57vU.png)
### 为ä¸ä¸ªç¤¾äº¤ç½ç»è®¾è®¡æ°æ®ç»æ
[æ¥çå®è·µä¸è§£ç](solutions/system_design/social_graph/README.md)
![Imgur](images/cdCv5g7.png)
### 为æç´¢å¼æ设计ä¸ä¸ª key-value å¨å
[æ¥çå®è·µä¸è§£ç](solutions/system_design/query_cache/README.md)
![Imgur](images/4j99mhe.png)
### 设计æç±»å«åç±»ç Amazon éå®æå
[æ¥çå®è·µä¸è§£ç](solutions/system_design/sales_rank/README.md)
![Imgur](images/MzExP06.png)
### å¨ AWS ä¸è®¾è®¡ä¸ä¸ªç¾ä¸ç¨æ·çº§å«çç³»ç»
[æ¥çå®è·µä¸è§£ç](solutions/system_design/scaling_aws/README.md)
![Imgur](images/jj3A5N8.png)
## é¢å对象设计çé¢è¯é®é¢å解ç
> 常è§é¢å对象设计é¢è¯é®é¢åå®ä¾è®¨è®ºï¼ä»£ç åå¾è¡¨æ¼ç¤ºã
>
> ä¸å
容ç¸å
³ç解å³æ¹æ¡å¨ `solutions/` æ件夹ä¸ã
>**注ï¼æ¤èè¿å¨å®åä¸**
| é®é¢ | |
| ------------ | ---------------------------------------- |
| 设计 hash map | [解å³æ¹æ¡](solutions/object_oriented_design/hash_table/hash_map.ipynb) |
| 设计 LRU ç¼å | [解å³æ¹æ¡](solutions/object_oriented_design/lru_cache/lru_cache.ipynb) |
| 设计ä¸ä¸ªå¼å«ä¸å¿ | [解å³æ¹æ¡](solutions/object_oriented_design/call_center/call_center.ipynb) |
| 设计ä¸å¯ç | [解å³æ¹æ¡](solutions/object_oriented_design/deck_of_cards/deck_of_cards.ipynb) |
| 设计ä¸ä¸ªåè½¦åº | [解å³æ¹æ¡](solutions/object_oriented_design/parking_lot/parking_lot.ipynb) |
| 设计ä¸ä¸ªè天æå¡ | [解å³æ¹æ¡](solutions/object_oriented_design/online_chat/online_chat.ipynb) |
| 设计ä¸ä¸ªç¯å½¢æ°ç» | [å¾
解å³](#è´¡ç®) |
| æ·»å ä¸ä¸ªé¢å对象设计é®é¢ | [å¾
解å³](#è´¡ç®) |
## ç³»ç»è®¾è®¡ä¸»é¢ï¼ä»è¿éå¼å§
ä¸çæç³»ç»è®¾è®¡ï¼
é¦å
ï¼ä½ éè¦å¯¹ä¸è¬æ§ååæä¸ä¸ªåºæ¬ç认è¯ï¼ç¥éå®ä»¬æ¯ä»ä¹ï¼ææ ·ä½¿ç¨ä»¥åå©å¼ã
### 第ä¸æ¥ï¼å顾å¯æ©å±æ§ï¼scalabilityï¼çè§é¢è®²åº§
[åä½å¤§å¦å¯æ©å±æ§è®²åº§](https://www.youtube.com/watch?v=-W9F__D3oY4)
* 主é¢æ¶µç
* åç´æ©å±ï¼Vertical scalingï¼
* æ°´å¹³æ©å±ï¼Horizontal scalingï¼
* ç¼å
* è´è½½åè¡¡
* æ°æ®åºå¤å¶
* æ°æ®åºååº
### 第äºæ¥ï¼å顾å¯æ©å±æ§æç«
[å¯æ©å±æ§](http://www.lecloud.net/tagged/scalability/chrono)
* 主é¢æ¶µçï¼
* [Clones](http://www.lecloud.net/post/7295452622/scalability-for-dummies-part-1-clones)
* [æ°æ®åº](http://www.lecloud.net/post/7994751381/scalability-for-dummies-part-2-database)
* [ç¼å](http://www.lecloud.net/post/9246290032/scalability-for-dummies-part-3-cache)
* [å¼æ¥](http://www.lecloud.net/post/9699762917/scalability-for-dummies-part-4-asynchronism)
### æ¥ä¸æ¥çæ¥éª¤
æ¥ä¸æ¥ï¼æ们å°ççé«é¶çæè¡¡ååè:
* **æ§è½**ä¸**å¯æ©å±æ§**
* **延è¿**ä¸**ååé**
* **å¯ç¨æ§**ä¸**ä¸è´æ§**
è®°ä½**æ¯ä¸ªæ¹é¢é½é¢ä¸´åèåæè¡¡**ã
ç¶åï¼æ们å°æ·±å
¥æ´å
·ä½ç主é¢ï¼å¦ DNSãCDN åè´è½½åè¡¡å¨ã
## æ§è½ä¸å¯æ©å±æ§
å¦ææå¡**æ§è½**çå¢é¿ä¸èµæºçå¢å æ¯ææ¯ä¾çï¼æå¡å°±æ¯å¯æ©å±çãé常ï¼æé«æ§è½æå³çæå¡äºæ´å¤çå·¥ä½åå
ï¼å¦ä¸æ¹é¢ï¼å½æ°æ®éå¢é¿æ¶ï¼åæ ·ä¹å¯ä»¥å¤çæ´å¤§çå·¥ä½åä½ã1
å¦ä¸ä¸ªè§åº¦æ¥çå¾
æ§è½ä¸å¯æ©å±æ§:
* å¦æä½ çç³»ç»æ**æ§è½**é®é¢ï¼å¯¹äºå个ç¨æ·æ¥è¯´æ¯ç¼æ
¢çã
* å¦æä½ çç³»ç»æ**å¯æ©å±æ§**é®é¢ï¼å个ç¨æ·è¾å¿«ä½å¨é«è´è½½ä¸ä¼åæ
¢ã
### æ¥æºå延伸é
读
* [ç®åè°è°å¯æ©å±æ§](http://www.allthingsdistributed.com/2006/03/a_word_on_scalability.html)
* [å¯æ©å±æ§ï¼å¯ç¨æ§ï¼ç¨³å®æ§å模å¼](http://www.slideshare.net/jboner/scalability-availability-stability-patterns/)
## 延è¿ä¸ååé
**延è¿**æ¯æ§è¡æä½æè¿ç®ç»ææè±è´¹çæ¶é´ã
**ååé**æ¯åä½æ¶é´å
ï¼æ§è¡ï¼æ¤ç±»æä½æè¿ç®çæ°éã
é常ï¼ä½ åºè¯¥ä»¥**å¯æ¥å级延è¿**ä¸**æ大åååé**为ç®æ ã
### æ¥æºå延伸é
读
* [ç解延è¿ä¸ååé](https://community.cadence.com/cadence_blogs_8/b/sd/archive/2010/09/13/understanding-latency-vs-throughput)
## å¯ç¨æ§ä¸ä¸è´æ§
### CAP ç论
æ¥æºï¼åç CAP ç论
å¨ä¸ä¸ªåå¸å¼è®¡ç®ç³»ç»ä¸ï¼åªè½åæ¶æ»¡è¶³ä¸åç两ç¹:
* **ä¸è´æ§** â æ¯æ¬¡è®¿é®é½è½è·å¾ææ°æ°æ®ä½å¯è½ä¼æ¶å°é误ååº
* **å¯ç¨æ§** â æ¯æ¬¡è®¿é®é½è½æ¶å°ééååºï¼ä½ä¸ä¿è¯è·åå°ææ°æ°æ®
* **ååºå®¹éæ§** â å¨ä»»æååºç½ç»æ
éçæ
åµä¸ç³»ç»ä»è½ç»§ç»è¿è¡
**ç½ç»å¹¶ä¸å¯é ï¼æä»¥ä½ åºè¦æ¯æååºå®¹éæ§ï¼å¹¶éè¦å¨è½¯ä»¶å¯ç¨æ§åä¸è´æ§é´ååºåèã**
#### CP â ä¸è´æ§åååºå®¹éæ§
çå¾
ååºèç¹çååºå¯è½ä¼å¯¼è´å»¶æ¶é误ãå¦æä½ çä¸å¡éæ±éè¦åå读åï¼CP æ¯ä¸ä¸ªä¸éçéæ©ã
#### AP â å¯ç¨æ§ä¸ååºå®¹éæ§
ååºèç¹ä¸å¯ç¨æ°æ®çæè¿çæ¬å¯è½å¹¶ä¸æ¯ææ°çãå½ååºè§£æå®åï¼åå
¥ï¼æä½ï¼å¯è½éè¦ä¸äºæ¶é´æ¥ä¼ æã
å¦æä¸å¡éæ±å
许[æç»ä¸è´æ§](#æç»ä¸è´æ§)ï¼æå½æå¤é¨æ
éæ¶è¦æ±ç³»ç»ç»§ç»è¿è¡ï¼AP æ¯ä¸ä¸ªä¸éçéæ©ã
### æ¥æºå延伸é
读
* [åç CAP ç论](http://robertgreiner.com/2014/08/cap-theorem-revisited/)
* [éä¿ææå°ä»ç» CAP ç论](http://ksat.me/a-plain-english-introduction-to-cap-theorem/)
* [CAP FAQ](https://github.com/henryr/cap-faq)
## ä¸è´æ§æ¨¡å¼
æåä¸ä»½æ°æ®çå¤ä»½å¯æ¬ï¼æ们é¢ä¸´çææ ·åæ¥å®ä»¬çéæ©ï¼ä»¥ä¾¿è®©å®¢æ·ç«¯æä¸è´çæ¾ç¤ºæ°æ®ãåæ³ [CAP ç论](#cap-ç论)ä¸çä¸è´æ§å®ä¹ â æ¯æ¬¡è®¿é®é½è½è·å¾ææ°æ°æ®ä½å¯è½ä¼æ¶å°é误ååº
### å¼±ä¸è´æ§
å¨åå
¥ä¹åï¼è®¿é®å¯è½çå°ï¼ä¹å¯è½çä¸å°ï¼åå
¥æ°æ®ï¼ãå°½åä¼åä¹è®©å
¶è½è®¿é®ææ°æ°æ®ã
è¿ç§æ¹å¼å¯ä»¥ memcached çç³»ç»ä¸çå°ãå¼±ä¸è´æ§å¨ VoIPï¼è§é¢è天åå®æ¶å¤äººæ¸¸æççå®ç¨ä¾ä¸è¡¨ç°ä¸éãæ个æ¯æ¹ï¼å¦æä½ å¨éè¯ä¸ä¸¢å¤±ä¿¡å·å ç§éæ¶é´ï¼å½éæ°è¿æ¥æ¶ä½ æ¯å¬ä¸å°è¿å ç§éæ说çè¯çã
### æç»ä¸è´æ§
å¨åå
¥åï¼è®¿é®æç»è½çå°åå
¥æ°æ®ï¼é常å¨æ°æ¯«ç§å
ï¼ãæ°æ®è¢«å¼æ¥å¤å¶ã
DNS å email çç³»ç»ä½¿ç¨çæ¯æ¤ç§æ¹å¼ãæç»ä¸è´æ§å¨é«å¯ç¨æ§ç³»ç»ä¸ææä¸éã
### 强ä¸è´æ§
å¨åå
¥åï¼è®¿é®ç«å³å¯è§ãæ°æ®è¢«åæ¥å¤å¶ã
æ件系ç»åå
³ç³»åæ°æ®åºï¼RDBMSï¼ä¸ä½¿ç¨çæ¯æ¤ç§æ¹å¼ã强ä¸è´æ§å¨éè¦è®°å½çç³»ç»ä¸è¿ä½è¯å¥½ã
### æ¥æºå延伸é
读
* [Transactions across data centers](http://snarfed.org/transactions_across_datacenters_io.html)
## å¯ç¨æ§æ¨¡å¼
æ两ç§æ¯æé«å¯ç¨æ§ç模å¼: **æ
éåæ¢ï¼fail-overï¼**å**å¤å¶ï¼replicationï¼**ã
### æ
éåæ¢
#### å·¥ä½å°å¤ç¨åæ¢ï¼Active-passiveï¼
å
³äºå·¥ä½å°å¤ç¨çæ
éåæ¢æµç¨æ¯ï¼å·¥ä½æå¡å¨åéå¨æä¿¡å·ç»å¾
æºä¸çå¤ç¨æå¡å¨ãå¦æå¨æä¿¡å·ä¸æï¼å¤ç¨æå¡å¨åæ¢æå·¥ä½æå¡å¨ç IP å°å并æ¢å¤æå¡ã
å®æºæ¶é´åå³äºå¤ç¨æå¡å¨å¤äºâçâå¾
æºç¶æè¿æ¯éè¦ä»âå·âå¾
æºç¶æè¿è¡å¯å¨ãåªæå·¥ä½æå¡å¨å¤çæµéã
å·¥ä½å°å¤ç¨çæ
éåæ¢ä¹è¢«ç§°ä¸ºä¸»ä»åæ¢ã
#### åå·¥ä½åæ¢ï¼Active-activeï¼
å¨åå·¥ä½åæ¢ä¸ï¼åæ¹é½å¨ç®¡æ§æµéï¼å¨å®ä»¬ä¹é´åæ£è´è½½ã
å¦ææ¯å¤ç½æå¡å¨ï¼DNS å°éè¦å¯¹ä¸¤æ¹é½äºè§£ãå¦ææ¯å
ç½æå¡å¨ï¼åºç¨ç¨åºé»è¾å°éè¦å¯¹ä¸¤æ¹é½äºè§£ã
åå·¥ä½åæ¢ä¹å¯ä»¥ç§°ä¸ºä¸»ä¸»åæ¢ã
### 缺é·ï¼æ
éåæ¢
* æ
éåæ¢éè¦æ·»å é¢å¤ç¡¬ä»¶å¹¶å¢å å¤ææ§ã
* å¦ææ°åå
¥æ°æ®å¨è½è¢«å¤å¶å°å¤ç¨ç³»ç»ä¹åï¼å·¥ä½ç³»ç»åºç°äºæ
éï¼åæå¯è½ä¼ä¸¢å¤±æ°æ®ã
### å¤å¶
#### 主âä»å¤å¶å主â主å¤å¶
è¿ä¸ªä¸»é¢è¿ä¸æ¥æ¢è®¨äº[æ°æ®åº](#æ°æ®åº)é¨å:
* [主âä»å¤å¶](#主ä»å¤å¶)
* [主â主å¤å¶](#主主å¤å¶)
## ååç³»ç»
æ¥æºï¼DNS å®å
¨ä»ç»
ååç³»ç»æ¯æ www.example.com çåå转æ¢æ IP å°åã
ååç³»ç»æ¯åå±æ¬¡çï¼ä¸äº DNS æå¡å¨ä½äºé¡¶å±ãå½æ¥è¯¢ï¼ååï¼ IP æ¶ï¼è·¯ç±æ ISP æä¾è¿æ¥ DNS æå¡å¨çä¿¡æ¯ãè¾åºå±ç DNS æå¡å¨ç¼åæ å°ï¼å®å¯è½ä¼å 为 DNS ä¼ æ延æ¶è失æãDNS ç»æå¯ä»¥ç¼åå¨æµè§å¨ææä½ç³»ç»ä¸ä¸æ®µæ¶é´ï¼æ¶é´é¿çåå³äº[åæ´»æ¶é´ TTL](https://en.wikipedia.org/wiki/Time_to_live)ã
* **NS è®°å½ï¼ååæå¡ï¼** â æå®è§£æååæåååç DNS æå¡å¨ã
* **MX è®°å½ï¼é®ä»¶äº¤æ¢ï¼** â æå®æ¥æ¶ä¿¡æ¯çé®ä»¶æå¡å¨ã
* **A è®°å½ï¼å°åï¼** â æå®åå对åºç IP å°åè®°å½ã
* **CNAMEï¼è§èï¼** â ä¸ä¸ªååæ å°å°å¦ä¸ä¸ªååæ `CNAME` è®°å½ï¼ example.com æå www.example.com ï¼ææ å°å°ä¸ä¸ª `A` è®°å½ã
[CloudFlare](https://www.cloudflare.com/dns/) å [Route 53](https://aws.amazon.com/route53/) çå¹³å°æä¾ç®¡ç DNS çåè½ãæäº DNS æå¡éè¿éä¸æ¹å¼æ¥è·¯ç±æµé:
* [å æ轮询è°åº¦](http://g33kinfo.com/info/archives/2657)
* é²æ¢æµéè¿å
¥ç»´æ¤ä¸çæå¡å¨
* å¨ä¸å大å°é群é´è´è½½åè¡¡
* A/B æµè¯
* åºäºå»¶è¿è·¯ç±
* åºäºå°çä½ç½®è·¯ç±
### 缺é·:DNS
* è½è¯´ç¼åå¯ä»¥åè½» DNS 延è¿ï¼ä½è¿æ¥ DNS æå¡å¨è¿æ¯å¸¦æ¥äºè½»å¾®ç延è¿ã
* è½ç¶å®ä»¬é常ç±[æ¿åºï¼ç½ç»æå¡æä¾åå大å
¬å¸](http://superuser.com/questions/472695/who-controls-the-dns-servers/472729)管çï¼ä½ DNS æå¡ç®¡çä»å¯è½æ¯å¤æçã
* DNS æå¡æè¿éå [DDoS æ»å»](http://dyn.com/blog/dyn-analysis-summary-of-friday-october-21-attack/)ï¼é»æ¢ä¸ç¥é Twitter IP å°åçç¨æ·è®¿é® Twitterã
### æ¥æºå延伸é
读
* [DNS æ¶æ](https://technet.microsoft.com/en-us/library/dd197427(v=ws.10).aspx)
* [Wikipedia](https://en.wikipedia.org/wiki/Domain_Name_System)
* [å
³äº DNS çæç« ](https://support.dnsimple.com/categories/dns/)
## å
容ååç½ç»ï¼CDNï¼
æ¥æºï¼ä¸ºä»ä¹ä½¿ç¨ CDN
å
容ååç½ç»ï¼CDNï¼æ¯ä¸ä¸ªå
¨çæ§ç代çæå¡å¨åå¸å¼ç½ç»ï¼å®ä»é è¿ç¨æ·çä½ç½®æä¾å
容ãé常ï¼HTML/CSS/JSï¼å¾çåè§é¢çéæå
å®¹ç± CDN æä¾ï¼è½ç¶äºé©¬é CloudFront çä¹æ¯æå¨æå
容ãCDN ç DNS 解æä¼åç¥å®¢æ·ç«¯è¿æ¥åªå°æå¡å¨ã
å°å
容åå¨å¨ CDN ä¸å¯ä»¥ä»ä¸¤ä¸ªæ¹é¢æ¥æä¾æ§è½:
* ä»é è¿ç¨æ·çæ°æ®ä¸å¿æä¾èµæº
* éè¿ CDN ä½ çæå¡å¨ä¸å¿
ççå¤ç请æ±
### CDN æ¨éï¼pushï¼
å½ä½ æå¡å¨ä¸å
容åçåå¨æ¶ï¼æ¨é CDN æ¥åæ°å
容ãç´æ¥æ¨éç» CDN 并éå URL å°å以æåä½ çå
容ç CDN å°åãä½ å¯ä»¥é
ç½®å
容å°ææ¶é´åä½æ¶æ´æ°ãå
容åªæå¨æ´æ¹ææ°å¢æ¯ææ¨éï¼æµéæå°åï¼ä½å¨åæ大åã
### CDN æåï¼pullï¼
CDN æåæ¯å½ç¬¬ä¸ä¸ªç¨æ·è¯·æ±è¯¥èµæºæ¶ï¼ä»æå¡å¨ä¸æåèµæºãä½ å°å
容çå¨èªå·±çæå¡å¨ä¸å¹¶éå URL æå CDN å°åãç´å°å
容被ç¼åå¨ CDN ä¸ä¸ºæ¢ï¼è¿æ ·è¯·æ±åªä¼æ´æ
¢ï¼
[åæ´»æ¶é´ï¼TTLï¼](https://en.wikipedia.org/wiki/Time_to_live)å³å®ç¼åå¤ä¹
æ¶é´ãCDN æåæ¹å¼æå°å CDN ä¸çå¨å空é´ï¼ä½å¦æè¿ææ件并å¨å®é
æ´æ¹ä¹å被æåï¼åä¼å¯¼è´åä½çæµéã
é«æµéç«ç¹ä½¿ç¨ CDN æåææä¸éï¼å 为åªææè¿è¯·æ±çå
容ä¿åå¨ CDN ä¸ï¼æµéæè½æ´å¹³è¡¡å°åæ£ã
### 缺é·ï¼CDN
* CDN ææ¬å¯è½å æµéèå¼ï¼å¯è½å¨æè¡¡ä¹åä½ å°ä¸ä¼ä½¿ç¨ CDNã
* å¦æå¨ TTL è¿æä¹åæ´æ°å
容ï¼CDN ç¼åå
容å¯è½ä¼è¿æ¶ã
* CDN éè¦æ´æ¹éæå
容ç URL å°å以æå CDNã
### æ¥æºå延伸é
读
* [å
¨çæ§å
容ååç½ç»](http://repository.cmu.edu/cgi/viewcontent.cgi?article=2112&context=compsci)
* [CDN æåå CDN æ¨éçåºå«](http://www.travelblogadvice.com/technical/the-differences-between-push-and-pull-cdns/)
* [Wikipedia](https://en.wikipedia.org/wiki/Content_delivery_network)
## è´è½½åè¡¡å¨
æ¥æºï¼å¯æ©å±çç³»ç»è®¾è®¡æ¨¡å¼
è´è½½åè¡¡å¨å°ä¼ å
¥ç请æ±ååå°åºç¨æå¡å¨åæ°æ®åºç计ç®èµæºãæ 论åªç§æ
åµï¼è´è½½åè¡¡å¨å°ä»è®¡ç®èµæºæ¥çååºè¿åç»æ°å½ç客æ·ç«¯ãè´è½½åè¡¡å¨çæç¨å¨äº:
* é²æ¢è¯·æ±è¿å
¥ä¸å¥½çæå¡å¨
* é²æ¢èµæºè¿è½½
* 帮å©æ¶é¤åä¸çæ
éç¹
è´è½½åè¡¡å¨å¯ä»¥éè¿ç¡¬ä»¶ï¼æè´µï¼æ HAProxy ç软件æ¥å®ç°ã
å¢å ç好å¤å
æ¬:
* **SSL ç»ç»** â 解å¯ä¼ å
¥ç请æ±å¹¶å å¯æå¡å¨ååºï¼è¿æ ·çè¯å端æå¡å¨å°±ä¸å¿
åæ§è¡è¿äºæ½å¨é«æ¶èè¿ç®äºã
* ä¸éè¦åæ¯å°æå¡å¨ä¸å®è£
[X.509 è¯ä¹¦](https://en.wikipedia.org/wiki/X.509)ã
* **Session çå** â å¦æ Web åºç¨ç¨åºä¸è¿½è¸ªä¼è¯ï¼ååº cookie 并å°ç¹å®å®¢æ·ç«¯ç请æ±è·¯ç±å°åä¸å®ä¾ã
é常ä¼è®¾ç½®éç¨[å·¥ä½âå¤ç¨](#å·¥ä½å°å¤ç¨åæ¢active-passive) æ [åå·¥ä½](#åå·¥ä½åæ¢active-active) 模å¼çå¤ä¸ªè´è½½åè¡¡å¨ï¼ä»¥å
åçæ
éã
è´è½½åè¡¡å¨è½åºäºå¤ç§æ¹å¼æ¥è·¯ç±æµé:
* éæº
* æå°è´è½½
* Session/cookie
* [轮询è°åº¦æå æ轮询è°åº¦ç®æ³](http://g33kinfo.com/info/archives/2657)
* [åå±è´è½½åè¡¡](#åå±è´è½½åè¡¡)
* [ä¸å±è´è½½åè¡¡](#ä¸å±è´è½½åè¡¡)
### åå±è´è½½åè¡¡
åå±è´è½½åè¡¡æ ¹æ®çç[ä¼ è¾å±](#é讯)çä¿¡æ¯æ¥å³å®å¦ä½åå请æ±ãé常ï¼è¿ä¼æ¶åæ¥æºï¼ç®æ IP å°åå请æ±å¤´ä¸ç端å£ï¼ä½ä¸å
æ¬æ°æ®å
ï¼æ¥æï¼å
容ãåå±è´è½½åè¡¡æ§è¡[ç½ç»å°å转æ¢ï¼NATï¼](https://www.nginx.com/resources/glossary/layer-4-load-balancing/)æ¥åä¸æ¸¸æå¡å¨è½¬åç½ç»æ°æ®å
ã
### ä¸å±è´è½½åè¡¡å¨
ä¸å±è´è½½åè¡¡å¨æ ¹æ®çæ§[åºç¨å±](#é讯)æ¥å³å®ææ ·åå请æ±ãè¿ä¼æ¶å请æ±å¤´çå
容ï¼æ¶æ¯å cookieãä¸å±è´è½½åè¡¡å¨ç»ç»ç½ç»æµéï¼è¯»åæ¶æ¯ï¼ååºè´è½½åè¡¡å¤å®ï¼ç¶åä¼ éç»ç¹å®æå¡å¨ãæ¯å¦ï¼ä¸ä¸ªä¸å±è´è½½åè¡¡å¨è½ç´æ¥å°è§é¢æµéè¿æ¥å°æ管è§é¢çæå¡å¨ï¼åæ¶å°æ´ææçç¨æ·è´¦åæµéå¼å¯¼å°å®å
¨æ§æ´å¼ºçæå¡å¨ã
以æ失çµæ´»æ§ä¸ºä»£ä»·ï¼åå±è´è½½åè¡¡æ¯ä¸å±è´è½½åè¡¡è±è´¹æ´å°æ¶é´å计ç®èµæºï¼è½ç¶è¿å¯¹ç°ä»£åç¨ç¡¬ä»¶çæ§è½å½±åçå¾®ã
### æ°´å¹³æ©å±
è´è½½åè¡¡å¨è¿è½å¸®å©æ°´å¹³æ©å±ï¼æé«æ§è½åå¯ç¨æ§ã使ç¨åä¸ç¡¬ä»¶çæ§ä»·æ¯æ´é«ï¼å¹¶ä¸æ¯å¨åå°ç¡¬ä»¶ä¸**åç´æ©å±**æ´è´µç硬件å
·ææ´é«çå¯ç¨æ§ãç¸æ¯æèç¹å®ä¼ä¸ç³»ç»äººæï¼æèåä¸ç¡¬ä»¶æ¹é¢ç人ææ´å 容æã
#### 缺é·ï¼æ°´å¹³æ©å±
* æ°´å¹³æ©å±å¼å
¥äºå¤æ度并æ¶åæå¡å¨å¤å¶
* æå¡å¨åºè¯¥æ¯æ ç¶æç:å®ä»¬ä¹ä¸è¯¥å
å«å session æèµæå¾ççä¸ç¨æ·å
³èçæ°æ®ã
* session å¯ä»¥éä¸åå¨å¨æ°æ®åºææä¹
å[ç¼å](#ç¼å)ï¼RedisãMemcachedï¼çæ°æ®åå¨åºä¸ã
* ç¼ååæ°æ®åºçä¸æ¸¸æå¡å¨éè¦éçä¸æ¸¸æå¡å¨è¿è¡æ©å±ï¼ä»¥å¤çæ´å¤ç并åè¿æ¥ã
### 缺é·ï¼è´è½½åè¡¡å¨
* å¦æ没æ足å¤çèµæºé
ç½®æé
ç½®é误ï¼è´è½½åè¡¡å¨ä¼åæä¸ä¸ªæ§è½ç¶é¢ã
* å¼å
¥è´è½½åè¡¡å¨ä»¥å¸®å©æ¶é¤åç¹æ
éä½å¯¼è´äºé¢å¤çå¤ææ§ã
* å个è´è½½åè¡¡å¨ä¼å¯¼è´åç¹æ
éï¼ä½é
ç½®å¤ä¸ªè´è½½åè¡¡å¨ä¼è¿ä¸æ¥å¢å å¤ææ§ã
### æ¥æºå延伸é
读
* [NGINX æ¶æ](https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/)
* [HAProxy æ¶ææå](http://www.haproxy.org/download/1.2/doc/architecture.txt)
* [å¯æ©å±æ§](http://www.lecloud.net/post/7295452622/scalability-for-dummies-part-1-clones)
* [Wikipedia](https://en.wikipedia.org/wiki/Load_balancing_(computing))
* [åå±è´è½½å¹³è¡¡](https://www.nginx.com/resources/glossary/layer-4-load-balancing/)
* [ä¸å±è´è½½å¹³è¡¡](https://www.nginx.com/resources/glossary/layer-7-load-balancing/)
* [ELB çå¬å¨é
ç½®](http://docs.aws.amazon.com/elasticloadbalancing/latest/classic/elb-listener-config.html)
## åå代çï¼web æå¡å¨ï¼
èµææ¥æºï¼ç»´åºç¾ç§
åå代çæ¯ä¸ç§å¯ä»¥éä¸å°è°ç¨å
é¨æå¡ï¼å¹¶æä¾ç»ä¸æ¥å£ç»å
Œ
±å®¢æ·ç web æå¡å¨ãæ¥èªå®¢æ·ç«¯ç请æ±å
被åå代çæå¡å¨è½¬åå°å¯ååºè¯·æ±çæå¡å¨ï¼ç¶å代çåææå¡å¨çååºç»æè¿åç»å®¢æ·ç«¯ã
带æ¥ç好å¤å
æ¬ï¼
- **å¢å å®å
¨æ§** - éèå端æå¡å¨çä¿¡æ¯ï¼å±è½é»ååä¸ç IPï¼éå¶æ¯ä¸ªå®¢æ·ç«¯çè¿æ¥æ°ã
- **æé«å¯æ©å±æ§åçµæ´»æ§** - 客æ·ç«¯åªè½çå°åå代çæå¡å¨ç IPï¼è¿ä½¿ä½ å¯ä»¥å¢åæå¡å¨æè
ä¿®æ¹å®ä»¬çé
ç½®ã
- **æ¬å°ç»ç» SSL ä¼è¯** - 解å¯ä¼ å
¥è¯·æ±ï¼å å¯æå¡å¨ååºï¼è¿æ ·å端æå¡å¨å°±ä¸å¿
å®æè¿äºæ½å¨çé«ææ¬çæä½ã
- å
é¤äºå¨æ¯ä¸ªæå¡å¨ä¸å®è£
[X.509](https://en.wikipedia.org/wiki/X.509) è¯ä¹¦çéè¦
- **å缩** - å缩æå¡å¨ååº
- **ç¼å** - ç´æ¥è¿åå½ä¸çç¼åç»æ
- **éæå
容** - ç´æ¥æä¾éæå
容
- HTML/CSS/JS
- å¾ç
- è§é¢
- çç
### è´è½½åè¡¡å¨ä¸åå代ç
- å½ä½ æå¤ä¸ªæå¡å¨æ¶ï¼é¨ç½²è´è½½åè¡¡å¨é常æç¨ãé常ï¼è´è½½åè¡¡å¨å°æµéè·¯ç±ç»ä¸ç»åè½ç¸åçæå¡å¨ä¸ã
- å³ä½¿åªæä¸å° web æå¡å¨æè
åºç¨æå¡å¨æ¶ï¼åå代çä¹æç¨ï¼å¯ä»¥åèä¸ä¸èä»ç»ç好å¤ã
- NGINX å HAProxy ç解å³æ¹æ¡å¯ä»¥åæ¶æ¯æ第ä¸å±åå代çåè´è½½åè¡¡ã
### ä¸å©ä¹å¤ï¼åå代ç
- å¼å
¥åå代çä¼å¢å ç³»ç»çå¤æ度ã
- åç¬ä¸ä¸ªåå代çæå¡å¨ä»å¯è½åçåç¹æ
éï¼é
ç½®å¤å°åå代çæå¡å¨ï¼å¦[æ
é转移](https://en.wikipedia.org/wiki/Failover)ï¼ä¼è¿ä¸æ¥å¢å å¤æ度ã
### æ¥æºå延伸é
读
- [åå代çä¸è´è½½åè¡¡](https://www.nginx.com/resources/glossary/reverse-proxy-vs-load-balancer/)
- [NGINX æ¶æ](https://www.nginx.com/blog/inside-nginx-how-we-designed-for-performance-scale/)
- [HAProxy æ¶ææå](http://www.haproxy.org/download/1.2/doc/architecture.txt)
- [Wikipedia](https://en.wikipedia.org/wiki/Reverse_proxy)
## åºç¨å±
èµææ¥æºï¼å¯ç¼©æ¾ç³»ç»ææ¶ä»ç»
å° Web æå¡å±ä¸åºç¨å±ï¼ä¹è¢«ç§°ä½å¹³å°å±ï¼å离ï¼å¯ä»¥ç¬ç«ç¼©æ¾åé
ç½®è¿ä¸¤å±ãæ·»å æ°ç API åªéè¦æ·»å åºç¨æå¡å¨ï¼èä¸å¿
æ·»å é¢å¤ç web æå¡å¨ã
**åä¸èè´£åå**æå¡å°åçï¼èªæ²»çæå¡å
±ååä½ãå°å¢ééè¿æä¾å°åçæå¡ï¼å¯ä»¥æ´æ¿è¿å°è®¡åå¢é¿ã
åºç¨å±ä¸çå·¥ä½è¿ç¨ä¹æå¯ä»¥å®ç°[å¼æ¥å](#å¼æ¥)ã
### å¾®æå¡
ä¸æ¤è®¨è®ºç¸å
³çè¯é¢æ¯ [å¾®æå¡](https://en.wikipedia.org/wiki/Microservices)ï¼å¯ä»¥è¢«æ述为ä¸ç³»åå¯ä»¥ç¬ç«é¨ç½²çå°åçï¼æ¨¡ååæå¡ãæ¯ä¸ªæå¡è¿è¡å¨ä¸ä¸ªç¬ç«ç线ç¨ä¸ï¼éè¿æç¡®å®ä¹çè½»é级æºå¶é讯ï¼å
±åå®ç°ä¸å¡ç®æ ã1
ä¾å¦ï¼Pinterest å¯è½æè¿äºå¾®æå¡ï¼ ç¨æ·èµæãå
³æ³¨è
ãFeed æµãæç´¢ãç
§çä¸ä¼ çã
### æå¡åç°
å [Consul](https://www.consul.io/docs/index.html)ï¼[Etcd](https://coreos.com/etcd/docs/latest) å [Zookeeper](http://www.slideshare.net/sauravhaloi/introduction-to-apache-zookeeper) è¿æ ·çç³»ç»å¯ä»¥éè¿è¿½è¸ªæ³¨ååãå°åã端å£çä¿¡æ¯æ¥å¸®å©æå¡äºç¸åç°å¯¹æ¹ã[Health checks](https://www.consul.io/intro/getting-started/checks.html) å¯ä»¥å¸®å©ç¡®è®¤æå¡çå®æ´æ§åæ¯å¦ç»å¸¸ä½¿ç¨ä¸ä¸ª [HTTP](#è¶
ææ¬ä¼ è¾åè®®http) è·¯å¾ãConsul å Etcd é½æä¸ä¸ªå
建ç [key-value åå¨](#é®-å¼åå¨) ç¨æ¥åå¨é
置信æ¯åå
¶ä»çå
±äº«ä¿¡æ¯ã
### ä¸å©ä¹å¤ï¼åºç¨å±
- æ·»å ç±å¤ä¸ªæ¾è¦åæå¡ç»æçåºç¨å±ï¼ä»æ¶æãè¿è¥ãæµç¨çå±é¢æ¥è®²å°é常ä¸åï¼ç¸å¯¹äºåä½ç³»ç»ï¼ã
- å¾®æå¡ä¼å¢å é¨ç½²åè¿è¥çå¤æ度ã
### æ¥æºå延伸é
读
- [å¯ç¼©æ¾ç³»ç»ææ¶ä»ç»](http://lethain.com/introduction-to-architecting-systems-for-scale)
- [ç ´è§£ç³»ç»è®¾è®¡é¢è¯](http://www.puncsky.com/blog/2016-02-13-crack-the-system-design-interview)
- [é¢åæå¡æ¶æ](https://en.wikipedia.org/wiki/Service-oriented_architecture)
- [Zookeeper ä»ç»](http://www.slideshare.net/sauravhaloi/introduction-to-apache-zookeeper)
- [æ建微æå¡ï¼ä½ æéè¦ç¥éçä¸å](https://cloudncode.wordpress.com/2016/07/22/msa-getting-started/)
## æ°æ®åº
èµææ¥æºï¼æ©å±ä½ çç¨æ·æ°å°ç¬¬ä¸ä¸ªä¸åä¸
### å
³ç³»åæ°æ®åºç®¡çç³»ç»ï¼RDBMSï¼
å SQL è¿æ ·çå
³ç³»åæ°æ®åºæ¯ä¸ç³»å以表çå½¢å¼ç»ç»çæ°æ®é¡¹éåã
> æ ¡å¯¹æ³¨ï¼è¿éä½è
SQL å¯è½æçæ¯ MySQL
**ACID** ç¨æ¥æè¿°å
³ç³»åæ°æ®åº[äºå¡](https://en.wikipedia.org/wiki/Database_transaction)çç¹æ§ã
- **ååæ§** - æ¯ä¸ªäºå¡å
é¨æææä½è¦ä¹å
¨é¨å®æï¼è¦ä¹å
¨é¨ä¸å®æã
- **ä¸è´æ§** - ä»»ä½äºå¡é½ä½¿æ°æ®åºä»ä¸ä¸ªææçç¶æ转æ¢å°å¦ä¸ä¸ªææç¶æã
- **é离æ§** - 并åæ§è¡äºå¡çç»æä¸é¡ºåºæ§è¡äºå¡çç»æç¸åã
- **æä¹
æ§** - äºå¡æ交åï¼å¯¹ç³»ç»çå½±åæ¯æ°¸ä¹
çã
å
³ç³»åæ°æ®åºæ©å±å
æ¬è®¸å¤ææ¯ï¼**主ä»å¤å¶**ã**主主å¤å¶**ã**èå**ã**åç**ã**éè§èå**å **SQLè°ä¼**ã
èµææ¥æºï¼å¯æ©å±æ§ãå¯ç¨æ§ã稳å®æ§ã模å¼
#### 主ä»å¤å¶
主åºåæ¶è´è´£è¯»åååå
¥æä½ï¼å¹¶å¤å¶åå
¥å°ä¸ä¸ªæå¤ä¸ªä»åºä¸ï¼ä»åºåªè´è´£è¯»æä½ãæ ç¶å½¢å¼çä»åºåå°åå
¥å¤å¶å°æ´å¤çä»åºä¸å»ãå¦æ主åºç¦»çº¿ï¼ç³»ç»å¯ä»¥ä»¥åªè¯»æ¨¡å¼è¿è¡ï¼ç´å°æ个ä»åºè¢«æå为主åºæææ°ç主åºåºç°ã
##### ä¸å©ä¹å¤ï¼ä¸»ä»å¤å¶
- å°ä»åºæå为主åºéè¦é¢å¤çé»è¾ã
- åè[ä¸å©ä¹å¤ï¼å¤å¶](#ä¸å©ä¹å¤å¤å¶)ä¸ï¼ä¸»ä»å¤å¶å主主å¤å¶**å
±å**çé®é¢ã
èµææ¥æºï¼å¯æ©å±æ§ãå¯ç¨æ§ã稳å®æ§ã模å¼
#### 主主å¤å¶
两个主åºé½è´è´£è¯»æä½ååæä½ï¼åå
¥æä½æ¶äºç¸åè°ãå¦æå
¶ä¸ä¸ä¸ªä¸»åºææºï¼ç³»ç»å¯ä»¥ç»§ç»è¯»åååå
¥ã
##### ä¸å©ä¹å¤ï¼ 主主å¤å¶
- ä½ éè¦æ·»å è´è½½åè¡¡å¨æè
å¨åºç¨é»è¾ä¸åæ¹å¨ï¼æ¥ç¡®å®åå
¥åªä¸ä¸ªæ°æ®åºã
- å¤æ°ä¸»-主系ç»è¦ä¹ä¸è½ä¿è¯ä¸è´æ§ï¼è¿å ACIDï¼ï¼è¦ä¹å 为åæ¥äº§çäºåå
¥å»¶è¿ã
- éçæ´å¤åå
¥èç¹çå å
¥å延è¿çæé«ï¼å¦ä½è§£å³å²çªæ¾å¾è¶åéè¦ã
- åè[ä¸å©ä¹å¤ï¼å¤å¶](#ä¸å©ä¹å¤å¤å¶)ä¸ï¼ä¸»ä»å¤å¶å主主å¤å¶**å
±å**çé®é¢ã
##### ä¸å©ä¹å¤ï¼å¤å¶
- å¦æ主åºå¨å°æ°åå
¥çæ°æ®å¤å¶å°å
¶ä»èç¹åææï¼åææ°æ®ä¸¢å¤±çå¯è½ã
- åå
¥ä¼è¢«éæ¾å°è´è´£è¯»åæä½çå¯æ¬ãå¯æ¬å¯è½å 为è¿å¤åæä½é»å¡ä½ï¼å¯¼è´è¯»ååè½å¼å¸¸ã
- 读åä»åºè¶å¤ï¼éè¦å¤å¶çåå
¥æ°æ®å°±è¶å¤ï¼å¯¼è´æ´ä¸¥éçå¤å¶å»¶è¿ã
- å¨æäºæ°æ®åºç³»ç»ä¸ï¼åå
¥ä¸»åºçæä½å¯ä»¥ç¨å¤ä¸ªçº¿ç¨å¹¶è¡åå
¥ï¼ä½è¯»åå¯æ¬åªæ¯æå线ç¨é¡ºåºå°åå
¥ã
- å¤å¶æå³çæ´å¤ç硬件åé¢å¤çå¤æ度ã
##### æ¥æºå延伸é
读
- [æ©å±æ§ï¼å¯ç¨æ§ï¼ç¨³å®æ§æ¨¡å¼](http://www.slideshare.net/jboner/scalability-availability-stability-patterns/)
- [å¤ä¸»å¤å¶](https://en.wikipedia.org/wiki/Multi-master_replication)
#### èå
èµææ¥æºï¼æ©å±ä½ çç¨æ·æ°å°ç¬¬ä¸ä¸ªä¸åä¸
èåï¼ææåè½ååï¼å°æ°æ®åºæ对åºåè½åå²ãä¾å¦ï¼ä½ å¯ä»¥æä¸ä¸ªæ°æ®åºï¼**论å**ã**ç¨æ·**å**产å**ï¼èä¸ä»
æ¯ä¸ä¸ªåä½æ°æ®åºï¼ä»èåå°æ¯ä¸ªæ°æ®åºç读åååå
¥æµéï¼åå°å¤å¶å»¶è¿ãè¾å°çæ°æ®åºæå³çæ´å¤éåæ¾å
¥å
åçæ°æ®ï¼è¿èæå³çæ´é«çç¼åå½ä¸å çã没æåªè½ä¸²è¡åå
¥çä¸å¿å主åºï¼ä½ å¯ä»¥å¹¶è¡åå
¥ï¼æé«è´è½½è½åã
##### ä¸å©ä¹å¤ï¼èå
- å¦æä½ çæ°æ®åºæ¨¡å¼éè¦å¤§éçåè½åæ°æ®è¡¨ï¼èåçæç并ä¸å¥½ã
- ä½ éè¦æ´æ°åºç¨ç¨åºçé»è¾æ¥ç¡®å®è¦è¯»åååå
¥åªä¸ªæ°æ®åºã
- ç¨ [server link](http://stackoverflow.com/questions/5145637/querying-data-by-joining-two-tables-in-two-database-on-different-servers) ä»ä¸¤ä¸ªåºèç»æ°æ®æ´å¤æã
- èåéè¦æ´å¤ç硬件åé¢å¤çå¤æ度ã
##### æ¥æºå延伸é
读ï¼èå
- [æ©å±ä½ çç¨æ·æ°å°ç¬¬ä¸ä¸ªä¸åä¸](https://www.youtube.com/watch?v=w95murBkYmU)
#### åç
èµææ¥æºï¼å¯æ©å±æ§ãå¯ç¨æ§ã稳å®æ§ã模å¼
åçå°æ°æ®åé
å¨ä¸åçæ°æ®åºä¸ï¼ä½¿å¾æ¯ä¸ªæ°æ®åºä»
管çæ´ä¸ªæ°æ®éçä¸ä¸ªåéã以ç¨æ·æ°æ®åºä¸ºä¾ï¼éçç¨æ·æ°éçå¢å ï¼è¶æ¥è¶å¤çåçä¼è¢«æ·»å å°é群ä¸ã
类似[èå](#èå)çä¼ç¹ï¼åçå¯ä»¥åå°è¯»åååå
¥æµéï¼åå°å¤å¶å¹¶æé«ç¼åå½ä¸çãä¹åå°äºç´¢å¼ï¼é常æå³çæ¥è¯¢æ´å¿«ï¼æ§è½æ´å¥½ãå¦æä¸ä¸ªåçåºé®é¢ï¼å
¶ä»çä»è½è¿è¡ï¼ä½ å¯ä»¥ä½¿ç¨æç§å½¢å¼çåä½æ¥é²æ¢æ°æ®ä¸¢å¤±ã类似èåï¼æ²¡æåªè½ä¸²è¡åå
¥çä¸å¿å主åºï¼ä½ å¯ä»¥å¹¶è¡åå
¥ï¼æé«è´è½½è½åã
常è§çåæ³æ¯ç¨æ·å§æ°çé¦åæ¯æè
ç¨æ·çå°çä½ç½®æ¥åéç¨æ·è¡¨ã
##### ä¸å©ä¹å¤ï¼åç
- ä½ éè¦ä¿®æ¹åºç¨ç¨åºçé»è¾æ¥å®ç°åçï¼è¿ä¼å¸¦æ¥å¤æç SQL æ¥è¯¢ã
- åçä¸åçå¯è½å¯¼è´æ°æ®è´è½½ä¸åè¡¡ãä¾å¦ï¼è¢«é¢ç¹è®¿é®çç¨æ·æ°æ®ä¼å¯¼è´å
¶æå¨åççè´è½½ç¸å¯¹å
¶ä»åçé«ã
- å平衡ä¼å¼å
¥é¢å¤çå¤æ度ãåºäº[ä¸è´æ§åå¸](http://www.paperplanes.de/2011/12/9/the-magic-of-consistent-hashing.html)çåçç®æ³å¯ä»¥åå°è¿ç§æ
åµã
- èç»å¤ä¸ªåççæ°æ®æä½æ´å¤æã
- åçéè¦æ´å¤ç硬件åé¢å¤çå¤æ度ã
#### æ¥æºå延伸é
读ï¼åç
- [åçæ¶ä»£æ¥ä¸´](http://highscalability.com/blog/2009/8/6/an-unorthodox-approach-to-database-design-the-coming-of-the.html)
- [æ°æ®åºåçæ¶æ](https://en.wikipedia.org/wiki/Shard_(database_architecture))
- [ä¸è´æ§åå¸](http://www.paperplanes.de/2011/12/9/the-magic-of-consistent-hashing.html)
#### éè§èå
éè§èåè¯å¾ä»¥åå
¥æ§è½ä¸ºä»£ä»·æ¥æ¢å读åæ§è½ãå¨å¤ä¸ªè¡¨ä¸åä½æ°æ®å¯æ¬ï¼ä»¥é¿å
é«ææ¬çèç»æä½ãä¸äºå
³ç³»åæ°æ®åºï¼æ¯å¦ [PostgreSQL](https://en.wikipedia.org/wiki/PostgreSQL) å Oracle æ¯æ[ç©åè§å¾](https://en.wikipedia.org/wiki/Materialized_view)ï¼å¯ä»¥å¤çåä½ä¿¡æ¯åå¨åä¿è¯åä½å¯æ¬ä¸è´ã
å½æ°æ®ä½¿ç¨è¯¸å¦[èå](#èå)å[åç](#åç)çææ¯è¢«åå²ï¼è¿ä¸æ¥æé«äºå¤çè·¨æ°æ®ä¸å¿çèç»æä½å¤æ度ãéè§èåå¯ä»¥è§é¿è¿ç§å¤æçèç»æä½ã
å¨å¤æ°ç³»ç»ä¸ï¼è¯»åæä½çé¢çè¿é«äºåå
¥æä½ï¼æ¯ä¾å¯è¾¾å° 100:1ï¼çè³ 1000:1ãéè¦å¤æçæ°æ®åºèç»ç读åæä½ææ¬é常é«ï¼å¨ç£çæä½ä¸æ¶èäºå¤§éæ¶é´ã
##### ä¸å©ä¹å¤ï¼éè§èå
- æ°æ®ä¼åä½ã
- 约æå¯ä»¥å¸®å©åä½çä¿¡æ¯å¯æ¬ä¿æåæ¥ï¼ä½è¿æ ·ä¼å¢å æ°æ®åºè®¾è®¡çå¤æ度ã
- éè§èåçæ°æ®åºå¨é«åå
¥è´è½½ä¸æ§è½å¯è½æ¯è§èåçæ°æ®åºå·®ã
##### æ¥æºå延伸é
读ï¼éè§èå
- [éè§èå](https://en.wikipedia.org/wiki/Denormalization)
#### SQL è°ä¼
SQL è°ä¼æ¯ä¸ä¸ªèå´å¾å¹¿çè¯é¢ï¼æå¾å¤ç¸å
³ç[书](https://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=sql+tuning)å¯ä»¥ä½ä¸ºåèã
å©ç¨**åºåæµè¯**å**æ§è½åæ**æ¥æ¨¡æååç°ç³»ç»ç¶é¢å¾éè¦ã
- **åºåæµè¯** - ç¨ [ab](http://httpd.apache.org/docs/2.2/programs/ab.html) çå·¥å
·æ¨¡æé«è´è½½æ
åµã
- **æ§è½åæ** - éè¿å¯ç¨å¦[æ
¢æ¥è¯¢æ¥å¿](http://dev.mysql.com/doc/refman/5.7/en/slow-query-log.html)çå·¥å
·æ¥è¾
å©è¿½è¸ªæ§è½é®é¢ã
åºåæµè¯åæ§è½åæå¯è½ä¼æå¼ä½ å°ä»¥ä¸ä¼åæ¹æ¡ã
##### æ¹è¿æ¨¡å¼
- 为äºå®ç°å¿«é访é®ï¼MySQL å¨ç£çä¸ç¨è¿ç»çååå¨æ°æ®ã
- ä½¿ç¨ `CHAR` ç±»ååå¨åºå®é¿åº¦çå段ï¼ä¸è¦ç¨ `VARCHAR`ã
- `CHAR` å¨å¿«éãéæºè®¿é®æ¶æçå¾é«ãå¦æä½¿ç¨ `VARCHAR`ï¼å¦æä½ æ³è¯»åä¸ä¸ä¸ªå符串ï¼ä¸å¾ä¸å
读åå°å½åå符串çæ«å°¾ã
- ä½¿ç¨ `TEXT` ç±»ååå¨å¤§åçææ¬ï¼ä¾å¦å客æ£æã`TEXT` è¿å
许å¸å°æç´¢ãä½¿ç¨ `TEXT` å段éè¦å¨ç£çä¸åå¨ä¸ä¸ªç¨äºå®ä½ææ¬åçæéã
- ä½¿ç¨ `INT` ç±»ååå¨é«è¾¾ 2^32 æ 40 亿çè¾å¤§æ°åã
- ä½¿ç¨ `DECIMAL` ç±»ååå¨è´§å¸å¯ä»¥é¿å
æµ®ç¹æ°è¡¨ç¤ºé误ã
- é¿å
ä½¿ç¨ `BLOBS` åå¨å®é
对象ï¼èæ¯ç¨æ¥åå¨åæ¾å¯¹è±¡çä½ç½®ã
- `VARCHAR(255)` æ¯ä»¥ 8 ä½æ°ååå¨çæ大å符æ°ï¼å¨æäºå
³ç³»åæ°æ®åºä¸ï¼æ大é度å°å©ç¨åèã
- å¨éç¨åºæ¯ä¸è®¾ç½® `NOT NULL` 约ææ¥[æé«æç´¢æ§è½](http://stackoverflow.com/questions/1017239/how-do-null-values-affect-performance-in-a-database-search)ã
##### 使ç¨æ£ç¡®çç´¢å¼
- ä½ æ£æ¥è¯¢ï¼`SELECT`ã`GROUP BY`ã`ORDER BY`ã`JOIN`ï¼çåå¦æç¨äºç´¢å¼ä¼æ´å¿«ã
- ç´¢å¼é常表示为èªå¹³è¡¡ç [B æ ](https://en.wikipedia.org/wiki/B-tree)ï¼å¯ä»¥ä¿ææ°æ®æåºï¼å¹¶å
许å¨å¯¹æ°æ¶é´å
è¿è¡æç´¢ï¼é¡ºåºè®¿é®ï¼æå
¥ï¼å é¤æä½ã
- 设置索å¼ï¼ä¼å°æ°æ®åå¨å
åä¸ï¼å ç¨äºæ´å¤å
å空é´ã
- åå
¥æä½ä¼åæ
¢ï¼å 为索å¼éè¦è¢«æ´æ°ã
- å 载大éæ°æ®æ¶ï¼ç¦ç¨ç´¢å¼åå è½½æ°æ®ï¼ç¶åé建索å¼ï¼è¿æ ·ä¹è®¸ä¼æ´å¿«ã
##### é¿å
é«ææ¬çèç»æä½
- ææ§è½éè¦ï¼å¯ä»¥è¿è¡éè§èåã
##### åå²æ°æ®è¡¨
- å°çç¹æ°æ®æåå°åç¬çæ°æ®è¡¨ä¸ï¼å¯ä»¥æå©äºç¼åã
##### è°ä¼æ¥è¯¢ç¼å
- å¨æäºæ
åµä¸ï¼[æ¥è¯¢ç¼å](http://dev.mysql.com/doc/refman/5.7/en/query-cache)å¯è½ä¼å¯¼è´[æ§è½é®é¢](https://www.percona.com/blog/2014/01/28/10-mysql-performance-tuning-settings-after-installation/)ã
##### æ¥æºå延伸é
读
- [MySQL æ¥è¯¢ä¼åå°è´´å£«](http://20bits.com/article/10-tips-for-optimizing-mysql-queries-that-dont-suck)
- [为ä»ä¹ VARCHAR(255) å¾å¸¸è§ï¼](http://stackoverflow.com/questions/1217466/is-there-a-good-reason-i-see-varchar255-used-so-often-as-opposed-to-another-l)
- [Null å¼æ¯å¦ä½å½±åæ°æ®åºæ§è½çï¼](http://stackoverflow.com/questions/1017239/how-do-null-values-affect-performance-in-a-database-search)
- [æ
¢æ¥è¯¢æ¥å¿](http://dev.mysql.com/doc/refman/5.7/en/slow-query-log.html)
### NoSQL
NoSQL æ¯**é®-å¼æ°æ®åº**ã**ææ¡£åæ°æ®åº**ã**ååæ°æ®åº**æ**å¾æ°æ®åº**çç»ç§°ãæ°æ®åºæ¯éè§èåçï¼è¡¨èç»å¤§å¤å¨åºç¨ç¨åºä»£ç ä¸å®æã大å¤æ° NoSQL æ æ³å®ç°çæ£ç¬¦å ACID çäºå¡ï¼æ¯æ[æç»ä¸è´](#æç»ä¸è´æ§)ã
**BASE** é常被ç¨äºæè¿° NoSQL æ°æ®åºçç¹æ§ãç¸æ¯ [CAP ç论](#cap-ç论)ï¼BASE 强è°å¯ç¨æ§è¶
è¿ä¸è´æ§ã
- **åºæ¬å¯ç¨** - ç³»ç»ä¿è¯å¯ç¨æ§ã
- **软ç¶æ** - å³ä½¿æ²¡æè¾å
¥ï¼ç³»ç»ç¶æä¹å¯è½éçæ¶é´ååã
- **æç»ä¸è´æ§** - ç»è¿ä¸æ®µæ¶é´ä¹åï¼ç³»ç»æç»ä¼åä¸è´ï¼å 为系ç»å¨æ¤æé´æ²¡ææ¶å°ä»»ä½è¾å
¥ã
é¤äºå¨ [SQL è¿æ¯ NoSQL](#sql-è¿æ¯-nosql) ä¹é´åéæ©ï¼äºè§£åªç§ç±»åç NoSQL æ°æ®åºæéåä½ çç¨ä¾ä¹æ¯é常æ帮å©çãæ们å°å¨ä¸ä¸èä¸å¿«éäºè§£ä¸ **é®-å¼åå¨**ã**ææ¡£ååå¨**ã**åååå¨**å**å¾åå¨**æ°æ®åºã
#### é®-å¼åå¨
> æ½è±¡æ¨¡åï¼åå¸è¡¨
é®-å¼åå¨é常å¯ä»¥å®ç° O(1) æ¶é´è¯»åï¼ç¨å
åæ SSD åå¨æ°æ®ãæ°æ®åå¨å¯ä»¥æ[åå
¸é¡ºåº](https://en.wikipedia.org/wiki/Lexicographical_order)ç»´æ¤é®ï¼ä»èå®ç°é®çé«ææ£ç´¢ãé®-å¼åå¨å¯ä»¥ç¨äºåå¨å
æ°æ®ã
é®-å¼åå¨æ§è½å¾é«ï¼é常ç¨äºåå¨ç®åæ°æ®æ¨¡åæé¢ç¹ä¿®æ¹çæ°æ®ï¼å¦åæ¾å¨å
åä¸çç¼åãé®-å¼åå¨æä¾çæä½æéï¼å¦æéè¦æ´å¤æä½ï¼å¤æ度å°è½¬å«å°åºç¨ç¨åºå±é¢ã
é®-å¼åå¨æ¯å¦ææ¡£åå¨ï¼å¨æäºæ
åµä¸ï¼çè³æ¯å¾åå¨çæ´å¤æçåå¨ç³»ç»çåºç¡ã
#### æ¥æºå延伸é
读
- [é®-å¼æ°æ®åº](https://en.wikipedia.org/wiki/Key-value_database)
- [é®-å¼åå¨çå£å¿](http://stackoverflow.com/questions/4056093/what-are-the-disadvantages-of-using-a-key-value-table-over-nullable-columns-or)
- [Redis æ¶æ](http://qnimate.com/overview-of-redis-architecture/)
- [Memcached æ¶æ](https://adayinthelifeof.nl/2011/02/06/memcache-internals/)
#### æ档类ååå¨
> æ½è±¡æ¨¡åï¼å°ææ¡£ä½ä¸ºå¼çé®-å¼åå¨
æ档类ååå¨ä»¥ææ¡£ï¼XMLãJSONãäºè¿å¶æ件çï¼ä¸ºä¸å¿ï¼ææ¡£åå¨äºæå®å¯¹è±¡çå
¨é¨ä¿¡æ¯ãææ¡£åå¨æ ¹æ®ææ¡£èªèº«çå
é¨ç»ææä¾ API ææ¥è¯¢è¯å¥æ¥å®ç°æ¥è¯¢ã请注æï¼è®¸å¤é®-å¼åå¨æ°æ®åºæç¨å¼åå¨å
æ°æ®çç¹æ§ï¼è¿ä¹æ¨¡ç³äºè¿ä¸¤ç§åå¨ç±»åççéã
åºäºåºå±å®ç°ï¼ææ¡£å¯ä»¥æ ¹æ®éåãæ ç¾ãå
æ°æ®æè
æ件夹ç»ç»ã尽管ä¸åææ¡£å¯ä»¥è¢«ç»ç»å¨ä¸èµ·æè
åæä¸ç»ï¼ä½ç¸äºä¹é´å¯è½å
·æå®å
¨ä¸åçå段ã
MongoDB å CouchDB çä¸äºæ档类ååå¨è¿æä¾äºç±»ä¼¼ SQL è¯è¨çæ¥è¯¢è¯å¥æ¥å®ç°å¤ææ¥è¯¢ãDynamoDB åæ¶æ¯æé®-å¼åå¨åæ档类ååå¨ã
æ档类ååå¨å
·å¤é«åº¦ççµæ´»æ§ï¼å¸¸ç¨äºå¤çå¶å°ååçæ°æ®ã
#### æ¥æºå延伸é
读ï¼æ档类ååå¨
- [é¢åææ¡£çæ°æ®åº](https://en.wikipedia.org/wiki/Document-oriented_database)
- [MongoDB æ¶æ](https://www.mongodb.com/mongodb-architecture)
- [CouchDB æ¶æ](https://blog.couchdb.org/2016/08/01/couchdb-2-0-architecture/)
- [Elasticsearch æ¶æ](https://www.elastic.co/blog/found-elasticsearch-from-the-bottom-up)
#### åååå¨
èµææ¥æº: SQL å NoSQLï¼ä¸ä¸ªç®ççåå²
> æ½è±¡æ¨¡åï¼åµå¥ç `ColumnFamily>` æ å°
ç±»ååå¨çåºæ¬æ°æ®åå
æ¯åï¼åï¼å¼å¯¹ï¼ãåå¯ä»¥å¨åæï¼ç±»ä¼¼äº SQL çæ°æ®è¡¨ï¼ä¸è¢«åç»ãè¶
级åæååç»æ®éåæãä½ å¯ä»¥ä½¿ç¨è¡é®ç¬ç«è®¿é®æ¯ä¸åï¼å
·æç¸åè¡é®å¼çåç»æä¸è¡ãæ¯ä¸ªå¼é½å
å«çæ¬çæ¶é´æ³ç¨äºè§£å³çæ¬å²çªã
Google åå¸äºç¬¬ä¸ä¸ªåååå¨æ°æ®åº [Bigtable](http://www.read.seas.harvard.edu/~kohler/class/cs239-w08/chang06bigtable.pdf)ï¼å®å½±åäº Hadoop çæç³»ç»ä¸æ´»è·çå¼æºæ°æ®åº [HBase](https://www.mapr.com/blog/in-depth-look-hbase-architecture) å Facebook ç [Cassandra](http://docs.datastax.com/en/archived/cassandra/2.0/cassandra/architecture/architectureIntro_c.html)ãå BigTableï¼HBase å Cassandra è¿æ ·çåå¨ç³»ç»å°é®ä»¥åæ¯é¡ºåºåå¨ï¼å¯ä»¥é«æå°è¯»åé®åã
åååå¨å
·å¤é«å¯ç¨æ§åé«å¯æ©å±æ§ãé常被ç¨äºå¤§æ°æ®ç¸å
³åå¨ã
##### æ¥æºå延伸é
读ï¼åååå¨
- [SQL ä¸ NoSQL ç®å²](http://blog.grio.com/2015/11/sql-nosql-a-brief-history.html)
- [BigTable æ¶æ](http://www.read.seas.harvard.edu/~kohler/class/cs239-w08/chang06bigtable.pdf)
- [Hbase æ¶æ](https://www.mapr.com/blog/in-depth-look-hbase-architecture)
- [Cassandra æ¶æ](http://docs.datastax.com/en/archived/cassandra/2.0/cassandra/architecture/architectureIntro_c.html)
#### å¾æ°æ®åº
èµææ¥æºï¼å¾æ°æ®åº
> æ½è±¡æ¨¡åï¼ å¾
å¨å¾æ°æ®åºä¸ï¼ä¸ä¸ªèç¹å¯¹åºä¸æ¡è®°å½ï¼ä¸ä¸ªå¼§å¯¹åºä¸¤ä¸ªèç¹ä¹é´çå
³ç³»ãå¾æ°æ®åºè¢«ä¼åç¨äºè¡¨ç¤ºå¤é®ç¹å¤çå¤æå
³ç³»æå¤å¯¹å¤å
³ç³»ã
å¾æ°æ®åºä¸ºåå¨å¤æå
³ç³»çæ°æ®æ¨¡åï¼å¦ç¤¾äº¤ç½ç»ï¼æä¾äºå¾é«çæ§è½ãå®ä»¬ç¸å¯¹è¾æ°ï¼å°æªå¹¿æ³åºç¨ï¼æ¥æ¾å¼åå·¥å
·æè
èµæºç¸å¯¹è¾é¾ã许å¤å¾åªè½éè¿ [REST API](#表述æ§ç¶æ转移rest) 访é®ã
##### ç¸å
³èµæºå延伸é
读ï¼å¾
- [å¾æ°æ®åº](https://en.wikipedia.org/wiki/Graph_database)
- [Neo4j](https://neo4j.com/)
- [FlockDB](https://blog.twitter.com/2010/introducing-flockdb)
#### æ¥æºå延伸é
读ï¼NoSQL
- [æ°æ®åºæ¯è¯è§£é](http://stackoverflow.com/questions/3342497/explanation-of-base-terminology)
- [NoSQL æ°æ®åº - è°æ¥åå³çæå](https://medium.com/baqend-blog/nosql-databases-a-survey-and-decision-guidance-ea7823a822d#.wskogqenq)
- [å¯æ©å±æ§](http://www.lecloud.net/post/7994751381/scalability-for-dummies-part-2-database)
- [NoSQL ä»ç»](https://www.youtube.com/watch?v=qI_g07C_Q5I)
- [NoSQL 模å¼](http://horicky.blogspot.com/2009/11/nosql-patterns.html)
### SQL è¿æ¯ NoSQL
èµææ¥æºï¼ä» RDBMS 转æ¢å° NoSQL
éå **SQL** çåå :
- ç»æåæ°æ®
- ä¸¥æ ¼ç模å¼
- å
³ç³»åæ°æ®
- éè¦å¤æçèç»æä½
- äºå¡
- æ¸
æ°çæ©å±æ¨¡å¼
- æ¢æèµæºæ´ä¸°å¯ï¼å¼åè
ã社åºã代ç åºãå·¥å
·ç
- éè¿ç´¢å¼è¿è¡æ¥è¯¢é常快
éå **NoSQL** çåå ï¼
- åç»æåæ°æ®
- å¨ææçµæ´»ç模å¼
- éå
³ç³»åæ°æ®
- ä¸éè¦å¤æçèç»æä½
- åå¨ TB ï¼çè³ PBï¼çº§å«çæ°æ®
- é«æ°æ®å¯éçå·¥ä½è´è½½
- IOPS é«ååé
éå NoSQL ç示ä¾æ°æ®ï¼
- åç¹æ°æ®åæ¥å¿æ°æ®
- æè¡æ¦æè
å¾åæ°æ®
- 临æ¶æ°æ®ï¼å¦è´ç©è½¦
- é¢ç¹è®¿é®çï¼âçâï¼è¡¨
- å
æ°æ®ï¼æ¥æ¾è¡¨
##### æ¥æºå延伸é
读ï¼SQL æ NoSQL
- [æ©å±ä½ çç¨æ·æ°å°ç¬¬ä¸ä¸ªåä¸](https://www.youtube.com/watch?v=w95murBkYmU)
- [SQL å NoSQL çä¸å](https://www.sitepoint.com/sql-vs-nosql-differences/)
## ç¼å
èµææ¥æºï¼å¯æ©å±çç³»ç»è®¾è®¡æ¨¡å¼
ç¼åå¯ä»¥æé«é¡µé¢å è½½é度ï¼å¹¶å¯ä»¥åå°æå¡å¨åæ°æ®åºçè´è½½ãå¨è¿ä¸ªæ¨¡åä¸ï¼ååå¨å
æ¥ç请æ±ä¹åæ¯å¦è¢«ååºè¿ï¼å¦ææåå°ä¹åçç»æç´æ¥è¿åï¼æ¥çæçæ£çå¤çã
æ°æ®åºåçåååå¸ç读åæ¯æ好çãä½æ¯çé¨æ°æ®ä¼è®©è¯»ååå¸ä¸ååï¼è¿æ ·å°±ä¼é æç¶é¢ï¼å¦æå¨æ°æ®åºåå 个ç¼åï¼å°±ä¼æ¹å¹³ä¸ååçè´è½½åçªåæµé对æ°æ®åºçå½±åã
### 客æ·ç«¯ç¼å
ç¼åå¯ä»¥ä½äºå®¢æ·ç«¯ï¼æä½ç³»ç»æè
æµè§å¨ï¼ï¼[æå¡ç«¯](#åå代çweb-æå¡å¨)æè
ä¸åçç¼åå±ã
### CDN ç¼å
[CDN](#å
容ååç½ç»cdn) ä¹è¢«è§ä¸ºä¸ç§ç¼åã
### Web æå¡å¨ç¼å
[åå代ç](#åå代çweb-æå¡å¨)åç¼åï¼æ¯å¦ [Varnish](https://www.varnish-cache.org/)ï¼å¯ä»¥ç´æ¥æä¾éæåå¨æå
容ãWeb æå¡å¨åæ ·ä¹å¯ä»¥ç¼å请æ±ï¼è¿åç¸åºç»æèä¸å¿
è¿æ¥åºç¨æå¡å¨ã
### æ°æ®åºç¼å
æ°æ®åºçé»è®¤é
ç½®ä¸é常å
å«ç¼å级å«ï¼é对ä¸è¬ç¨ä¾è¿è¡äºä¼åãè°æ´é
ç½®ï¼å¨ä¸åæ
åµä¸ä½¿ç¨ä¸åç模å¼å¯ä»¥è¿ä¸æ¥æé«æ§è½ã
### åºç¨ç¼å
åºäºå
åçç¼åæ¯å¦ Memcached å Redis æ¯åºç¨ç¨åºåæ°æ®åå¨ä¹é´çä¸ç§é®å¼åå¨ãç±äºæ°æ®ä¿åå¨ RAM ä¸ï¼å®æ¯åå¨å¨ç£çä¸çå
¸åæ°æ®åºè¦å¿«å¤äºãRAM æ¯ç£çéå¶æ´å¤ï¼æ以ä¾å¦ [least recently used (LRU)](https://en.wikipedia.org/wiki/Cache_algorithms#Least_Recently_Used) ç[ç¼åæ æç®æ³](https://en.wikipedia.org/wiki/Cache_algorithms)å¯ä»¥å°ãçé¨æ°æ®ãæ¾å¨ RAM ä¸ï¼è对ä¸äºæ¯è¾ãå·é¨ãçæ°æ®ä¸åå¤çã
Redis æä¸åéå åè½ï¼
- æä¹
æ§é项
- å
ç½®æ°æ®ç»ææ¯å¦æåºéååå表
æå¤ä¸ªç¼å级å«ï¼å为两大类ï¼**æ°æ®åºæ¥è¯¢**å**对象**ï¼
- è¡çº§å«
- æ¥è¯¢çº§å«
- å®æ´çå¯åºåå对象
- å®å
¨æ¸²æç HTML
ä¸è¬æ¥è¯´ï¼ä½ åºè¯¥å°½éé¿å
åºäºæ件çç¼åï¼å 为è¿ä½¿å¾å¤å¶åèªå¨ç¼©æ¾å¾å°é¾ã
### æ°æ®åºæ¥è¯¢çº§å«çç¼å
å½ä½ æ¥è¯¢æ°æ®åºçæ¶åï¼å°æ¥è¯¢è¯å¥çåå¸å¼ä¸æ¥è¯¢ç»æåå¨å°ç¼åä¸ãè¿ç§æ¹æ³ä¼éå°ä»¥ä¸é®é¢ï¼
- å¾é¾ç¨å¤æçæ¥è¯¢å é¤å·²ç¼åç»æã
- å¦æä¸æ¡æ°æ®æ¯å¦è¡¨ä¸ææ¡æ°æ®çä¸é¡¹è¢«æ¹åï¼åéè¦å é¤ææå¯è½å
å«å·²æ´æ¹é¡¹çç¼åç»æã
### 对象级å«çç¼å
å°æ¨çæ°æ®è§ä¸ºå¯¹è±¡ï¼å°±å对å¾
ä½ çåºç¨ä»£ç ä¸æ ·ã让åºç¨ç¨åºå°æ°æ®ä»æ°æ®åºä¸ç»åå°ç±»å®ä¾ææ°æ®ç»æä¸ï¼
- å¦æ对象çåºç¡æ°æ®å·²ç»æ´æ¹äºï¼é£ä¹ä»ç¼åä¸å æè¿ä¸ªå¯¹è±¡ã
- å
许å¼æ¥å¤çï¼workers éè¿ä½¿ç¨ææ°çç¼å对象æ¥ç»è£
对象ã
建议ç¼åçå
容ï¼
- ç¨æ·ä¼è¯
- å®å
¨æ¸²æç Web 页é¢
- æ´»å¨æµ
- ç¨æ·å¾æ°æ®
### ä½æ¶æ´æ°ç¼å
ç±äºä½ åªè½å¨ç¼åä¸åå¨æéçæ°æ®ï¼æä»¥ä½ éè¦éæ©ä¸ä¸ªéç¨äºä½ ç¨ä¾çç¼åæ´æ°çç¥ã
#### ç¼å模å¼
èµææ¥æºï¼ä»ç¼åå°å
åæ°æ®ç½æ ¼
åºç¨ä»åå¨å¨è¯»åãç¼åä¸ååå¨å¨ç´æ¥äº¤äºï¼åºç¨æ§è¡ä»¥ä¸æä½ï¼
- å¨ç¼åä¸æ¥æ¾è®°å½ï¼å¦ææéæ°æ®ä¸å¨ç¼åä¸
- ä»æ°æ®åºä¸å è½½æéå
容
- å°æ¥æ¾å°çç»æåå¨å°ç¼åä¸
- è¿åæéå
容
```python
def get_user(self, user_id):
user = cache.get("user.{0}", user_id)
if user is None:
user = db.query("SELECT * FROM users WHERE user_id = {0}", user_id)
if user is not None:
key = "user.{0}".format(user_id)
cache.set(key, json.dumps(user))
return user
```
[Memcached](https://memcached.org/) é常ç¨è¿ç§æ¹å¼ä½¿ç¨ã
æ·»å å°ç¼åä¸çæ°æ®è¯»åé度å¾å¿«ãç¼å模å¼ä¹ç§°ä¸ºå»¶è¿å è½½ãåªç¼åæ请æ±çæ°æ®ï¼è¿é¿å
äºæ²¡æ被请æ±çæ°æ®å 满äºç¼å空é´ã
##### ç¼åç缺ç¹ï¼
- 请æ±çæ°æ®å¦æä¸å¨ç¼åä¸å°±éè¦ç»è¿ä¸ä¸ªæ¥éª¤æ¥è·åæ°æ®ï¼è¿ä¼å¯¼è´ææ¾ç延è¿ã
- å¦ææ°æ®åºä¸çæ°æ®æ´æ°äºä¼å¯¼è´ç¼åä¸çæ°æ®è¿æ¶ãè¿ä¸ªé®é¢éè¦éè¿è®¾ç½® TTL 强å¶æ´æ°ç¼åæè
ç´å模å¼æ¥ç¼è§£è¿ç§æ
åµã
- å½ä¸ä¸ªèç¹åºç°æ
éçæ¶åï¼å®å°ä¼è¢«ä¸ä¸ªæ°çèç¹æ¿ä»£ï¼è¿å¢å äºå»¶è¿çæ¶é´ã
#### ç´å模å¼
èµææ¥æºï¼å¯æ©å±æ§ãå¯ç¨æ§ã稳å®æ§ã模å¼
åºç¨ä½¿ç¨ç¼åä½ä¸ºä¸»è¦çæ°æ®åå¨ï¼å°æ°æ®è¯»åå°ç¼åä¸ï¼èç¼åè´è´£ä»æ°æ®åºä¸è¯»åæ°æ®ã
- åºç¨åç¼åä¸æ·»å /æ´æ°æ°æ®
- ç¼ååæ¥å°åå
¥æ°æ®åå¨
- è¿åæéå
容
åºç¨ä»£ç ï¼
```
set_user(12345, {"foo":"bar"})
```
ç¼å代ç ï¼
```python
def set_user(user_id, values):
user = db.query("UPDATE Users WHERE id = {0}", user_id, values)
cache.set(user_id, user)
```
ç±äºååæä½æ以ç´å模å¼æ´ä½æ¯ä¸ç§å¾æ
¢çæä½ï¼ä½æ¯è¯»åååå
¥çæ°æ®å¾å¿«ãç¸æ¯è¯»åæ°æ®ï¼ç¨æ·é常æ¯è¾è½æ¥åæ´æ°æ°æ®æ¶é度è¾æ
¢ãç¼åä¸çæ°æ®ä¸ä¼è¿æ¶ã
##### ç´å模å¼ç缺ç¹ï¼
- ç±äºæ
éæè
缩æ¾èå建çæ°çèç¹ï¼æ°çèç¹ä¸ä¼ç¼åï¼ç´å°æ°æ®åºæ´æ°ä¸ºæ¢ãç¼ååºç¨ç´å模å¼å¯ä»¥ç¼è§£è¿ä¸ªé®é¢ã
- åå
¥ç大å¤æ°æ°æ®å¯è½æ°¸è¿é½ä¸ä¼è¢«è¯»åï¼ç¨ TTL å¯ä»¥æå°åè¿ç§æ
åµçåºç°ã
#### åå模å¼
èµææ¥æºï¼å¯æ©å±æ§ãå¯ç¨æ§ã稳å®æ§ã模å¼
å¨åå模å¼ä¸ï¼åºç¨æ§è¡ä»¥ä¸æä½ï¼
- å¨ç¼åä¸å¢å æè
æ´æ°æ¡ç®
- å¼æ¥åå
¥æ°æ®ï¼æé«åå
¥æ§è½ã
##### åå模å¼ç缺ç¹ï¼
- ç¼åå¯è½å¨å
¶å
容æååå¨ä¹å丢失æ°æ®ã
- æ§è¡ç´å模å¼æ¯ç¼åæè
åå模å¼æ´å¤æã
#### å·æ°
èµææ¥æºï¼ä»ç¼åå°å
åæ°æ®ç½æ ¼
ä½ å¯ä»¥å°ç¼åé
ç½®æå¨å°æä¹åèªå¨å·æ°æè¿è®¿é®è¿çå
容ã
å¦æç¼åå¯ä»¥åç¡®é¢æµå°æ¥å¯è½è¯·æ±åªäºæ°æ®ï¼é£ä¹å·æ°å¯è½ä¼å¯¼è´å»¶è¿ä¸è¯»åæ¶é´çéä½ã
##### å·æ°ç缺ç¹ï¼
- ä¸è½åç¡®é¢æµå°æªæ¥éè¦ç¨å°çæ°æ®å¯è½ä¼å¯¼è´æ§è½ä¸å¦ä¸ä½¿ç¨å·æ°ã
### ç¼åç缺ç¹ï¼
- éè¦ä¿æç¼ååçå®æ°æ®æºä¹é´çä¸è´æ§ï¼æ¯å¦æ°æ®åºæ ¹æ®[ç¼åæ æ](https://en.wikipedia.org/wiki/Cache_algorithms)ã
- éè¦æ¹ååºç¨ç¨åºæ¯å¦å¢å Redis æè
memcachedã
- æ æç¼åæ¯ä¸ªé¾é¢ï¼ä»ä¹æ¶åæ´æ°ç¼åæ¯ä¸ä¹ç¸å
³çå¤æé®é¢ã
### ç¸å
³èµæºå延伸é
读
- [ä»ç¼åå°å
åæ°æ®](http://www.slideshare.net/tmatyashovsky/from-cache-to-in-memory-data-grid-introduction-to-hazelcast)
- [å¯æ©å±ç³»ç»è®¾è®¡æ¨¡å¼](http://horicky.blogspot.com/2010/10/scalable-system-design-patterns.html)
- [å¯ç¼©æ¾ç³»ç»ææ¶ä»ç»](http://lethain.com/introduction-to-architecting-systems-for-scale/)
- [å¯æ©å±æ§ï¼å¯ç¨æ§ï¼ç¨³å®æ§å模å¼](http://www.slideshare.net/jboner/scalability-availability-stability-patterns/)
- [å¯æ©å±æ§](http://www.lecloud.net/post/9246290032/scalability-for-dummies-part-3-cache)
- [AWS ElastiCache çç¥](http://docs.aws.amazon.com/AmazonElastiCache/latest/UserGuide/Strategies.html)
- [ç»´åºç¾ç§](https://en.wikipedia.org/wiki/Cache_(computing))
## å¼æ¥
èµææ¥æºï¼å¯ç¼©æ¾ç³»ç»ææ¶ä»ç»
å¼æ¥å·¥ä½æµæå©äºåå°é£äºåæ¬é¡ºåºæ§è¡ç请æ±æ¶é´ãå®ä»¬å¯ä»¥éè¿æåè¿è¡ä¸äºèæ¶çå·¥ä½æ¥å¸®å©åå°è¯·æ±æ¶é´ï¼æ¯å¦å®ææ±æ»æ°æ®ã
### æ¶æ¯éå
æ¶æ¯éåæ¥æ¶ï¼ä¿çåä¼ éæ¶æ¯ãå¦ææ顺åºæ§è¡æä½å¤ªæ
¢çè¯ï¼ä½ å¯ä»¥ä½¿ç¨æ以ä¸å·¥ä½æµçæ¶æ¯éåï¼
- åºç¨ç¨åºå°ä½ä¸åå¸å°éåï¼ç¶åéç¥ç¨æ·ä½ä¸ç¶æ
- ä¸ä¸ª worker ä»éåä¸ååºè¯¥ä½ä¸ï¼å¯¹å
¶è¿è¡å¤çï¼ç¶åæ¾ç¤ºè¯¥ä½ä¸å®æ
ä¸å»é»å¡ç¨æ·æä½ï¼ä½ä¸å¨åå°å¤çãå¨æ¤æé´ï¼å®¢æ·ç«¯å¯è½ä¼è¿è¡ä¸äºå¤ç使å¾çä¸å»åæ¯ä»»å¡å·²ç»å®æäºãä¾å¦ï¼å¦æè¦åéä¸æ¡æ¨æï¼æ¨æå¯è½ä¼é©¬ä¸åºç°å¨ä½ çæ¶é´çº¿ä¸ï¼ä½æ¯å¯è½éè¦ä¸äºæ¶é´æè½å°ä½ çæ¨ææ¨éå°ä½ çææå
³æ³¨è
é£éå»ã
**Redis** æ¯ä¸ä¸ªä»¤äººæ»¡æçç®åçæ¶æ¯ä»£çï¼ä½æ¯æ¶æ¯æå¯è½ä¼ä¸¢å¤±ã
**RabbitMQ** å¾å欢è¿ä½æ¯è¦æ±ä½ éåºãAMQPãå议并ä¸ç®¡çä½ èªå·±çèç¹ã
**Amazon SQS** æ¯è¢«æ管çï¼ä½å¯è½å
·æé«å»¶è¿ï¼å¹¶ä¸æ¶æ¯å¯è½ä¼è¢«ä¼ é两次ã
### ä»»å¡éå
ä»»å¡éåæ¥æ¶ä»»å¡åå
¶ç¸å
³æ°æ®ï¼è¿è¡å®ä»¬ï¼ç¶åä¼ éå
¶ç»æã å®ä»¬å¯ä»¥æ¯æè°åº¦ï¼å¹¶å¯ç¨äºå¨åå°è¿è¡è®¡ç®å¯éåä½ä¸ã
**Celery** æ¯æè°åº¦ï¼ä¸»è¦æ¯ç¨ Python å¼åçã
### èå
å¦æéåå¼å§ææ¾å¢é¿ï¼é£ä¹éå大å°å¯è½ä¼è¶
è¿å
å大å°ï¼å¯¼è´é«éç¼åæªå½ä¸ï¼ç£ç读åï¼çè³æ§è½æ´æ
¢ã[èå](http://mechanical-sympathy.blogspot.com/2012/05/apply-back-pressure-when-overloaded.html)å¯ä»¥éè¿éå¶éå大å°æ¥å¸®å©æ们ï¼ä»è为éåä¸çä½ä¸ä¿æé«ååçåè¯å¥½çååºæ¶é´ãä¸æ¦éå填满ï¼å®¢æ·ç«¯å°å¾å°æå¡å¨å¿æè
HTTP 503 ç¶æç ï¼ä»¥ä¾¿ç¨åéè¯ã客æ·ç«¯å¯ä»¥å¨ç¨åæ¶é´éè¯è¯¥è¯·æ±ï¼ä¹è®¸æ¯[ææ°éé¿](https://en.wikipedia.org/wiki/Exponential_backoff)ã
### å¼æ¥ç缺ç¹ï¼
- ç®åç计ç®åå®æ¶å·¥ä½æµçç¨ä¾å¯è½æ´éç¨äºåæ¥æä½ï¼å 为å¼å
¥éåå¯è½ä¼å¢å 延è¿åå¤ææ§ã
### ç¸å
³èµæºå延伸é
读
- [è¿æ¯ä¸ä¸ªæ°å游æ](https://www.youtube.com/watch?v=1KRYH75wgy4)
- [è¶
è½½æ¶åºç¨èå](http://mechanical-sympathy.blogspot.com/2012/05/apply-back-pressure-when-overloaded.html)
- [å©ç¹å°æ³å](https://en.wikipedia.org/wiki/Little%27s_law)
- [æ¶æ¯éåä¸ä»»å¡éåæä»ä¹åºå«ï¼](https://www.quora.com/What-is-the-difference-between-a-message-queue-and-a-task-queue-Why-would-a-task-queue-require-a-message-broker-like-RabbitMQ-Redis-Celery-or-IronMQ-to-function)
## é讯
èµææ¥æºï¼OSI 7å±æ¨¡å
### è¶
ææ¬ä¼ è¾åè®®ï¼HTTPï¼
HTTP æ¯ä¸ç§å¨å®¢æ·ç«¯åæå¡å¨ä¹é´ç¼ç åä¼ è¾æ°æ®çæ¹æ³ãå®æ¯ä¸ä¸ªè¯·æ±/ååºåè®®ï¼å®¢æ·ç«¯åæå¡ç«¯é对ç¸å
³å
容åå®æç¶æä¿¡æ¯ç请æ±åååºãHTTP æ¯ç¬ç«çï¼å
许请æ±åååºæµç»è®¸å¤æ§è¡è´è½½åè¡¡ï¼ç¼åï¼å å¯åå缩çä¸é´è·¯ç±å¨åæå¡å¨ã
ä¸ä¸ªåºæ¬ç HTTP 请æ±ç±ä¸ä¸ªå¨è¯ï¼æ¹æ³ï¼åä¸ä¸ªèµæºï¼ç«¯ç¹ï¼ç»æã 以ä¸æ¯å¸¸è§ç HTTP å¨è¯ï¼
| å¨è¯ | æè¿° | *å¹ç | å®å
¨æ§ | å¯ç¼å |
| ------ | -------------- | ---- | ---- | -------------- |
| GET | 读åèµæº | Yes | Yes | Yes |
| POST | å建èµæºæ触åå¤çæ°æ®çè¿ç¨ | No | No | Yesï¼å¦æååºå
å«å·æ°ä¿¡æ¯ |
| PUT | å建ææ¿æ¢èµæº | Yes | No | No |
| PATCH | é¨åæ´æ°èµæº | No | No | Yesï¼å¦æååºå
å«å·æ°ä¿¡æ¯ |
| DELETE | å é¤èµæº | Yes | No | No |
**å¤æ¬¡æ§è¡ä¸ä¼äº§çä¸åçç»æ**ã
HTTP æ¯ä¾èµäºè¾ä½çº§åè®®ï¼å¦ **TCP** å **UDP**ï¼çåºç¨å±åè®®ã
#### æ¥æºå延伸é
读ï¼HTTP
* [README](https://www.quora.com/What-is-the-difference-between-HTTP-protocol-and-TCP-protocol) +
* [HTTP æ¯ä»ä¹ï¼](https://www.nginx.com/resources/glossary/http/)
* [HTTP å TCP çåºå«](https://www.quora.com/What-is-the-difference-between-HTTP-protocol-and-TCP-protocol)
* [PUT å PATCHçåºå«](https://laracasts.com/discuss/channels/general-discussion/whats-the-differences-between-put-and-patch?page=1)
### ä¼ è¾æ§å¶åè®®ï¼TCPï¼
èµææ¥æºï¼å¦ä½å¶ä½å¤äººæ¸¸æ
TCP æ¯éè¿ [IP ç½ç»](https://en.wikipedia.org/wiki/Internet_Protocol)çé¢åè¿æ¥çåè®®ã 使ç¨[æ¡æ](https://en.wikipedia.org/wiki/Handshaking)建ç«åæå¼è¿æ¥ã åéçæææ°æ®å
ä¿è¯ä»¥åå§é¡ºåºå°è¾¾ç®çå°ï¼ç¨ä»¥ä¸æªæ½ä¿è¯æ°æ®å
ä¸è¢«æåï¼
- æ¯ä¸ªæ°æ®å
çåºåå·å[æ ¡éªç ](https://en.wikipedia.org/wiki/Transmission_Control_Protocol#Checksum_computation)ã
- [确认å
](https://en.wikipedia.org/wiki/Acknowledgement_(data_networks))åèªå¨éä¼
å¦æåéè
没ææ¶å°æ£ç¡®çååºï¼å®å°éæ°åéæ°æ®å
ãå¦æå¤æ¬¡è¶
æ¶ï¼è¿æ¥å°±ä¼æå¼ãTCP å®è¡[æµéæ§å¶](https://en.wikipedia.org/wiki/Flow_control_(data))å[æ¥å¡æ§å¶](https://en.wikipedia.org/wiki/Network_congestion#Congestion_control)ãè¿äºç¡®ä¿æªæ½ä¼å¯¼è´å»¶è¿ï¼èä¸é常导è´ä¼ è¾æçæ¯ UDP ä½ã
为äºç¡®ä¿é«ååéï¼Web æå¡å¨å¯ä»¥ä¿æ大éç TCP è¿æ¥ï¼ä»è导è´é«å
å使ç¨ãå¨ Web æå¡å¨çº¿ç¨é´æ¥æ大éå¼æ¾è¿æ¥å¯è½å¼é巨大ï¼æ¶èèµæºè¿å¤ï¼ä¹å°±æ¯è¯´ï¼ä¸ä¸ª [memcached](#memcached) æå¡å¨ã[è¿æ¥æ± ](https://en.wikipedia.org/wiki/Connection_pool) å¯ä»¥å¸®å©é¤äºå¨éç¨çæ
åµä¸åæ¢å° UDPã
TCP 对äºéè¦é«å¯é æ§ä½æ¶é´ç´§è¿«çåºç¨ç¨åºå¾æç¨ãæ¯å¦å
æ¬ Web æå¡å¨ï¼æ°æ®åºä¿¡æ¯ï¼SMTPï¼FTP å SSHã
以ä¸æ
åµä½¿ç¨ TCP ä»£æ¿ UDPï¼
- ä½ éè¦æ°æ®å®å¥½æ æã
- ä½ æ³å¯¹ç½ç»ååéèªå¨è¿è¡æä½³è¯ä¼°ã
### ç¨æ·æ°æ®æ¥åè®®ï¼UDPï¼
èµææ¥æºï¼å¦ä½å¶ä½å¤äººæ¸¸æ
UDP æ¯æ è¿æ¥çãæ°æ®æ¥ï¼ç±»ä¼¼äºæ°æ®å
ï¼åªå¨æ°æ®æ¥çº§å«æä¿è¯ãæ°æ®æ¥å¯è½ä¼æ åºçå°è¾¾ç®çå°ï¼ä¹æå¯è½ä¼é失ãUDP ä¸æ¯ææ¥å¡æ§å¶ãè½ç¶ä¸å¦ TCP é£æ ·æä¿è¯ï¼ä½ UDP é常æçæ´é«ã
UDP å¯ä»¥éè¿å¹¿æå°æ°æ®æ¥åéè³åç½å
çææ设å¤ãè¿å¯¹ [DHCP](https://en.wikipedia.org/wiki/Dynamic_Host_Configuration_Protocol) å¾æç¨ï¼å 为åç½å
ç设å¤è¿æ²¡æåé
IP å°åï¼è IP å¯¹äº TCP æ¯å¿
é¡»çã
UDP å¯é æ§æ´ä½ä½éåç¨å¨ç½ç»çµè¯ãè§é¢è天ï¼æµåªä½åå®æ¶å¤äººæ¸¸æä¸ã
以ä¸æ
åµä½¿ç¨ UDP ä»£æ¿ TCPï¼
* ä½ éè¦ä½å»¶è¿
* ç¸å¯¹äºæ°æ®ä¸¢å¤±æ´ç³çæ¯æ°æ®å»¶è¿
* ä½ æ³å®ç°èªå·±çéè¯¯æ ¡æ£æ¹æ³
#### æ¥æºå延伸é
读ï¼TCP ä¸ UDP
* [游æç¼ç¨çç½ç»](http://gafferongames.com/networking-for-game-programmers/udp-vs-tcp/)
* [TCP ä¸ UDP çå
³é®åºå«](http://www.cyberciti.biz/faq/key-differences-between-tcp-and-udp-protocols/)
* [TCP ä¸ UDP çä¸å](http://stackoverflow.com/questions/5970383/difference-between-tcp-and-udp)
* [ä¼ è¾æ§å¶åè®®](https://en.wikipedia.org/wiki/Transmission_Control_Protocol)
* [ç¨æ·æ°æ®æ¥åè®®](https://en.wikipedia.org/wiki/User_Datagram_Protocol)
* [Memcache å¨ Facebook çæ©å±](http://www.cs.bu.edu/~jappavoo/jappavoo.github.com/451/papers/memcache-fb.pdf)
### è¿ç¨è¿ç¨è°ç¨åè®®ï¼RPCï¼
Source: Crack the system design interview
å¨ RPC ä¸ï¼å®¢æ·ç«¯ä¼å»è°ç¨å¦ä¸ä¸ªå°å空é´ï¼é常æ¯ä¸ä¸ªè¿ç¨æå¡å¨ï¼éçæ¹æ³ãè°ç¨ä»£ç çèµ·æ¥å°±åæ¯è°ç¨çæ¯ä¸ä¸ªæ¬å°æ¹æ³ï¼å®¢æ·ç«¯åæå¡å¨äº¤äºçå
·ä½è¿ç¨è¢«æ½è±¡ãè¿ç¨è°ç¨ç¸å¯¹äºæ¬å°è°ç¨ä¸è¬è¾æ
¢èä¸å¯é æ§æ´å·®ï¼å æ¤åºå两è
æ¯æ帮å©çãçé¨ç RPC æ¡æ¶å
æ¬ [Protobuf](https://developers.google.com/protocol-buffers/)ã[Thrift](https://thrift.apache.org/) å [Avro](https://avro.apache.org/docs/current/)ã
RPC æ¯ä¸ä¸ªâ请æ±-ååºâåè®®ï¼
* **客æ·ç«¯ç¨åº** ââ è°ç¨å®¢æ·ç«¯åæ ¹ç¨åºãå°±åè°ç¨æ¬å°æ¹æ³ä¸æ ·ï¼åæ°ä¼è¢«åå
¥æ ä¸ã
* **客æ·ç«¯ stub ç¨åº** ââ å°è¯·æ±è¿ç¨ç id ååæ°æå
è¿è¯·æ±ä¿¡æ¯ä¸ã
* **客æ·ç«¯é信模å** ââ å°ä¿¡æ¯ä»å®¢æ·ç«¯åéè³æå¡ç«¯ã
* **æå¡ç«¯é信模å** ââ å°æ¥åçå
ä¼ ç»æå¡ç«¯åæ ¹ç¨åºã
* **æå¡ç«¯ stub ç¨åº** ââ å°ç»æ解å
ï¼ä¾æ®è¿ç¨ id è°ç¨æå¡ç«¯æ¹æ³å¹¶å°åæ°ä¼ éè¿å»ã
RPC è°ç¨ç¤ºä¾ï¼
```
GET /someoperation?data=anId
POST /anotheroperation
{
"data":"anId";
"anotherdata": "another value"
}
```
RPC ä¸æ³¨äºæ´é²æ¹æ³ãRPC é常ç¨äºå¤çå
é¨é讯çæ§è½é®é¢ï¼è¿æ ·ä½ å¯ä»¥æå¨å¤çæ¬å°è°ç¨ä»¥æ´å¥½çéåºä½ çæ
åµã
å½ä»¥ä¸æ
åµæ¶éæ©æ¬å°åºï¼ä¹å°±æ¯ SDKï¼ï¼
* ä½ ç¥éä½ çç®æ å¹³å°ã
* ä½ æ³æ§å¶å¦ä½è®¿é®ä½ çâé»è¾âã
* ä½ æ³å¯¹åçå¨ä½ çåºä¸çé误è¿è¡æ§å¶ã
* æ§è½åç»ç«¯ç¨æ·ä½éªæ¯ä½ æå
³å¿çäºã
éµå¾ª **REST** ç HTTP API å¾å¾æ´éç¨äºå
Œ
± APIã
#### 缺ç¹ï¼RPC
* RPC 客æ·ç«¯ä¸æå¡å®ç°æç»å°å¾ç´§å¯ã
* ä¸ä¸ªæ°ç API å¿
é¡»å¨æ¯ä¸ä¸ªæä½æè
ç¨ä¾ä¸å®ä¹ã
* RPC å¾é¾è°è¯ã
* ä½ å¯è½æ²¡åæ³å¾æ¹ä¾¿çå»ä¿®æ¹ç°æçææ¯ã举个ä¾åï¼å¦æä½ å¸æå¨ [Squid](http://www.squid-cache.org/) è¿æ ·çç¼åæå¡å¨ä¸ç¡®ä¿ [RPC 被æ£ç¡®ç¼å](http://etherealbits.com/2012/12/debunking-the-myths-of-rpc-rest/)çè¯å¯è½éè¦ä¸äºé¢å¤çåªåäºã
### 表述æ§ç¶æ转移ï¼RESTï¼
REST æ¯ä¸ç§å¼ºå¶ç客æ·ç«¯/æå¡ç«¯æ¶æ设计模åï¼å®¢æ·ç«¯åºäºæå¡ç«¯ç®¡ççä¸ç³»åèµæºæä½ãæå¡ç«¯æä¾ä¿®æ¹æè·åèµæºçæ¥å£ãææçéä¿¡å¿
é¡»æ¯æ ç¶æåå¯ç¼åçã
RESTful æ¥å£æåæ¡è§åï¼
* **æ å¿èµæºï¼HTTP éç URIï¼** ââ æ 论ä»ä¹æä½é½ä½¿ç¨åä¸ä¸ª URIã
* **表示çæ¹åï¼HTTP çå¨ä½ï¼** ââ 使ç¨å¨ä½, headers å bodyã
* **å¯èªææè¿°çé误信æ¯ï¼HTTP ä¸ç status codeï¼** ââ 使ç¨ç¶æç ï¼ä¸è¦éæ°é è½®åã
* **[HATEOAS](http://restcookbook.com/Basics/hateoas/)ï¼HTTP ä¸çHTML æ¥å£ï¼** ââ ä½ ç web æå¡å¨åºè¯¥è½å¤éè¿æµè§å¨è®¿é®ã
REST 请æ±çä¾åï¼
```
GET /someresources/anId
PUT /someresources/anId
{"anotherdata": "another value"}
```
REST å
³æ³¨äºæ´é²æ°æ®ãå®åå°äºå®¢æ·ç«¯ï¼æå¡ç«¯çè¦åç¨åº¦ï¼ç»å¸¸ç¨äºå
Œ
± HTTP API æ¥å£è®¾è®¡ãREST 使ç¨æ´é常ä¸è§èåçæ¹æ³æ¥éè¿ URI æ´é²èµæºï¼[éè¿ header æ¥è¡¨è¿°](https://github.com/for-GET/know-your-http-well/blob/master/headers.md)并éè¿ GETãPOSTãPUTãDELETE å PATCH è¿äºå¨ä½æ¥è¿è¡æä½ãå 为æ ç¶æçç¹æ§ï¼REST æäºæ¨ªåæ©å±åé离ã
#### 缺ç¹ï¼REST
* ç±äº REST å°éç¹æ¾å¨æ´é²æ°æ®ï¼æ以å½èµæºä¸æ¯èªç¶ç»ç»çæè
ç»æå¤æçæ¶åå®å¯è½æ æ³å¾å¥½çéåºã举个ä¾åï¼è¿åè¿å»ä¸å°æ¶ä¸ä¸ç¹å®äºä»¶éå¹é
çæ´æ°è®°å½è¿ç§æä½å°±å¾é¾è¡¨ç¤ºä¸ºè·¯å¾ãä½¿ç¨ RESTï¼å¯è½ä¼ä½¿ç¨ URI è·¯å¾ï¼æ¥è¯¢åæ°åå¯è½ç请æ±ä½æ¥å®ç°ã
* REST ä¸è¬ä¾èµå 个å¨ä½ï¼GETãPOSTãPUTãDELETE å PATCHï¼ï¼ä½ææ¶åä»
ä»
è¿äºæ²¡æ³æ»¡è¶³ä½ çéè¦ã举个ä¾åï¼å°è¿æçæ档移å¨å°å½æ¡£æ件夹éå»ï¼è¿æ ·çæä½å¯è½æ²¡æ³ç®åçç¨ä¸é¢è¿å 个 verbs 表达ã
* 为äºæ¸²æå个页é¢ï¼è·å被åµå¥å¨å±çº§ç»æä¸çå¤æèµæºéè¦å®¢æ·ç«¯ï¼æå¡å¨ä¹é´å¤æ¬¡å¾è¿éä¿¡ãä¾å¦ï¼è·åå客å
容åå
¶å
³èè¯è®ºã对äºä½¿ç¨ä¸ç¡®å®ç½ç»ç¯å¢ç移å¨åºç¨æ¥è¯´ï¼è¿äºå¤æ¬¡å¾è¿éä¿¡æ¯é常麻ç¦çã
* éçæ¶é´çæ¨ç§»ï¼æ´å¤çå段å¯è½ä¼è¢«æ·»å å° API ååºä¸ï¼è¾æ§ç客æ·ç«¯å°ä¼æ¥æ¶å°æææ°çæ°æ®å段ï¼å³ä½¿æ¯é£äºå®ä»¬ä¸éè¦çå段ï¼ç»æå®ä¼å¢å è´è½½å¤§å°å¹¶å¼èµ·æ´å¤§ç延è¿ã
### RPC ä¸ REST æ¯è¾
| æä½ | RPC | REST |
| ----------- | ---------------------------------------- | ---------------------------------------- |
| 注å | **POST** /signup | **POST** /persons |
| 注é | **POST** /resign
{
"personid": "1234"
} | **DELETE** /persons/1234 |
| 读åç¨æ·ä¿¡æ¯ | **GET** /readPerson?personid=1234 | **GET** /persons/1234 |
| 读åç¨æ·ç©åå表 | **GET** /readUsersItemsList?personid=1234 | **GET** /persons/1234/items |
| åç¨æ·ç©åå表添å ä¸é¡¹ | **POST** /addItemToUsersItemsList
{
"personid": "1234";
"itemid": "456"
} | **POST** /persons/1234/items
{
"itemid": "456"
} |
| æ´æ°ä¸ä¸ªç©å | **POST** /modifyItem
{
"itemid": "456";
"key": "value"
} | **PUT** /items/456
{
"key": "value"
} |
| å é¤ä¸ä¸ªç©å | **POST** /removeItem
{
"itemid": "456"
} | **DELETE** /items/456 |
èµææ¥æºï¼ä½ ççç¥éä½ ä¸ºä»ä¹æ´å欢 REST èä¸æ¯ RPC å
#### æ¥æºå延伸é
读ï¼REST ä¸ RPC
* [ä½ ççç¥éä½ ä¸ºä»ä¹æ´å欢 REST èä¸æ¯ RPC å](https://apihandyman.io/do-you-really-know-why-you-prefer-rest-over-rpc/)
* [ä»ä¹æ¶å RPC æ¯ REST æ´åéï¼](http://programmers.stackexchange.com/a/181186)
* [REST vs JSON-RPC](http://stackoverflow.com/questions/15056878/rest-vs-json-rpc)
* [æå¼ RPC å REST çç¥ç§é¢çº±](http://etherealbits.com/2012/12/debunking-the-myths-of-rpc-rest/)
* [ä½¿ç¨ REST ç缺ç¹æ¯ä»ä¹](https://www.quora.com/What-are-the-drawbacks-of-using-RESTful-APIs)
* [ç ´è§£ç³»ç»è®¾è®¡é¢è¯](http://www.puncsky.com/blog/2016-02-13-crack-the-system-design-interview)
* [Thrift](https://code.facebook.com/posts/1468950976659943/)
* [为ä»ä¹å¨å
é¨ä½¿ç¨ REST èä¸æ¯ RPC](http://arstechnica.com/civis/viewtopic.php?t=1190508)
## å®å
¨
è¿ä¸é¨åéè¦æ´å¤å
容ã[ä¸èµ·æ¥å§](#è´¡ç®)ï¼
å®å
¨æ¯ä¸ä¸ªå®½æ³çè¯é¢ãé¤éä½ æç¸å½çç»éªãå®å
¨æ¹é¢èæ¯æè
æ£å¨ç³è¯·çèä½è¦æ±å®å
¨ç¥è¯ï¼ä½ ä¸éè¦äºè§£å®å
¨åºç¡ç¥è¯ä»¥å¤çå
容ï¼
* å¨è¿è¾åçå¾
è¿ç¨ä¸å å¯
* 对ææçç¨æ·è¾å
¥åä»ç¨æ·é£éåæ¥çåæ°è¿è¡å¤ç以é²æ¢ [XSS](https://en.wikipedia.org/wiki/Cross-site_scripting) å [SQL 注å
¥](https://en.wikipedia.org/wiki/SQL_injection)ã
* 使ç¨åæ°åçæ¥è¯¢æ¥é²æ¢ SQL 注å
¥ã
* 使ç¨[æå°æéåå](https://en.wikipedia.org/wiki/Principle_of_least_privilege)ã
### æ¥æºå延伸é
读
* [为å¼åè
åå¤çå®å
¨å¼å¯¼](https://github.com/FallibleInc/security-guide-for-developers)
* [OWASP top ten](https://www.owasp.org/index.php/OWASP_Top_Ten_Cheat_Sheet)
## éå½
ä¸äºæ¶åä½ ä¼è¢«è¦æ±ååºä¿å®ä¼°è®¡ãæ¯å¦ï¼ä½ å¯è½éè¦ä¼°è®¡ä»ç£çä¸çæ 100 å¼ å¾çç缩ç¥å¾éè¦çæ¶é´æè
ä¸ä¸ªæ°æ®ç»æéè¦å¤å°çå
åã**2 ç次æ¹è¡¨**å**æ¯ä¸ªå¼åè
é½éè¦ç¥éçä¸äºæ¶é´æ°æ®**ï¼è¯æ³¨ï¼OSChina ä¸æè¿ç¯æç« ç[è¯æ](https://www.oschina.net/news/30009/every-programmer-should-know)ï¼é½æ¯ä¸äºå¾æ¹ä¾¿çåèèµæã
### 2 ç次æ¹è¡¨
```
Power Exact Value Approx Value Bytes
---------------------------------------------------------------
7 128
8 256
10 1024 1 thousand 1 KB
16 65,536 64 KB
20 1,048,576 1 million 1 MB
30 1,073,741,824 1 billion 1 GB
32 4,294,967,296 4 GB
40 1,099,511,627,776 1 trillion 1 TB
```
#### æ¥æºå延伸é
读
* [2 ç次æ¹](https://en.wikipedia.org/wiki/Power_of_two)
### æ¯ä¸ªç¨åºåé½åºè¯¥ç¥éç延è¿æ°
```
Latency Comparison Numbers
--------------------------
L1 cache reference 0.5 ns
Branch mispredict 5 ns
L2 cache reference 7 ns 14x L1 cache
Mutex lock/unlock 25 ns
Main memory reference 100 ns 20x L2 cache, 200x L1 cache
Compress 1K bytes with Zippy 10,000 ns 10 us
Send 1 KB bytes over 1 Gbps network 10,000 ns 10 us
Read 4 KB randomly from SSD* 150,000 ns 150 us ~1GB/sec SSD
Read 1 MB sequentially from memory 250,000 ns 250 us
Round trip within same datacenter 500,000 ns 500 us
Read 1 MB sequentially from SSD* 1,000,000 ns 1,000 us 1 ms ~1GB/sec SSD, 4X memory
Disk seek 10,000,000 ns 10,000 us 10 ms 20x datacenter roundtrip
Read 1 MB sequentially from 1 Gbps 10,000,000 ns 10,000 us 10 ms 40x memory, 10X SSD
Read 1 MB sequentially from disk 30,000,000 ns 30,000 us 30 ms 120x memory, 30X SSD
Send packet CA->Netherlands->CA 150,000,000 ns 150,000 us 150 ms
Notes
-----
1 ns = 10^-9 seconds
1 us = 10^-6 seconds = 1,000 ns
1 ms = 10^-3 seconds = 1,000 us = 1,000,000 ns
```
åºäºä¸è¿°æ°åçææ ï¼
* ä»ç£ç以 30 MB/s çé度顺åºè¯»å
* 以 100 MB/s ä» 1 Gbps ç以太ç½é¡ºåºè¯»å
* ä» SSD 以 1 GB/s çé度读å
* 以 4 GB/s çé度ä»ä¸»å读å
* æ¯ç§è½ç»å°ç 6-7 å
* æ°æ®ä¸å¿å
æ¯ç§æ 2,000 次å¾è¿
#### 延è¿æ°å¯è§å
![](https://camo.githubusercontent.com/77f72259e1eb58596b564d1ad823af1853bc60a3/687474703a2f2f692e696d6775722e636f6d2f6b307431652e706e67)
#### æ¥æºå延伸é
读
* [æ¯ä¸ªç¨åºåé½åºè¯¥ç¥éç延è¿æ° â 1](https://gist.github.com/jboner/2841832)
* [æ¯ä¸ªç¨åºåé½åºè¯¥ç¥éç延è¿æ° â 2](https://gist.github.com/hellerbarde/2843375)
* [å
³äºå»ºè®¾å¤§ååå¸å¼ç³»ç»çç设计æ¹æ¡ã课ç¨å建议](http://www.cs.cornell.edu/projects/ladis2009/talks/dean-keynote-ladis2009.pdf)
* [å
³äºå»ºè®¾å¤§åå¯æå±åå¸å¼ç³»ç»ç软件工ç¨å¨è¯¢](https://static.googleusercontent.com/media/research.google.com/en//people/jeff/stanford-295-talk.pdf)
### å
¶å®çç³»ç»è®¾è®¡é¢è¯é¢
> 常è§çç³»ç»è®¾è®¡é¢è¯é®é¢ï¼ç»åºäºå¦ä½è§£å³çæ¹æ¡é¾æ¥
| é®é¢ | å¼ç¨ |
| ----------------------- | ---------------------------------------- |
| è®¾è®¡ç±»ä¼¼äº Dropbox çæ件åæ¥æå¡ | [youtube.com](https://www.youtube.com/watch?v=PE4gwstWhmc) |
| è®¾è®¡ç±»ä¼¼äº Google çæç´¢å¼æ | [queue.acm.org](http://queue.acm.org/detail.cfm?id=988407)
[stackexchange.com](http://programmers.stackexchange.com/questions/38324/interview-question-how-would-you-implement-google-search)
[ardendertat.com](http://www.ardendertat.com/2012/01/11/implementing-search-engines/)
[stanford.edu](http://infolab.stanford.edu/~backrub/google.html) |
| è®¾è®¡ç±»ä¼¼äº Google çå¯æ©å±ç½ç»ç¬è« | [quora.com](https://www.quora.com/How-can-I-build-a-web-crawler-from-scratch) |
| 设计 Google ææ¡£ | [code.google.com](https://code.google.com/p/google-mobwrite/)
[neil.fraser.name](https://neil.fraser.name/writing/sync/) |
| 设计类似 Redis çé®å¼åå¨ | [slideshare.net](http://www.slideshare.net/dvirsky/introduction-to-redis) |
| 设计类似 Memcached çç¼åç³»ç» | [slideshare.net](http://www.slideshare.net/oemebamo/introduction-to-memcached) |
| 设计类似äºé©¬éçæ¨èç³»ç» | [hulu.com](http://tech.hulu.com/blog/2011/09/19/recommendation-system.html)
[ijcai13.org](http://ijcai13.org/files/tutorial_slides/td3.pdf) |
| 设计类似 Bitly ççé¾æ¥ç³»ç» | [n00tc0d3r.blogspot.com](http://n00tc0d3r.blogspot.com/) |
| 设计类似 WhatsApp çè天åºç¨ | [highscalability.com](http://highscalability.com/blog/2014/2/26/the-whatsapp-architecture-facebook-bought-for-19-billion.html) |
| 设计类似 Instagram çå¾çåäº«ç³»ç» | [highscalability.com](http://highscalability.com/flickr-architecture)
[highscalability.com](http://highscalability.com/blog/2011/12/6/instagram-architecture-14-million-users-terabytes-of-photos.html) |
| 设计 Facebook çæ°é»æ¨èæ¹æ³ | [quora.com](http://www.quora.com/What-are-best-practices-for-building-something-like-a-News-Feed)
[quora.com](http://www.quora.com/Activity-Streams/What-are-the-scaling-issues-to-keep-in-mind-while-developing-a-social-network-feed)
[slideshare.net](http://www.slideshare.net/danmckinley/etsy-activity-feeds-architecture) |
| 设计 Facebook çæ¶é´çº¿ç³»ç» | [facebook.com](https://www.facebook.com/note.php?note_id=10150468255628920)
[highscalability.com](http://highscalability.com/blog/2012/1/23/facebook-timeline-brought-to-you-by-the-power-of-denormaliza.html) |
| 设计 Facebook çèå¤©ç³»ç» | [erlang-factory.com](http://www.erlang-factory.com/upload/presentations/31/EugeneLetuchy-ErlangatFacebook.pdf)
[facebook.com](https://www.facebook.com/note.php?note_id=14218138919&id=9445547199&index=0) |
| 设计类似 Facebook çå¾è¡¨æç´¢ç³»ç» | [facebook.com](https://www.facebook.com/notes/facebook-engineering/under-the-hood-building-out-the-infrastructure-for-graph-search/10151347573598920)
[facebook.com](https://www.facebook.com/notes/facebook-engineering/under-the-hood-indexing-and-ranking-in-graph-search/10151361720763920)
[facebook.com](https://www.facebook.com/notes/facebook-engineering/under-the-hood-the-natural-language-interface-of-graph-search/10151432733048920) |
| 设计类似 CloudFlare çå
å®¹ä¼ éç½ç» | [cmu.edu](http://repository.cmu.edu/cgi/viewcontent.cgi?article=2112&context=compsci) |
| 设计类似 Twitter ççé¨è¯é¢ç³»ç» | [michael-noll.com](http://www.michael-noll.com/blog/2013/01/18/implementing-real-time-trending-topics-in-storm/)
[snikolov .wordpress.com](http://snikolov.wordpress.com/2012/11/14/early-detection-of-twitter-trends/) |
| 设计ä¸ä¸ªéæº ID çæç³»ç» | [blog.twitter.com](https://blog.twitter.com/2010/announcing-snowflake)
[github.com](https://github.com/twitter/snowflake/) |
| è¿åä¸å®æ¶é´æ®µå
次æ°å k é«çè¯·æ± | [ucsb.edu](https://icmi.cs.ucsb.edu/research/tech_reports/reports/2005-23.pdf)
[wpi.edu](http://davis.wpi.edu/xmdv/docs/EDBT11-diyang.pdf) |
| 设计ä¸ä¸ªæ°æ®æºäºå¤ä¸ªæ°æ®ä¸å¿çæå¡ç³»ç» | [highscalability.com](http://highscalability.com/blog/2009/8/24/how-google-serves-data-from-multiple-datacenters.html) |
| 设计ä¸ä¸ªå¤äººç½ç»å¡ç游æ | [indieflashblog.com](https://web.archive.org/web/20180929181117/http://www.indieflashblog.com/how-to-create-an-asynchronous-multiplayer-game.html)
[buildnewgames.com](http://buildnewgames.com/real-time-multiplayer/) |
| 设计ä¸ä¸ªåå¾åæ¶ç³»ç» | [stuffwithstuff.com](http://journal.stuffwithstuff.com/2013/12/08/babys-first-garbage-collector/)
[washington.edu](http://courses.cs.washington.edu/courses/csep521/07wi/prj/rick.pdf) |
| æ·»å æ´å¤çç³»ç»è®¾è®¡é®é¢ | [è´¡ç®](#è´¡ç®) |
### çå®æ¶æ
> å
³äºç°å®ä¸çå®çç³»ç»æ¯æä¹è®¾è®¡çæç« ã
Source: Twitter timelines at scale
**ä¸è¦ä¸æ³¨äºä»¥ä¸æç« çç»èï¼ä¸æ³¨äºä»¥ä¸æ¹é¢ï¼**
* åç°è¿äºæç« ä¸çå
±åçååãææ¯å模å¼ã
* å¦ä¹ æ¯ä¸ªç»ä»¶è§£å³åªäºé®é¢ï¼ä»ä¹æ
åµä¸ä½¿ç¨ï¼ä»ä¹æ
åµä¸ä¸éç¨
* å¤ä¹ å¦è¿çæç«
| ç±»å | ç³»ç» | å¼ç¨ |
| --------------- | ---------------------------------------- | ---------------------------------------- |
| Data processing | **MapReduce** - Googleçåå¸å¼æ°æ®å¤ç | [research.google.com](http://static.googleusercontent.com/media/research.google.com/zh-CN/us/archive/mapreduce-osdi04.pdf) |
| Data processing | **Spark** - Databricks çåå¸å¼æ°æ®å¤ç | [slideshare.net](http://www.slideshare.net/AGrishchenko/apache-spark-architecture) |
| Data processing | **Storm** - Twitter çåå¸å¼æ°æ®å¤ç | [slideshare.net](http://www.slideshare.net/previa/storm-16094009) |
| | | |
| Data store | **Bigtable** - Google çåå¼æ°æ®åº | [harvard.edu](http://www.read.seas.harvard.edu/~kohler/class/cs239-w08/chang06bigtable.pdf) |
| Data store | **HBase** - Bigtable çå¼æºå®ç° | [slideshare.net](http://www.slideshare.net/alexbaranau/intro-to-hbase) |
| Data store | **Cassandra** - Facebook çåå¼æ°æ®åº | [slideshare.net](http://www.slideshare.net/planetcassandra/cassandra-introduction-features-30103666) |
| Data store | **DynamoDB** - Amazon çææ¡£æ°æ®åº | [harvard.edu](http://www.read.seas.harvard.edu/~kohler/class/cs239-w08/decandia07dynamo.pdf) |
| Data store | **MongoDB** - ææ¡£æ°æ®åº | [slideshare.net](http://www.slideshare.net/mdirolf/introduction-to-mongodb) |
| Data store | **Spanner** - Google çå
¨çåå¸æ°æ®åº | [research.google.com](http://research.google.com/archive/spanner-osdi2012.pdf) |
| Data store | **Memcached** - åå¸å¼å
åç¼åç³»ç» | [slideshare.net](http://www.slideshare.net/oemebamo/introduction-to-memcached) |
| Data store | **Redis** - è½å¤æä¹
ååå
·æå¼ç±»åçåå¸å¼å
åç¼åç³»ç» | [slideshare.net](http://www.slideshare.net/dvirsky/introduction-to-redis) |
| | | |
| File system | **Google File System (GFS)** - åå¸å¼æä»¶ç³»ç» | [research.google.com](http://static.googleusercontent.com/media/research.google.com/zh-CN/us/archive/gfs-sosp2003.pdf) |
| File system | **Hadoop File System (HDFS)** - GFS çå¼æºå®ç° | [apache.org](https://hadoop.apache.org/docs/r1.2.1/hdfs_design.html) |
| | | |
| Misc | **Chubby** - Google çåå¸å¼ç³»ç»çä½è¦åéæå¡ | [research.google.com](http://static.googleusercontent.com/external_content/untrusted_dlcp/research.google.com/en/us/archive/chubby-osdi06.pdf) |
| Misc | **Dapper** - åå¸å¼ç³»ç»è·è¸ªåºç¡è®¾æ½ | [research.google.com](http://static.googleusercontent.com/media/research.google.com/en//pubs/archive/36356.pdf) |
| Misc | **Kafka** - LinkedIn çåå¸è®¢é
æ¶æ¯ç³»ç» | [slideshare.net](http://www.slideshare.net/mumrah/kafka-talk-tri-hug) |
| Misc | **Zookeeper** - éä¸çåºç¡æ¶æååè°æå¡ | [slideshare.net](http://www.slideshare.net/sauravhaloi/introduction-to-apache-zookeeper) |
| | æ·»å æ´å¤ | [è´¡ç®](#è´¡ç®) |
### å
¬å¸çç³»ç»æ¶æ
| Company | Reference(s) |
| -------------- | ---------------------------------------- |
| Amazon | [Amazon çæ¶æ](http://highscalability.com/amazon-architecture) |
| Cinchcast | [æ¯å¤©äº§ç 1500 å°æ¶çé³é¢](http://highscalability.com/blog/2012/7/16/cinchcast-architecture-producing-1500-hours-of-audio-every-d.html) |
| DataSift | [æ¯ç§å®æ¶ææ 120000 æ¡ tweet](http://highscalability.com/blog/2011/11/29/datasift-architecture-realtime-datamining-at-120000-tweets-p.html) |
| DropBox | [æ们å¦ä½ç¼©æ¾ Dropbox](https://www.youtube.com/watch?v=PE4gwstWhmc) |
| ESPN | [æ¯ç§æä½ 100000 次](http://highscalability.com/blog/2013/11/4/espns-architecture-at-scale-operating-at-100000-duh-nuh-nuhs.html) |
| Google | [Google çæ¶æ](http://highscalability.com/google-architecture) |
| Instagram | [1400 ä¸ç¨æ·ï¼è¾¾å°å
级å«çç
§çåå¨](http://highscalability.com/blog/2011/12/6/instagram-architecture-14-million-users-terabytes-of-photos.html)
[æ¯ä»ä¹å¨é©±å¨ Instagram](http://instagram-engineering.tumblr.com/post/13649370142/what-powers-instagram-hundreds-of-instances) |
| Justin.tv | [Justin.Tv çç´æ广ææ¶æ](http://highscalability.com/blog/2010/3/16/justintvs-live-video-broadcasting-architecture.html) |
| Facebook | [Facebook çå¯æ©å± memcached](https://cs.uwaterloo.ca/~brecht/courses/854-Emerging-2014/readings/key-value/fb-memcached-nsdi-2013.pdf)
[TAO: Facebook 社交å¾çåå¸å¼æ°æ®åå¨](https://cs.uwaterloo.ca/~brecht/courses/854-Emerging-2014/readings/data-store/tao-facebook-distributed-datastore-atc-2013.pdf)
[Facebook çå¾çåå¨](https://www.usenix.org/legacy/event/osdi10/tech/full_papers/Beaver.pdf) |
| Flickr | [Flickr çæ¶æ](http://highscalability.com/flickr-architecture) |
| Mailbox | [å¨ 6 å¨å
ä» 0 å° 100 ä¸ç¨æ·](http://highscalability.com/blog/2013/6/18/scaling-mailbox-from-0-to-one-million-users-in-6-weeks-and-1.html) |
| Pinterest | [ä»é¶å°æ¯ææ°å亿çæµè§é](http://highscalability.com/blog/2013/4/15/scaling-pinterest-from-0-to-10s-of-billions-of-page-views-a.html)
[1800 ä¸è®¿é®ç¨æ·ï¼10 åå¢é¿ï¼12 ååå·¥](http://highscalability.com/blog/2012/5/21/pinterest-architecture-update-18-million-visitors-10x-growth.html) |
| Playfish | [æç¨æ·é 5000 ä¸å¹¶å¨ä¸æå¢é¿](http://highscalability.com/blog/2010/9/21/playfishs-social-gaming-architecture-50-million-monthly-user.html) |
| PlentyOfFish | [PlentyOfFish çæ¶æ](http://highscalability.com/plentyoffish-architecture) |
| Salesforce | [ä»ä»¬æ¯å¤©å¦ä½å¤ç 13 亿ç¬äº¤æ](http://highscalability.com/blog/2013/9/23/salesforce-architecture-how-they-handle-13-billion-transacti.html) |
| Stack Overflow | [Stack Overflow çæ¶æ](http://highscalability.com/blog/2009/8/5/stack-overflow-architecture.html) |
| TripAdvisor | [40M 访é®è
ï¼200M 页é¢æµè§éï¼30TB æ°æ®](http://highscalability.com/blog/2011/6/27/tripadvisor-architecture-40m-visitors-200m-dynamic-page-view.html) |
| Tumblr | [æ¯æ 150 亿çæµè§é](http://highscalability.com/blog/2012/2/13/tumblr-architecture-15-billion-page-views-a-month-and-harder.html) |
| Twitter | [Making Twitter 10000 percent faster](http://highscalability.com/scaling-twitter-making-twitter-10000-percent-faster)
[æ¯å¤©ä½¿ç¨ MySQL åå¨2.5äº¿æ¡ tweet](http://highscalability.com/blog/2011/12/19/how-twitter-stores-250-million-tweets-a-day-using-mysql.html)
[150M æ´»è·ç¨æ·ï¼300K QPSï¼22 MB/S çé²ç«å¢](http://highscalability.com/blog/2013/7/8/the-architecture-twitter-uses-to-deal-with-150m-active-users.html)
[å¯æ©å±æ¶é´è¡¨](https://www.infoq.com/presentations/Twitter-Timeline-Scalability)
[Twitter ç大å°æ°æ®](https://www.youtube.com/watch?v=5cKTP36HVgI)
[Twitter çè¡ä¸ºï¼è§æ¨¡è¶
è¿ 1 亿ç¨æ·](https://www.youtube.com/watch?v=z8LU0Cj6BOU) |
| Uber | [Uber å¦ä½æ©å±èªå·±çå®æ¶åå¸åº](http://highscalability.com/blog/2015/9/14/how-uber-scales-their-real-time-market-platform.html) |
| WhatsApp | [Facebook ç¨ 190 亿ç¾å
è´ä¹° WhatsApp çæ¶æ](http://highscalability.com/blog/2014/2/26/the-whatsapp-architecture-facebook-bought-for-19-billion.html) |
| YouTube | [YouTube çå¯æ©å±æ§](https://www.youtube.com/watch?v=w5WVu624fY8)
[YouTube çæ¶æ](http://highscalability.com/youtube-architecture) |
### å
¬å¸å·¥ç¨å客
> ä½ å³å°é¢è¯çå
¬å¸çæ¶æ
>
> ä½ é¢å¯¹çé®é¢å¯è½å°±æ¥èªäºåæ ·é¢å
* [Airbnb Engineering](http://nerds.airbnb.com/)
* [Atlassian Developers](https://developer.atlassian.com/blog/)
* [Autodesk Engineering](http://cloudengineering.autodesk.com/blog/)
* [AWS Blog](https://aws.amazon.com/blogs/aws/)
* [Bitly Engineering Blog](http://word.bitly.com/)
* [Box Blogs](https://www.box.com/blog/engineering/)
* [Cloudera Developer Blog](http://blog.cloudera.com/blog/)
* [Dropbox Tech Blog](https://tech.dropbox.com/)
* [Engineering at Quora](http://engineering.quora.com/)
* [Ebay Tech Blog](http://www.ebaytechblog.com/)
* [Evernote Tech Blog](https://blog.evernote.com/tech/)
* [Etsy Code as Craft](http://codeascraft.com/)
* [Facebook Engineering](https://www.facebook.com/Engineering)
* [Flickr Code](http://code.flickr.net/)
* [Foursquare Engineering Blog](http://engineering.foursquare.com/)
* [GitHub Engineering Blog](https://github.blog/category/engineering)
* [Google Research Blog](http://googleresearch.blogspot.com/)
* [Groupon Engineering Blog](https://engineering.groupon.com/)
* [Heroku Engineering Blog](https://engineering.heroku.com/)
* [Hubspot Engineering Blog](http://product.hubspot.com/blog/topic/engineering)
* [High Scalability](http://highscalability.com/)
* [Instagram Engineering](http://instagram-engineering.tumblr.com/)
* [Intel Software Blog](https://software.intel.com/en-us/blogs/)
* [Jane Street Tech Blog](https://blogs.janestreet.com/category/ocaml/)
* [LinkedIn Engineering](http://engineering.linkedin.com/blog)
* [Microsoft Engineering](https://engineering.microsoft.com/)
* [Microsoft Python Engineering](https://blogs.msdn.microsoft.com/pythonengineering/)
* [Netflix Tech Blog](http://techblog.netflix.com/)
* [Paypal Developer Blog](https://devblog.paypal.com/category/engineering/)
* [Pinterest Engineering Blog](http://engineering.pinterest.com/)
* [Quora Engineering](https://engineering.quora.com/)
* [Reddit Blog](http://www.redditblog.com/)
* [Salesforce Engineering Blog](https://developer.salesforce.com/blogs/engineering/)
* [Slack Engineering Blog](https://slack.engineering/)
* [Spotify Labs](https://labs.spotify.com/)
* [Twilio Engineering Blog](http://www.twilio.com/engineering)
* [Twitter Engineering](https://engineering.twitter.com/)
* [Uber Engineering Blog](http://eng.uber.com/)
* [Yahoo Engineering Blog](http://yahooeng.tumblr.com/)
* [Yelp Engineering Blog](http://engineeringblog.yelp.com/)
* [Zynga Engineering Blog](https://www.zynga.com/blogs/engineering)
#### æ¥æºå延伸é
读
* [kilimchoi/engineering-blogs](https://github.com/kilimchoi/engineering-blogs)
## æ£å¨å®åä¸
æå
´è¶£å å
¥æ·»å ä¸äºé¨åæè
帮å©å®åæäºé¨ååï¼[å å
¥è¿æ¥å§](#è´¡ç®)ï¼
* ä½¿ç¨ MapReduce è¿è¡åå¸å¼è®¡ç®
* ä¸è´æ§åå¸
* ç´æ¥åå¨å¨è®¿é®ï¼DMAï¼æ§å¶å¨
* [è´¡ç®](#è´¡ç®)
## è´è°¢
æ´ä¸ªä»åºé½æä¾äºè¯ä¹¦åæº
ç¹å«é¸£è°¢ï¼
* [Hired in tech](http://www.hiredintech.com/system-design/the-system-design-process/)
* [Cracking the coding interview](https://www.amazon.com/dp/0984782850/)
* [High scalability](http://highscalability.com/)
* [checkcheckzz/system-design-interview](https://github.com/checkcheckzz/system-design-interview)
* [shashank88/system_design](https://github.com/shashank88/system_design)
* [mmcgrana/services-engineering](https://github.com/mmcgrana/services-engineering)
* [System design cheat sheet](https://gist.github.com/vasanthk/485d1c25737e8e72759f)
* [A distributed systems reading list](http://dancres.github.io/Pages/)
* [Cracking the system design interview](http://www.puncsky.com/blog/2016-02-13-crack-the-system-design-interview)
## èç³»æ¹å¼
欢è¿èç³»æ讨论æ¬æçä¸è¶³ãé®é¢æè
æè§ã
å¯ä»¥å¨æç [GitHub 主页](https://github.com/donnemartin)ä¸æ¾å°æçèç³»æ¹å¼
## 许å¯
Creative Commons Attribution 4.0 International License (CC BY 4.0)
http://creativecommons.org/licenses/by/4.0/