本文是Distributed systems for fun and profit的第一部分份名,本文是閱讀該文后的一些記錄。
Introduction
看了好多分布式系統(tǒng)其馏,如Amazon's Dynamo, Google's BigTable and MapReduce, Apache's Hadoop等,那這些系統(tǒng)背后有沒(méi)有什么共性的東西呢?本文試圖去帶領(lǐng)大家闡述這些系統(tǒng)背后的思想贷痪。
在作者看來(lái),distributed programming主要是在處理分布式帶來(lái)的兩個(gè)主要的影響:
- information travels at the speed of light
- independent things fail independently
具體怎么理解呢蹦误?
信息的傳輸速度是光速劫拢,雖然已經(jīng)很快了,但是還是需要時(shí)間的强胰,另一個(gè)系統(tǒng)要處理的錯(cuò)誤不僅一類舱沧,而是會(huì)很多,而且這些錯(cuò)誤之間沒(méi)有聯(lián)系偶洋,彼此獨(dú)立熟吏。
作者希望通過(guò)本文,讓讀者能認(rèn)識(shí)對(duì)distance, time and consistency models
怎么交互有個(gè)更好的理解涡真。
Distributed systems at a high level
Distributed programming is the art of solving the same problem that you can solve on a single computer using multiple computers.
分布式系統(tǒng)遇到的問(wèn)題和單機(jī)系統(tǒng)是一樣的分俯,只是解決同一個(gè)問(wèn)題的時(shí)候肾筐,其系統(tǒng)的constraints不同了。
計(jì)算機(jī)系統(tǒng)主要解決兩類問(wèn)題
- 計(jì)算
- 存儲(chǔ)
市面上各種框架其可以劃分為兩個(gè)領(lǐng)域的戰(zhàn)爭(zhēng)缸剪,一個(gè)是偏向底層存儲(chǔ)的戰(zhàn)爭(zhēng)吗铐,一個(gè)是偏向計(jì)算的戰(zhàn)爭(zhēng)
偏向存儲(chǔ)的戰(zhàn)爭(zhēng)有關(guān)系型數(shù)據(jù)庫(kù)和非關(guān)系型數(shù)據(jù)庫(kù)(Relational vs NoSQL)的戰(zhàn)爭(zhēng),它們兩者都有各自的應(yīng)用特點(diǎn)杏节。
- 關(guān)系型數(shù)據(jù)庫(kù)最大的特點(diǎn)是事務(wù)的一致性唬渗,讀寫(xiě)操作都是事務(wù)的,具有ACID的特點(diǎn)奋渔,它在銀行這樣對(duì)一致性有要求的系統(tǒng)中應(yīng)用廣泛镊逝。
- 而非關(guān)系型數(shù)據(jù)庫(kù)一般對(duì)一致性要求不高,但支持高性能并發(fā)讀寫(xiě)嫉鲸,海量數(shù)據(jù)訪問(wèn)撑蒜,在微博、Facebook這類SNS應(yīng)用中廣泛使用玄渗。
另外座菠,非關(guān)系型數(shù)據(jù)庫(kù)內(nèi)部也有戰(zhàn)爭(zhēng),比如說(shuō)HBase和Cassandra藤树,前者注重一致性(Consistency)和可用性(Availability)浴滴,后者提供可用性(Availability)和分區(qū)容錯(cuò)性(Partition tolerance)
Redis和Memcached,它們都是內(nèi)存內(nèi)的Key/Value存儲(chǔ)岁钓,但Redis還支持哈希表升略,有序集和鏈表等多種數(shù)據(jù)結(jié)構(gòu)。
MongoDB屡限,CouchDB和Couchbase這三個(gè)文檔型數(shù)據(jù)庫(kù)品嚣,MongoDB更適用于需要?jiǎng)討B(tài)查詢的場(chǎng)景,CouchDB偏向于預(yù)定義查詢钧大,Couchbase比CouchDB有更強(qiáng)的一致性腰根,而且還可以作為Key/Value存儲(chǔ)。
搜索引擎Solr和Elasticsearch的拓型,它們都是基于Lucene,性能上相近瘸恼,但是前者在Java/C#開(kāi)發(fā)者中大受歡迎劣挫,而后者深受Python/PHP開(kāi)發(fā)者喜愛(ài)。
偏向計(jì)算的戰(zhàn)爭(zhēng)有MapReduce和Spark之間的戰(zhàn)爭(zhēng)东帅,此外還有Spark Streaming和Storm之間的戰(zhàn)爭(zhēng)等等压固。
以上關(guān)于計(jì)算和存儲(chǔ)的討論來(lái)自深入淺出Spark(1)什么是Spark
回到本文的內(nèi)容上:我們?yōu)槭裁葱枰植际?/strong>?理論上如果單機(jī)系統(tǒng)有無(wú)限的內(nèi)存靠闭,極致的計(jì)算速度帐我,那我們是沒(méi)必要將系統(tǒng)設(shè)計(jì)為分布式的坎炼,因?yàn)榉植际揭肓艘恍┝袉?wèn)題,但是呢拦键,我們沒(méi)有money啊谣光,我們只能妥協(xié)了,用平價(jià)機(jī)器來(lái)實(shí)現(xiàn)高性能芬为。
下面回答第二個(gè)問(wèn)題:我們通過(guò)分布式希望得到什么萄金?
在作者看來(lái)所有的問(wèn)題起源都是:size - scalability
everything starts with the need to deal with size
everything starts with size - scalability
那什么是scalability呢?
Scalability is the ability of a system, network, or process, to handle a growing amount of work in a capable manner or its ability to be enlarged to accommodate that growth.
scalability:伸縮性媚朦,系統(tǒng)伸縮性好意味著當(dāng)系統(tǒng)處理growing amount of work的時(shí)候氧敢,即使處理難度不是線性增長(zhǎng)的,也應(yīng)該是可預(yù)測(cè)的询张,而不是指數(shù)級(jí)增長(zhǎng)的孙乖。
那什么是growing amount of work呢?作者又舉了3個(gè)具體的例子:
- Size scalability: adding more nodes should make the system linearly faster; growing the dataset should not increase latency
- Geographic scalability: it should be possible to use multiple data centers to reduce the time it takes to respond to user queries, while dealing with cross-data center latency in some sensible manner.
- Administrative scalability: adding more nodes should not increase the administrative costs of the system (e.g. the administrators-to-machines ratio).
上面的討論我們可以看到每個(gè)例子都試著從存儲(chǔ)和計(jì)算兩方面進(jìn)行解釋份氧,然后每個(gè)處理都是在time唯袄,distance和consistency的抉擇。
隨著系統(tǒng)size的增加半火,我們的設(shè)計(jì)需要不斷滿足實(shí)際的需求越妈,而這些需求主要來(lái)自于
- performance
- availability
Performance (and latency)
而這兩方面在實(shí)際系統(tǒng)中的測(cè)量方式也有很多,先來(lái)看performance
Performance is characterized by the amount of useful work accomplished by a computer system compared to the time and resources used.
性能主要是通過(guò)計(jì)算機(jī)系統(tǒng)有效工作占計(jì)算時(shí)間和資源的比例來(lái)衡量的钮糖,更具體來(lái)說(shuō):
- Short response time/low latency for a given piece of work
- High throughput (rate of processing work)
- Low utilization of computing resource(s)
低延遲梅掠,高吞吐,低利用率
先看延遲
Latency:The state of being latent; delay, a period between the initiation of something and the occurrence.
Latent:From Latin latens, latentis, present participle of lateo ("lie hidden"). Existing or present but concealed or inactive.
延遲:一件事情發(fā)生到真正產(chǎn)生影響
舉個(gè)例子:假設(shè)一個(gè)數(shù)據(jù)中心店归,有一個(gè)功能是通過(guò)計(jì)算所有數(shù)據(jù)阎抒,然后返回一個(gè)結(jié)果
result = query(all data in the system)
對(duì)于上面這個(gè)請(qǐng)求,影響延遲的因素是什么呢消痛?
- the amount of old data
- the speed at which new data "takes e?ect" in the system
是1還是2呢且叁?
我們可以知道如果系統(tǒng)處理query的速度和新數(shù)據(jù)在系統(tǒng)中生效的速度一樣,那么我們會(huì)發(fā)現(xiàn)query將永遠(yuǎn)不會(huì)返回秩伞,因此真正決定延遲的是2.
另外一點(diǎn)我們需要注意的是:延遲是因?yàn)橛行聰?shù)據(jù)的產(chǎn)生逞带,那如果系統(tǒng)沒(méi)有產(chǎn)生數(shù)據(jù),那就沒(méi)有延遲一說(shuō)纱新。
最后一點(diǎn)我們無(wú)法忽略的延遲是:
- the speed of light limits how fast information can travel, and
- hardware components have a minimum latency cost incurred per operation (think RAM and hard drives but also CPUs).
信息傳輸產(chǎn)生的延遲以及硬件本身產(chǎn)生的延遲展氓。
因此決定延遲的因素有:
- 操作本身的耗時(shí)
- 信息傳輸延遲
看完性能后,我們看下一個(gè)分布式系統(tǒng)的主需求:可用性
Availability (and fault tolerance)
Availability the proportion of time a system is in a functioning condition. If a user cannot access the system, it is said to be unavailable.
單機(jī)系統(tǒng)相比較于分布式系統(tǒng)永遠(yuǎn)無(wú)法達(dá)到的一點(diǎn)是:高可用或者說(shuō)容錯(cuò)
分布式系統(tǒng)可以通過(guò)一群不可靠的組件脸爱,最后達(dá)到一個(gè)高可用的系統(tǒng)遇汞,這就是分布式的魅力。
那我們來(lái)看下什么是容錯(cuò)(fault tolerance)
Fault tolerance:ability of a system to behave in a well-de?ned manner once faults occur
從定義我們可以看到,首先我們要規(guī)定問(wèn)題邊界:要處理的錯(cuò)誤空入,基于定義好的錯(cuò)誤我們?nèi)ラ_(kāi)發(fā)算法去解決它络它。如果系統(tǒng)發(fā)生了我們從未考慮的錯(cuò)誤,那何來(lái)談容錯(cuò)一說(shuō)歪赢。
What prevents us from achieving good things?
分布式系統(tǒng)受限于兩個(gè)物理約束:
- the number of nodes (which increases with the required storage and computation capacity)
- the distance between nodes (information travels, at best, at the speed of light)
簡(jiǎn)單來(lái)說(shuō)就是數(shù)量和距離化戳,上面的物理限制在增加節(jié)點(diǎn)的時(shí)候會(huì)帶來(lái):
- an increase in the number of independent nodes increases the probability of failure in a system (reducing availability and increasing administrative costs)【降低可用性和管理成本】
- an increase in the number of independent nodes may increase the need for communication between nodes (reducing performance as scale increases)【通信成本增加降低系統(tǒng)性能】
- an increase in geographic distance increases the minimum latency for communication between distant nodes (reducing performance for certain operations)【多數(shù)據(jù)中心增加通信成本】
上面說(shuō)了這么多限制,那怎么定義系統(tǒng)好不好呢轨淌?總的來(lái)說(shuō)是從 performance 和 availability 來(lái)看迂烁,具體衡量則是我們經(jīng)常說(shuō)的SLA(service level agreement)。
除了 performance 和 availability 外递鹉,還有一個(gè)很重要的點(diǎn)是: intelligibility盟步,即系統(tǒng)好不好理解,如果系統(tǒng)設(shè)計(jì)的非常好躏结,但是只有設(shè)計(jì)的人懂却盘,那這種系統(tǒng)價(jià)值也是不大的。
Abstractions and models
抽象幫我們做減法媳拴,只剩下本質(zhì)的東西黄橘,而模型則將抽象具現(xiàn)化,讓我們能更精確的定義抽象的東西屈溉。
我們來(lái)舉一些具體的模型的例子塞关,這些也是之后章節(jié)會(huì)講的東西:
- System model (asynchronous / synchronous)
- Failure model (crash-fail, partitions, Byzantine)
- Consistency model (strong, eventual)
我們?cè)O(shè)計(jì)分布式系統(tǒng)的目標(biāo)是:work like a single system,但是現(xiàn)實(shí)中我們有很多的節(jié)點(diǎn)需要處理子巾,我們很難讓使用者在無(wú)感知的情況下使用分布式系統(tǒng)帆赢,模型越是簡(jiǎn)單,意味著提供給用戶的功能越是抽象线梗,如果用戶想要獲得更好的性能椰于,更高級(jí)的功能,只能穿過(guò)給用戶提供的抽象仪搔,去看系統(tǒng)的實(shí)現(xiàn)細(xì)節(jié)瘾婿。
Design techniques: partition and replicate
為了存儲(chǔ)更多的數(shù)據(jù),我們將數(shù)據(jù)保存到不同的節(jié)點(diǎn)上烤咧,為了讓運(yùn)算更快偏陪,我們將同一份數(shù)據(jù)copy多份,讓多個(gè)實(shí)例進(jìn)行計(jì)算煮嫌,一個(gè)很好的總結(jié)是:
Divide and conquer - I mean, partition and replicate.
Partitioning
- Partitioning improves performance by limiting the amount of data to be examined and by locating related data in the same partition【數(shù)據(jù)分片后減少數(shù)據(jù)大小從而提高性能】
- Partitioning improves availability by allowing partitions to fail independently, increasing the number of nodes that need to fail before availability is sacri?ced【每個(gè)數(shù)據(jù)片失敗都是獨(dú)立的竹挡,提供了可用性】
Replication
To replication! The cause of, and solution to all of life's problems.
復(fù)制是應(yīng)對(duì)延遲的有效手段
- Replication improves performance by making additional computing power and bandwidth applicable to a new copy of the data
- Replication improves availability by creating additional copies of the data, increasing the number of nodes that need to fail before availability is sacri?ced
復(fù)制來(lái)到好處的同時(shí),也帶來(lái)了很多問(wèn)題立膛,最大的就是數(shù)據(jù)一致性問(wèn)題,只有當(dāng)模型是 strong consistency 的時(shí)候,我們才會(huì)得到一個(gè)簡(jiǎn)單的編程模型(和單機(jī)系統(tǒng)一致)宝泵,其他模型我們都好去理解系統(tǒng)內(nèi)部是怎么做的好啰,這樣子才能很好的滿足我們的需求。
以上就是第一部分概述的所有儿奶,下面第二部分將會(huì)更具體的介紹分布式系統(tǒng)的相關(guān)概念框往。