Raft要比Paxos更加简单易懂
这是一个Raft的flash演示
下面这篇文章讲解了Raft的leader选举的基本思想
raft论文中文版:
先总结一个大致的内容:
Leader Election
- Rule
- Follower
- Candidate
- Leader
- Timer
- Election Timeout - random time waiting until becomming a candidate 150 - 300ms
- One has reached the election timeout,it becomes a candidate and will ask for vote from other nodes and himself
- Other nodes receive the vote request, and reset the Election Timeout, and send to the Candidate note
- Heartbeat Timeout
- send Append Entries to other followers,
- followers wil reset the Election Timeout
- if the followers does not receive the Append Entriy,and the follow may become a candidate
Log Replication
- leader recevie
- leader send to followers
- follower ack to leader
- lead confirm and set data
- lead ack to followers
- follower confirm and set data
不会出现脑裂的问题,因为如果出现部分网络不通,会选择新的节点作为leader,这样有两个leader,但是由于log replication这个机制,使得没有超过半数的ack,那么数据是不能更新的,所以会导致操作失败,只有在一个集群里的操作是成功的,一旦网络恢复,那么假的leader以及它下面的节点的会回滚自己未提交的操作,然后同步真的leader