paxos、multi paxos、raft、zab 答疑
1. paxos 算法思想
首先需要说明,常见讲 paxos 算法的都是尝试解释 paxos 算法思想,而不是讲述 paxos 算法实现(算法实现非常复杂,可以尝试下 nacos 源码),
通过并不完善的例子尝试解释 paxos 算法思想效果并不理想,本文通过数学符号,精确表达算法思路,而不是算法实现
2. multi paxos
multi paxos 是对 paxos 算法改进,其思路是引入 leader 角色,系统只有一个 leader,由 leader 发起提案号,然后进行投票,决议。至于 leader 产生的方法可以随机产生,亦可以根据业务
系统特性产生,比如业务数据最新的机器作为 leader。
3. raft
raft 算法本质是 multi paxos 改进。算法的目标是:解决 “复制状态机集群” 状态最终一致性问题。它是 multi paxos 算法思想在这个问题的推广,针对这个问题, raft 算法增加
了一些约束条件(问题背景),并用自己的语言加以阐述算法流程。首先我们要弄清楚,“复制状态机集群” 状态最终一致性这是个啥问题
3.1 问题
上述服务包含共识模块、日志模块、状态机模块。共识模块接受指令,存入日志,状态机模块按照日志顺序执行指令,得到一系列状态。对于上述服务构成的集群,全部实例怎样才能实现状态
最终一致?这里状态包括顺序
3.2 算法
为解决这一问题,算法将问题拆解为三个子问题,予以解决
- Leader election: a new leader must be chosen when an existing leader fails
- Log replication: the leader must accept log entries from clients and replicate them across the cluster, forcing the other logs to agree with its own
- 安全问题,如下:
Election Safety: at most one leader can be elected in a given term. Leader Append-Only: a leader never overwrites or deletes entries in its log; it only appends new entries. Log Matching: if two logs contain an entry with the same index and term, then the logs are identical in all entries up through the given index. Leader Completeness: if a log entry is committed in a given term, then that entry will be present in the logs of the leaders for all higher-numbered terms. State Machine Safety: if a server has applied a log entry at a given index to its state machine, no other server will ever apply a different log entry for the same index.
具体算法直接看原论文 [1] Ongaro D , Ousterhout J K . In search of an understandable consensus algorithm[J]. draft of october, 2014.
4. zab 协议
zab 是 multi paxos 算法的一种实现,它跟 raft 差别为:
1. 选举阶段,zab 是相互举荐,而 raft 是自荐
2. 日志顺序,zab 通过 tcp 确认机制,而 raft 通过日志连续性策略
3. zab 读不具有一致性,而 raft 读可以强一致,也可以最终一致