大綱
- 同步
- 網(wǎng)絡(luò)
- 數(shù)據(jù)庫
- 分布式
- 性能
- 估算
- 面向?qū)ο?/li>
案例
- 社交網(wǎng)站信息流
- 日志統(tǒng)計
- 網(wǎng)絡(luò)爬蟲
- 電商產(chǎn)品頁面
Concurrency
Thread vs. Process
Consumer and Producer
Blockingqueue
Tracking:(log記錄)
Synchronized(同步):每次請求直接寫入磁盤
Asynchronized(異步):先放入緩沖區(qū)闺鲸,每隔一段時間刷新到磁盤上
Network
OSI模型(上到下封包)
- Application Layer(*http1.0/1.1協(xié)議,應(yīng)用層)
- Presentaation Layer(應(yīng)用層)
- Session Layer(會話層)
- Transport Layer(*TCP,UDP協(xié)議禽篱,傳輸層)
- Network Layer(網(wǎng)絡(luò)層)
- Data Link Layer(數(shù)據(jù)連接層)
- Physical Layer(物理層)
Visit URL
What happens after you typed a URL in your browser and pressed return key?
(*尋址與建立鏈接是關(guān)鍵)
- 訪問DNS
- DNS返回網(wǎng)頁服務(wù)器IP地址
- 與網(wǎng)頁服務(wù)器建立連接(三次無首惊完,80端口)
- 瀏覽器與服務(wù)器建立http會話(接受數(shù)據(jù))
- 瀏覽器解析數(shù)據(jù),渲染網(wǎng)頁
- 關(guān)閉瀏覽器褒侧,終止http會話
Database
Relational DB vs. KV Store(關(guān)系型數(shù)據(jù)庫VS.KeyValue存儲)
Sharding vs. Clustering(分片VS.集群)
TinyURL:
Store the mapping from shortlink code to full URL.
document:
- code:varchar(8)
- url:varchar(1000)
- created_at:timestamp
- We also need to store the reverse mapping from URL back to code.
Distribute System
How to scala Tiny URL service?(規(guī)亩范簦化Tiny URL服務(wù))
- Stateless frontend servers behind a load balancer(負(fù)載均衡)
- Sharded/replicated database(on shortlink code)(數(shù)據(jù)庫備份化)
- Memcached to scala read traffic(逐層加cache提高性能)
- Spread write load(平分化)
- Locally buffered evevt tracking + async flush to high-throughput messagequeue
- Use a distributed unique IDgenerator(64-bit)
Performance
Cache is KEY!
- Numbers Everyone Should Know
- L1 cache reference 0.5ns
- Branch mispredict 5ns
- L2 cache reference 7ns
- Mutex lock/unlock 25ns
- Main memory reference 100ns
- Compress 1K bytes with Zippy 3,000ns
- Send 2K bytes over 1 Gbps network 20,000ns
- Read 1 MB sequentially from memory 250,000ns
- Round trip within same datacenter 500,000ns
- Disk seek 10,000,000ns
- Read 1 MB sequentially from disk 20,000,000ns
- Send packet CA->Netherlands->CA 150,000,000ns
Estimation
How many piano tuners are there in the entire world?
Tiny URL:How much is total storage?
- URL Length 10-1000 chars.
- Total accumulated URL number 100M
- New URL registrations are on the order 100,000/day(1/sec)
- Redirect requests are on the order of 100M/day(1000/sec)
Design Pattern
23patterns
- MVC(前后端分離坞笙,中間件連接)
- Singleton(保證只有一個實例)
- Factory(工業(yè)框架,生成一系列子類)
- Iterator(迭代器碎赢,提供方法順序訪問聚合對象并且不暴露內(nèi)部特征)
- Decorator(裝飾器低剔,為對象添加額外職責(zé))
- Facade