《分布式一致性協(xié)議raft簡(jiǎn)介》

一. 由來

如果你搭建過大型的分布式系統(tǒng)贡蓖,那么一般你會(huì)用到zookeeper這個(gè)服務(wù)。該服務(wù)實(shí)現(xiàn)了ZAB算法煌茬。其通常會(huì)用在fail over選主斥铺,微服務(wù)上下游server配置中心等場(chǎng)景。但是ZAB和paxos有個(gè)缺點(diǎn)坛善,就是理解性比較差晾蜘。其論文內(nèi)容十分復(fù)雜邻眷,導(dǎo)致真正理解的開發(fā)人員非常少。【知乎:raft算法與paxos算法相比有什么優(yōu)勢(shì)剔交,使用場(chǎng)景有什么差異】

二. raft 原理描述

【raft homepage】
【raft paper】
【live demo】

1. 選主Leader Election

raft和zookeeper類似肆饶,一般需要3或者5個(gè)node。這樣有利于判斷選舉的多數(shù)情況
node分為3個(gè)狀態(tài):follower岖常,candidate驯镊,leader
狀態(tài)的轉(zhuǎn)換
raft有2個(gè)timeout設(shè)置
1)從follow而轉(zhuǎn)換到candidate的timeout: election timeout,設(shè)置為:150ms到300ms中的隨機(jī)數(shù)腥椒。一個(gè)node到達(dá)這個(gè)timeout之后會(huì)發(fā)起一個(gè)新的選舉term(遞增的阿宅,大的表示新的)候衍,向其他節(jié)點(diǎn)發(fā)起投票請(qǐng)求笼蛛,包括投給自己的那票,如果獲得了大多數(shù)選票蛉鹿,那么自己就轉(zhuǎn)換為leader狀態(tài)
2)node成為leader之后會(huì)向其他node發(fā)送Append Entries滨砍,這個(gè)時(shí)間為heartbeat timeout
如果lead在實(shí)際使用中down掉,剩下的節(jié)點(diǎn)會(huì)重新開啟1)和2)描述的選舉流程妖异,保證了高可用性
特殊情況
如果集群中剩下偶數(shù)個(gè)node惋戏,并且在選舉的過程中有2個(gè)node獲得相等的選票數(shù),那么會(huì)開啟新的一輪term選舉他膳。知道有一個(gè)node獲得多數(shù)選票(隨機(jī)的election timeout保證可行)

2. 分布式系統(tǒng)中數(shù)據(jù)的一致性和高可用保證log replication

client給leader發(fā)送數(shù)據(jù)修改請(qǐng)求
leader通過Append Entries在心跳的過程中將修改內(nèi)容下發(fā)到follower nodes
在多數(shù)follower 接收了修改內(nèi)容返回后响逢,leader向client確認(rèn)
leader向follower發(fā)送心跳,具體執(zhí)行修改操作棕孙,此后數(shù)據(jù)在集群中保持一致
特殊情況
節(jié)點(diǎn)之前的網(wǎng)絡(luò)狀況十分不好舔亭,此時(shí)會(huì)有多個(gè)leader,其term也是不同的蟀俊。
由于commit的修改需要多數(shù)通過钦铺,那么只有具有最多node的一個(gè)集群會(huì)commit修改成功。
當(dāng)網(wǎng)絡(luò)狀況恢復(fù)肢预,整個(gè)集群的節(jié)點(diǎn)會(huì)向多數(shù)節(jié)點(diǎn)的集群同步矛洞。這樣整個(gè)集群中的數(shù)據(jù)會(huì)繼續(xù)保持一致

3. raft集群擴(kuò)容Membership Changes

live demo中沒有提及,但是paper中說明的內(nèi)容烫映。
在實(shí)際使用中可有可能會(huì)遇到現(xiàn)有機(jī)器被新機(jī)器替換沼本,或者為了提升穩(wěn)定性擴(kuò)容raft集群的情況。作者給出了joint consensus的解決方案锭沟。其能保證切換過程是無縫的抽兆。

三. 在工業(yè)界系統(tǒng)的應(yīng)用

  1. 【MySQL 三節(jié)點(diǎn)企業(yè)版】
    mysql 三節(jié)點(diǎn)企業(yè)版

    利用分布式一致性協(xié)議(Raft)保障多節(jié)點(diǎn)狀態(tài)切換的可靠性和原子性。
  2. 【RethinkDB: pushes JSON to your apps in realtime】

How is cluster configuration propagated?
Updating the state of a cluster is a surprisingly difficult problem in distributed systems. At any given point different (and potentially) conflicting configurations can be selected on different sides of a netsplit, different configurations can reach different nodes in the cluster at unpredictable times, etc.
RethinkDB uses the Raft algorithm to store and propagate cluster configuration in most cases, although there are some situations it uses semilattices, versioned with internal timestamps. This architecture turns out to have sufficient mathematical properties to address all the issues mentioned above (this result has been known in distributed systems research for quite a while)

  1. 【etcd:A distributed, reliable key-value store for the most critical data of a distributed system】

