Raft算法是强一致性的一种算法,算法分大体分为 Leader选举 和 log Replication
Raft算法中节点有3中状态:
Follower
Candidate
Leader
Raft中的超时时间是在150ms ~ 300ms的随机数
Leader选举(3个节点为例,):
1. 开始所有的node都是Follwer状态
2. 超时时间开始在所有的node(3个节点只有一个需要状态为Candidate),哪个node最先超时,哪个节点状态由Follower转换成Candidate
3. Candidate节点向其他节点发送选举投票选择,多个Candidate最后比谁的票数最多,谁就是Leader,如果两个Candidate 票数一样,那就看哪个节点谁先超时. 谁就是Leader.
4. 成为Leader之后就会发送心跳包给所有Follower节点
log Replicatoin(一致性):
1. client 连接进入Leader操作(Set 5)
2. Leader 将操作记录在leader.log中(WAL)
3. Leader将log发送给所有Follower,(多数)Follower返回Leader
4. Leader 返回给client并且更新自己操作,并且发送notifies 给所有follwer the entry is commited,
5. 各个follower更新操作
详细过程清参考:http://thesecretlivesofdata.com/raft/
etcd中的一致性算法就是使用raft,如果要使用raft,可以使用etcd中的raft(golang),因为goraft作者已经不维护了。