读书笔记 · 约 14,318 字 · 预计推送 2 部分

Raft 共识论文精读:领导人选举、日志复制与安全性(配合可视化资源理解) 未开始

目录

题记与说明

主题研究笔记 · 精读员 #0 · 2026-11-04 · 前缀 knowledge-927cp4si-0

0. 来源与阅读路径(共 5 个来源,均有网络实测抓取)

#来源类型本次实际动作
S1《In Search of an Understandable Consensus Algorithm (Extended Version)》, Diego Ongaro & John Ousterhout, Stanford, 2014-05-20(USENIX ATC'14 最佳论文的扩展版,18 页)<https://raft.github.io/raft.pdf>论文原典curl 下载 PDF(567KB,file 确认 PDF v1.4 / 18 页),pdftotext 全文抽取后逐段读完 §1–§12 与参考文献
S2Raft 官网 <https://raft.github.io/>资源枢纽WebFetch 抓取,确认论文/演讲/可视化/课程/实现清单及各链接
S3The Secret Lives of Data(Ben Johnson 的交互式 Raft 动画教程)<http://thesecretlivesofdata.com/raft/>可视化教程页面为 JS 渲染、正文抓不到;进一步直接抓取其前端帧脚本 scripts/frames/{intro,overview,election,replication,conclusion}.js,从源码提取了全部旁白台词(本次笔记中的 S3 引语均出自脚本原文)
S4RaftScope(官方可视化)<https://raft.github.io/raftscope/index.html>可视化沙盘WebFetch 抓到页面标题与全部键盘快捷键(页面正文说明文字未能抓到)
S5Jon Gjengset《Students' Guide to Raft》(MIT 6.824 助教实现指南)<https://thesquareplanet.com/blog/students-guide-to-raft/>第三方实践视角WebFetch 全文摘要,提取实现级坑点清单

阅读路径建议(本次实测有效的顺序):先用 S3 的 election/replication 两段动画建立直觉 → 再读 S1 的 Figure 2(算法压缩规约)与 §5 三个小节 → 最后用 S5 对照 Figure 2 做实现级校验,用 S4 沙盘亲手制造分裂投票。


1. 七维提取

1.1 核心问题

如何设计一个可理解性(understandability)优先、同时工程上完整可用的共识算法来管理复制日志(replicated log),替代"出了名难懂、且与真实实现严重脱节"的 Paxos?论文将共识拆成三个相对独立的子问题:领导人选举(leader 失效后如何选出新 leader)、日志复制(leader 如何把条目复制到全集群并提交)、安全性(任何服务器已应用到状态机的条目,绝不允许被其他服务器用不同条目覆盖)。

1.2 主要主张

  1. Raft 结果等价于 (multi-)Paxos、效率相当,但更容易理解,且是更好的系统构建基础——摘要原文:"*It produces a result equivalent to (multi-)Paxos, and it is as efficient as Paxos, but its structure is different from Paxos; this makes Raft more understandable than Paxos and also provides a better foundation for building practical systems.*"(S1 摘要,p.1)
  2. 可理解性来自两个可复用的技术:分解(把共识拆为 leader election / log replication / safety / membership changes 分别求解)与状态空间化简(强领导者、日志无空洞、限制日志不一致的方式、用随机化消灭对称性)。
  3. 三个新颖点:强领导者(日志条目只从 leader 流向 follower);随机化定时器选举(在心跳机制上加一点点机制就简单快速地解决选票分裂);联合共识(joint consensus)成员变更(新旧两配置的大多数重叠,变更期间集群可继续服务)。
  4. 一组五条安全性质(Figure 3)始终成立:Election Safety(每 term 至多一个 leader)、Leader Append-Only、Log Matching、Leader Completeness、State Machine Safety。

1.3 方法

1.4 数据与证据(是否充分:核心主张"更易懂"有量化证据支撑;但样本小、且可理解性度量天然主观,属"有证据但不铁")

1.5 结论

以可理解性为第一目标不只是"教学友好",它同时降低了实现出错的概率、简化了正确性论证、还顺带造出了更实用的架构(强领导者、日志单向流动、leader 无需特殊操作即可自动收敛 follower 日志)。论文结论原话(§11):"*Although these are all worthy goals, we believe that understandability is just as important.*"以及"*These techniques not only improved the understandability of Raft but also made it easier to convince ourselves of its correctness.*"

1.6 局限

1.7 对我的启发

  1. "分解 + 状态空间化简"是可迁移的通用设计方法论,不止于共识:遇到复杂系统先问"能否拆成可独立解释的子问题"与"能否删掉状态、消灭非确定性";随机化是合法武器——"*handling all possible choices in a similar fashion ('choose any; it doesn't matter')*"(§4)。
  2. 先可视化建立直觉,再读规约,再动手实现:S3 动画把 term、心跳、分裂投票、分区恢复变成可见过程,之后再读 Figure 2 的规则表会发现每条规则都对应动画里一个坑。
  3. 维护一张"性质清单"当不变式检查表(对应 Figure 3 五条性质):设计协议/状态机时先写下永远成立的性质,所有机制改动都拿这张表过一遍。
  4. "保守换简单"是划算的:Raft 明知某些旧 term 条目其实可安全提交,仍选择"只按当前 term 计数"——理由仅仅是让推理更简单。工程里类似的"多余但换来全局可推理"的决策值得保留。
  5. 教学/文档写作可借鉴:论文把完整算法压缩成一张 Figure 2 + 五条性质 + 三小节解释,这种"一张图可执行"的写法是技术写作的标杆。

2. 三个子问题逐项深读(论文原文 × 可视化对照)

2.1 领导人选举(S1 §5.1–5.2, p.4–6 × S3 election 帧 × S4 快捷键)

机制链条:

  1. 三状态:leader / follower / candidate;正常时恰有一个 leader,其余全是 follower;follower 只被动应答,客户端找上 follower 会被重定向到 leader(§5.1)。
  2. term 是逻辑时钟,单调递增,随每次通信交换;发现自己的 term 落后就立即更新,candidate/leader 发现 term 过时就退回 follower,收到旧 term 请求直接拒绝(§5.1)。
  3. 选举触发:follower 在 election timeout 内没收到 leader(或候选者)的有效 RPC → 自增 term、自投一票、并行发 RequestVote(§5.2)。
  4. 获胜条件(原文):"*A candidate wins an election if it receives votes from a majority of the servers in the full cluster for the same term.*" 每人每 term 至多投一票(先到先得),多数票 ⇒ 每 term 至多一个 leader(Election Safety)。
  5. 三种结局:赢得多数当 leader 并立刻发心跳确立权威;遇到 term ≥ 自己的 AppendEntries 就承认对方退回 follower;无人获胜则超时重选。
  6. 随机化是关键:"*election timeouts are chosen randomly from a fixed interval (e.g., 150–300ms)*"——把大家的超时错开,多数情况只有一台先超时、当选并发心跳压住其他人。分裂投票出现时也靠重启随机超时来化解。§9.3 数据:无随机化时分裂投票反复发生、选主 >10 秒;5ms 随机化即降中位到 287ms。
  7. 设计取舍的花絮(§5.2 末):作者最初用"排名制"选主,每修一个边界条件又冒出新坑,最后认定随机重试"*more obvious and understandable*"——这是"可理解性优先"的绝佳案例。

可视化对照:

2.2 日志复制(S1 §5.3, p.6–7 × S3 replication 帧)

机制链条:

  1. 客户端命令 → leader 追加为日志条目(含 term 号 + 指令 + 整数索引)→ 并行 AppendEntries 复制;follower 崩溃/慢/丢包时 leader 无限重试(哪怕已回复客户端)。
  2. 提交定义(原文):"*A log entry is committed once the leader that created the entry has replicated it on a majority of the servers*";提交同时隐式提交其之前所有条目;leader 把 commitIndex 随后续 AppendEntries(含心跳)广播,follower 得知后按序应用到状态机。
  3. Log Matching 性质由一致性检查归纳保证:AppendEntries 携带 prevLogIndex/prevLogTerm,follower 找不到匹配条目就拒绝——"*the initial empty state of the logs satisfies the Log Matching Property, and the consistency check preserves the Log Matching Property whenever logs are extended*"(数学归纳法结构)。
  4. 日志不一致(leader 崩溃遗留)时,leader 强制 follower 复制自己的日志:为每个 follower 维护 nextIndex,被拒就递减重试,直到找到两日志一致点,删除 follower 冲突条目、补上 leader 条目。原文强调 leader 无需任何"上任清理"动作:"*a leader does not need to take any special actions to restore log consistency when it comes to power. It just begins normal operation, and the logs automatically converge*",且 "*A leader never overwrites or deletes entries in its own log*"(Leader Append-Only)。
  5. 性质:只要多数服务器在线就能接受/复制/应用新条目;正常情况一轮 RPC 即可复制;单个慢 follower 不拖累整体(§5.3 末)。

可视化对照(S3 replication 帧脚本原文):提交流程 "*An entry is committed once a majority of followers acknowledge it... and a response is sent to the client.*";分区演示是这一帧的精华——5 节点切成 2+3 两半后 "*we now have two leaders in different terms*",少数派的写入 "*cannot replicate to a majority so its log entry stays uncommitted*",多数派写入成功;愈合后 "*Node B will see the higher election term and step down*"、"*Both nodes A & B will roll back their uncommitted entries and match the new leader's log.*"——这正是 §5.3 强制复制 + §5.4 term 规则的动态展示。

2.3 安全性(S1 §5.4, p.7–9)——本次精读的重点与难点

  1. 问题:仅靠 §5.2/5.3 不够——掉线的 follower 可能错过若干提交,随后被选为新 leader 并用自己的日志覆盖它们,导致不同状态机执行不同命令序列。
  2. 选举限制(§5.4.1):candidate 必须日志"至少与多数派中任何一台一样新"才能拿到选票(RequestVote 携带 lastLogIndex/lastLogTerm,投票者自己更新就不投)。up-to-date 判定(原文):"*If the logs have last entries with different terms, then the log with the later term is more up-to-date. If the logs end with the same term, then whichever log is longer is more up-to-date.*" 注意比的是 term 优先、长度其次,S5 特意警告实现者 "*No cheating and just checking the length!*"。
  3. Figure 8 陷阱(§5.4.2):旧 term 的条目即使已复制到多数,也可能被未来 leader 覆盖(论文用五时间线场景证明)。因此提交规则收紧为(原文):"*Raft never commits log entries from previous terms by counting replicas. Only log entries from the leader's current term are committed by counting replicas*"——当前 term 条目一旦按多数提交,Log Matching 性质顺带把之前所有条目"间接提交"。作者自陈这是"保守换简单"。
  4. Leader Completeness 证明骨架(§5.4.3,9 步反证):假设 term T 的 leader 提交了条目而某后续 term U 的 leader 没有 → 取最小的 U;T 的多数与 U 的选票多数必相交于"voter";voter 投票前已接受该条目且投票时仍保有它(中间 leader 都保有、leader 不删、follower 只在与 leader 冲突时删);voter 肯投票说明 leaderU 日志至少一样新 → 两种情况都推出 leaderU 其实含有该条目,矛盾。由此 + 按索引序应用 ⇒ State Machine Safety。
  5. 时序与可用性(§5.6):安全不依赖时间,但可用性依赖,须满足 "*broadcastTime ≪ electionTimeout ≪ MTBF*"(广播时间比选举超时小一个数量级、选举超时比平均故障间隔小几个数量级)。租约式读是反例——"*this would rely on timing for safety*",被作者明确排除。

可视化对照:S3 的分区帧覆盖了安全性的"现象层"(少数派未提交、愈合后回滚);Figure 8 的"多数存储≠提交"这一微妙点两个可视化都没演示(Secret Lives of Data 不含该场景),必须回到论文 Figure 8 与 §5.4.2——这是可视化资源与论文之间的缺口,学习时应特别补上。


3. 支撑机制速览(完整算法拼图)


4. 原文引述索引(便于回查)

引语位置
"Raft is a consensus algorithm for managing a replicated log. It produces a result equivalent to (multi-)Paxos..."S1 摘要 p.1
"It was important not just for the algorithm to work, but for it to be obvious why it works."S1 §1 p.1
"There are significant gaps between the description of the Paxos algorithm and the needs of a real-world system... the final system will be based on an unproven protocol."(Chubby 实现者评 Paxos)S1 §3 p.2
"randomized approaches... handling all possible choices in a similar fashion ('choose any; it doesn't matter')"S1 §4 p.3
Figure 3 五条安全性质(Election Safety / Leader Append-Only / Log Matching / Leader Completeness / State Machine Safety)S1 p.4
"A candidate wins an election if it receives votes from a majority of the servers in the full cluster for the same term."S1 §5.2 p.5
"election timeouts are chosen randomly from a fixed interval (e.g., 150–300ms)"S1 §5.2 p.5
"A log entry is committed once the leader that created the entry has replicated it on a majority of the servers"S1 §5.3 p.6
"A leader never overwrites or deletes entries in its own log (the Leader Append-Only Property in Figure 3)."S1 §5.3 p.7
"If the logs have last entries with different terms, then the log with the later term is more up-to-date..."S1 §5.4.1 p.8
"Raft never commits log entries from previous terms by counting replicas."S1 §5.4.2 p.8
"broadcastTime ≪ electionTimeout ≪ MTBF"S1 §5.6 p.9
"In the absence of randomness, leader election consistently took longer than 10 seconds... Adding just 5ms of randomness helps significantly, resulting in a median downtime of 287ms."S1 §9.3 p.15
"the mean Raft score was 25.7 and the mean Paxos score was 20.8"(43 人中 33 人 Raft 更高)S1 §9.1 p.14
"VR and ZooKeeper each define 10 different message types, while Raft has only 4 message types"S1 §10 p.16
"In Raft there are two timeout settings which control elections." 等全套旁白S3 帧脚本源码(election.js / replication.js / overview.js)
"It needs to be followed to the letter." / "MUST NOT truncate its log" / "No cheating and just checking the length!"S5

5. 引用来源清单

  1. Ongaro, D. & Ousterhout, J. *In Search of an Understandable Consensus Algorithm (Extended Version)*. Stanford, 2014. PDF: https://raft.github.io/raft.pdf (ATC'14 会议版: https://www.usenix.org/conference/atc14/technical-sessions/presentation/ongaro )
  2. Raft 官网(论文/演讲/可视化/课程/实现枢纽): https://raft.github.io/
  3. Ben Johnson, *The Secret Lives of Data — Raft*: http://thesecretlivesofdata.com/raft/ (旁白台词取自其前端帧脚本 scripts/frames/election.js、replication.js、overview.js、intro.js)
  4. RaftScope: https://raft.github.io/raftscope/index.html (项目仓库 https://github.com/ongardie/raftscope )
  5. Jon Gjengset, *Students' Guide to Raft*: https://thesquareplanet.com/blog/students-guide-to-raft/
  6. 延伸(本次未精读,仅列出处):Ongaro 博士论文 *Consensus: Bridging Theory and Practice* https://github.com/ongardie/dissertation ;TLA+ 规约 https://github.com/ongardie/raft.tla
抓取局限说明:S3/S4 均为 JS 渲染页面,正文 HTML 抓不到旁白与说明文字;S3 已通过直接抓取帧脚本源码获得台词原文(引语可靠),但动画的视觉编排细节(节点布局、配色)系按脚本逻辑推断;S4 仅抓到标题与键盘快捷键,页面正文说明未能获取。论文部分(S1)为全文精读,无此限制。
← 资料库今日播报 →