What is failure tolerance?
An etcd cluster operates so long as a member quorum can be established. If quorum is lost through transient network failures (e.g., partitions), etcd automatically and safely resumes once the network recovers and restores quorum; Raft enforces cluster consistency. For power loss, etcd persists the Raft log to disk; etcd replays the log to the point of failure and resumes cluster participation. For permanent hardware failure, the node may be removed from the cluster through runtime reconfiguration.
It is recommended to have an odd number of members in a cluster. An odd-size cluster tolerates the same number of failures as an even-size cluster but with fewer nodes. The difference can be seen by comparing even and odd sized clusters:

etcd節(jié)點(diǎn)數(shù)與容錯(cuò)能力數(shù)據(jù):奇數(shù)個(gè)node有優(yōu)勢(shì)

Adding a member to bring the size of cluster up to an even number doesn't buy additional fault tolerance. Likewise, during a network partition, an odd number of members guarantees that there will always be a majority partition that can continue to operate and be the source of truth when the partition ends.

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末冈钦,一起剝皮案震驚了整個(gè)濱河市郊丛,隨后出現(xiàn)的幾起案子李请,更是在濱河造成了極大的恐慌,老刑警劉巖厉熟,帶你破解...
    沈念sama閱讀 217,185評(píng)論 6 503
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件导盅,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡揍瑟,警方通過查閱死者的電腦和手機(jī)白翻,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 92,652評(píng)論 3 393
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來绢片,“玉大人滤馍,你說我怎么就攤上這事〉籽” “怎么了巢株?”我有些...
    開封第一講書人閱讀 163,524評(píng)論 0 353
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)熙涤。 經(jīng)常有香客問我阁苞,道長(zhǎng),這世上最難降的妖魔是什么祠挫? 我笑而不...
    開封第一講書人閱讀 58,339評(píng)論 1 293
  • 正文 為了忘掉前任那槽,我火速辦了婚禮,結(jié)果婚禮上等舔,老公的妹妹穿的比我還像新娘骚灸。我一直安慰自己,他們只是感情好慌植,可當(dāng)我...
    茶點(diǎn)故事閱讀 67,387評(píng)論 6 391
  • 文/花漫 我一把揭開白布甚牲。 她就那樣靜靜地躺著,像睡著了一般涤浇。 火紅的嫁衣襯著肌膚如雪鳖藕。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 51,287評(píng)論 1 301
  • 那天只锭,我揣著相機(jī)與錄音著恩,去河邊找鬼。 笑死蜻展,一個(gè)胖子當(dāng)著我的面吹牛喉誊,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播纵顾,決...
    沈念sama閱讀 40,130評(píng)論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼伍茄,長(zhǎng)吁一口氣:“原來是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來了施逾?” 一聲冷哼從身側(cè)響起敷矫,我...
    開封第一講書人閱讀 38,985評(píng)論 0 275
  • 序言:老撾萬榮一對(duì)情侶失蹤例获,失蹤者是張志新(化名)和其女友劉穎,沒想到半個(gè)月后曹仗,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體榨汤,經(jīng)...
    沈念sama閱讀 45,420評(píng)論 1 313
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 37,617評(píng)論 3 334
  • 正文 我和宋清朗相戀三年怎茫,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了收壕。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片。...
    茶點(diǎn)故事閱讀 39,779評(píng)論 1 348
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡轨蛤,死狀恐怖蜜宪,靈堂內(nèi)的尸體忽然破棺而出,到底是詐尸還是另有隱情祥山,我是刑警寧澤圃验,帶...
    沈念sama閱讀 35,477評(píng)論 5 345
  • 正文 年R本政府宣布,位于F島的核電站枪蘑,受9級(jí)特大地震影響损谦,放射性物質(zhì)發(fā)生泄漏岖免。R本人自食惡果不足惜岳颇,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 41,088評(píng)論 3 328
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望颅湘。 院中可真熱鬧话侧,春花似錦、人聲如沸闯参。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,716評(píng)論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽鹿寨。三九已至新博,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間脚草,已是汗流浹背赫悄。 一陣腳步聲響...
    開封第一講書人閱讀 32,857評(píng)論 1 269
  • 我被黑心中介騙來泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留馏慨,地道東北人埂淮。 一個(gè)月前我還...
    沈念sama閱讀 47,876評(píng)論 2 370
  • 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像写隶,于是被迫代替她去往敵國(guó)和親倔撞。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 44,700評(píng)論 2 354

推薦閱讀更多精彩內(nèi)容