MIT 6.5840:Lecture 06 RSM and Raft (1)
本篇逐行译注 MIT 6.5840 Spring 2026 Lecture 6,覆盖 replicated state machine、Raft leader election、term、majority、随机 election timeout 和日志分叉。
MIT 6.5840:Lecture 06 RSM and Raft (1)
TL;DR
- RSM 的基本条件:相同初始状态、相同 commands、相同顺序、确定性执行,会得到相同终态。
- Raft 的 leader election:每个 term 最多一个 leader;candidate 必须获得全体 servers 的 majority votes。
- majority 的作用:防止 split brain,并允许少数节点故障时继续选举和提交。
- 随机 timeout:避免所有 peers 同时成为 candidate,降低 split vote 概率。
- 日志不是永远完全相同:leader 崩溃和网络分区可能造成尾部 divergence;后续 Lecture 7 处理日志收敛和额外选举约束。
名词解释
| 名词 | 含义 | 本讲对应物 |
|---|---|---|
| RSM | Replicated State Machine | 多个副本按相同顺序执行相同 commands |
| Term | Raft 中递增的逻辑时期 | 每个 term 最多一个 leader |
| Candidate | 发起 leader election 的 peer | 增加 term 并请求 votes |
| Majority | 全体 servers 的多数派 | 3 台中的 2 台;不是 live servers 的多数派 |
| Election timeout | peer 等待 leader heartbeat 的时间 | 超时后成为 candidate |
| Split vote | 多个 candidates 分散 votes,均未达 majority | 通过随机 timeout 缓解 |
| Commit | 日志 entry 已被 majority 保存 | 可交给 service/state machine 执行 |
目录
| 章节 | 内容 |
|---|---|
| 版本与来源 | 固定 Lecture 6 源文件 |
| 原文逐行翻译 | 按 l-raft.txt 顺序译注 |
| 机制小结 | RSM、选举和日志分叉的关系 |
| 本篇验收 | 源行、diagram 和边界记录 |
版本与来源
- 课程版本:MIT 6.5840 Spring 2026。
- Lecture 源文件:l-raft.txt,219 行,SHA-256:
8bc18871653e77cfc84e76a470946890acffea8040d84dc5d1e163ae1a54bef8。 - 配套论文:In Search of an Understandable Consensus Algorithm(Extended Version)。
- 课程日程:MIT 6.5840 Spring 2026 Schedule。
- 翻译边界:英文源行按原顺序保留,随后给出中文译文;RPC 名称、函数名、term、entry、command 和 diagram 占位原样保留。
- 图片结论:本 Lecture 源文件没有独立图片;
[diagram: ...]和日志时序示意按原位置保留。
原文逐行翻译
课程范围
6.5840 2026 Lecture 6: RSM and Raft (1)
6.5840 2026 年 Lecture 6:RSM 和 Raft(1)。
this lecture
本讲:
today: replicated state machine, and Raft elections (Lab 3A)
今天:replicated state machine,以及 Raft elections(Lab 3A)。
next: Raft persistence, client behavior, snapshots (Lab more 3B, 3C, 3D)
下一讲:Raft persistence、client behavior 和 snapshots(Lab 3B、3C、3D 的更多内容)。
High availability 与 replication
Goal: high availability
目标:high availability。
even if a machine fails, deliver service
即使一台机器 failure,也要继续提供 service。
i.e., no down time even if a machine fails
也就是说,即使一台机器 failure,也不能出现 down time。
approach: replication
方法:replication。
What kinds of failures can replication deal with?
replication 能处理哪些类型的 failures?
Replication is good for "fail-stop" failure of a single replica
Replication 很适合处理单个 replica 的“fail-stop” failure。
fan stops working, CPU overheats and shuts itself down
例如风扇停止工作,CPU 过热后自行关闭。
someone trips over replica's power cord or network cable
有人绊到了 replica 的电源线或网线。
software notices it is out of disk space and stops
软件发现磁盘空间耗尽,于是停止运行。
Replication may not help with bugs or operator error
Replication 可能无法解决 bugs 或 operator error。
Often not fail-stop
它们通常不是 fail-stop。
May be correlated (i.e. some input causes all replicas to crash)
它们可能是 correlated 的(例如某个 input 导致所有 replicas 都 crash)。
How about earthquake or city-wide power failure?
地震或全城停电呢?
Only if replicas are physically separated
只有 replicas 在物理上彼此分离时才有帮助。
How many replicas?
需要多少 replicas?
Often you want few because many is costly
通常希望 replicas 较少,因为数量多会增加成本。
But enough to survive failures during repair
但又要足够多,能够在 repair 期间继续承受 failures。
Many systems run with 3-5 replicas, as we will see
正如后面会看到的,许多系统使用 3–5 个 replicas。
This paper runs with only 2 replicas
这篇论文只使用 2 个 replicas。
resilient to 1 failure at the time
能够抵抗同时发生的 1 个 failure。
Replicated state machine
*** topic: replicated state machine
*** 主题:replicated state machine。
A popular approach to replication
一种常见的 replication 方法:
Clients send operations to primary,
Clients 把 operations 发送给 primary,
primary sequences and sends to backups
primary 为 operations 排序,并发送给 backups。
All replicas execute all operations
所有 replicas 都执行所有 operations。
If same start state,
如果初始 state 相同,
same operations,
operations 相同,
same order,
顺序相同,
deterministic,
执行是 deterministic 的,
then same end state.
那么最终 state 也相同。
Example: primary backup in GFS
例子:GFS 中的 primary-backup。
What if primary fails?
如果 primary failure 了呢?
- Coordinator picks new primary in GFS
- GFS 中由 coordinator 选择新的 primary。
- What if coordinator fails?
- 如果 coordinator failure 了呢?
选举新的 coordinator
Can we have the replicas elect a new primary
能否让 replicas 自己选出新的 primary?
how about two servers, S1 and S2
假设有两台 servers,S1 和 S2。
if both are up, S1 is in charge, forwards decisions to S2
如果两台都 up,S1 负责,并把 decisions 转发给 S2。
if S2 sees S1 is down, S2 takes over as coordinator
如果 S2 看到 S1 down 了,S2 就接管 coordinator 的职责。
what could go wrong?
可能出什么问题?
network partition! split brain!
network partition!split brain!
the problem: computers cannot distinguish "server crashed" from "network broken"
问题是:计算机无法区分“server crashed”和“network broken”。
the symptom is the same: no response to a query over the network
两者的症状相同:对 network query 没有 response。
this difficulty seemed insurmountable for a long time
长期以来,这个困难看起来难以克服。
seemed to require outside agent (a human) to decide when to switch servers
看起来必须依靠 outside agent(人)决定何时切换 servers。
we'd prefer an automated scheme!
但我们更希望有 automated scheme!
Two partition-tolerant replication schemes were invented around 1990,
大约在 1990 年,出现了两种 partition-tolerant replication schemes:
Paxos and View-Stamped Replication
Paxos 和 View-Stamped Replication。
called "consensus" or "agreement" protocols
它们被称为“consensus”或“agreement” protocols。
in the last 15 years this technology has seen a lot of real-world use
在过去 15 年中,这项技术已经得到大量 real-world use。
the Raft paper is a good introduction to modern techniques
Raft paper 是了解现代技术的一个很好的入口。
Raft 中的 state-machine replication
*** topic: state-machine replication with raft
*** 主题:使用 Raft 的 state-machine replication。
state machine replication with Raft -- Lab 2 + 4 as example:
以 Lab 2 + 4 为例,看 Raft state machine replication:
[diagram: clients, 3 replicas, k/v layer + state, raft layer + logs]
[diagram: clients, 3 replicas, k/v layer + state, raft layer + logs](原文 diagram 占位,保留在此处。)
Raft is a library included in each replica
Raft 是包含在每个 replica 中的 library。
time diagram of one client command
一个 client command 的时间路径:
[C, L, F1, F2]
[C, L, F1, F2](原文时序示意,保留在此处。)
client sends Put/Get "command" to k/v layer in leader
client 把 Put/Get“command”发送给 leader 中的 k/v layer。
k/v layer calls Start() to invoke Raft
k/v layer 调用 Start() 来 invoke Raft。
leader's Raft layer adds command to log
leader 的 Raft layer 把 command 加入 log。
leader sends AppendEntries RPCs to followers
leader 向 followers 发送 AppendEntries RPCs。
followers add command to log
followers 把 command 加入 log。
leader waits for replies from a bare majority (including itself)
leader 等待 bare majority 的 replies(包括自身)。
entry is "committed" if a majority put it in their logs
如果 majority 已经把 entry 放入自己的 logs,该 entry 就是“committed”。
committed means won't be forgotten even if failures
committed 意味着即使发生 failures,该 entry 也不会被遗忘。
majority -> will be seen by the next leader's vote requests
majority → 下一个 leader 的 vote requests 会看到它。
leader "piggybacks" commit info in next AppendEntries
leader 会在下一次 AppendEntries 中“piggybacks”commit info。
leader and follower hand commands to k/v layer once entry is committed
entry committed 后,leader 和 follower 把 commands 交给 k/v layer。
ApplyMsg and applyCh in lab
在 lab 中对应 ApplyMsg 和 applyCh。
leader sends response to client
leader 向 client 发送 response。
为什么需要 logs
why the logs?
为什么需要 logs?
the service keeps the state machine state, e.g. key/value DB
service 保存 state machine state,例如 key/value DB。
the log is an alternate representation of the same information!
log 是同一 information 的另一种 representation!
why both?
为什么两者都需要?
the log orders the commands
log 为 commands 排序。
to help replicas agree on a single execution order
帮助 replicas 对单一 execution order 达成一致。
to help the leader ensure followers have identical logs
帮助 leader 确保 followers 拥有 identical logs。
the log stores tentative commands until committed
log 在 commands committed 之前保存 tentative commands。
the log stores commands in case leader must re-send to followers
如果 leader 必须重新发送给 followers,log 中仍保存着 commands。
the log stores commands persistently for replay after reboot
log 持久化保存 commands,以便 reboot 后 replay。
are the servers' logs exact replicas of each other?
各 servers 的 logs 是彼此 exact replicas 吗?
no: some replicas may lag
不是:一些 replicas 可能 lag。
no: we'll see that they can temporarily have different entries
不是:后面会看到,它们可能暂时拥有不同的 entries。
the good news:
好消息是:
they'll eventually converge to be identical
它们最终会 converge,变得 identical。
the commit mechanism ensures servers only execute stable entries
commit mechanism 确保 servers 只执行 stable entries。
Implementation challenges
Implementation challenges:
Implementation challenges:
Failures
Failures。
network partitions, lost messages, server crashes
包括 network partitions、lost messages 和 server crashes。
Concurrency
Concurrency。
within a server and between servers
发生在 server 内部以及 servers 之间。
Result: many possible executions and many corner cases
结果是:存在许多可能的 executions 和许多 corner cases。
many details to work -- figure 2
需要处理许多细节——见 Figure 2。
Today: electing a new leader, which must handle these challenges
今天:electing a new leader,而它必须处理这些 challenges。
Leader election(Lab 3A)
*** topic: leader election (Lab 3A)
*** 主题:leader election(Lab 3A)。
why a leader?
为什么需要 leader?
ensures all replicas execute the same commands, in the same order
确保所有 replicas 按相同顺序执行相同的 commands。
(some designs, e.g. Paxos, don't have a leader)
(某些 designs,例如 Paxos,并没有 leader。)
Raft numbers the sequence of leaders
Raft 为 leaders 的 sequence 编号。
new leader -> new term
new leader → new term。
a term has at most one leader; might have no leader
一个 term 最多有一个 leader;也可能没有 leader。
the numbering helps servers follow latest leader, not superseded leader
编号帮助 servers 跟随 latest leader,而不是已经被 supersede 的 leader。
when does a Raft peer start a leader election?
Raft peer 什么时候开始 leader election?
when it doesn't hear from current leader for an "election timeout"
当它在一个“election timeout”内没有听到 current leader 的消息时。
increments local currentTerm, tries to collect votes
它增加本地 currentTerm,并尝试收集 votes。
note: this can lead to un-needed elections; that's slow but safe
注意:这可能导致不必要的 elections;这会变慢,但仍然 safe。
note: old leader may still be alive and think it is the leader
注意:old leader 可能仍然 alive,并认为自己是 leader。
每个 term 最多一个 leader
how to ensure at most one leader in a term?
如何确保一个 term 中最多只有一个 leader?
(Figure 2 RequestVote RPC and Rules for Servers)
(Figure 2 的 RequestVote RPC 和 Rules for Servers。)
leader must get "yes" votes from a majority of servers
leader 必须从 servers 的 majority 获得“yes” votes。
each server can cast only one vote per term
每个 server 在每个 term 中只能投一票。
if candidate, votes for itself
如果它是 candidate,就投给自己。
if not a candidate, votes for first that asks (within Figure 2 rules)
如果它不是 candidate,就投给第一个请求 vote 的 candidate(同时遵守 Figure 2 的 rules)。
at most one server can get majority of votes for a given term
对于给定 term,最多只有一个 server 能获得 majority votes。
-> at most one leader even if network partition
→ 即使发生 network partition,也最多只有一个 leader。
-> election can succeed even if some servers have failed
→ 即使一些 servers failure,election 仍然可以成功。
note: again, majority is out of all servers (not just the live servers)
注意:再次强调,majority 是针对所有 servers 计算的(不只是 live servers)。
新 leader 如何被发现
how does a server learn about a newly elected leader?
server 如何得知 newly elected leader?
the leader sends out AppendEntries heart-beats
leader 发送 AppendEntries heartbeats。
with the new higher term number
其中带有新的、更高的 term number。
only the leader sends AppendEntries
只有 leader 发送 AppendEntries。
only one leader per term
每个 term 只有一个 leader。
so if you see AppendEntries with term T, you know who the leader for T is
因此,如果看到 term 为 T 的 AppendEntries,就知道 T 的 leader 是谁。
the heart-beats suppress any new election
这些 heartbeats 会压制新的 election。
leader must send heart-beats more often than the election timeout
leader 必须以高于 election timeout 的频率发送 heartbeats。
Election 失败
an election may not succeed for two reasons:
election 可能因两个原因失败:
- less than a majority of servers are reachable
- 可达的 servers 少于 majority。
- simultaneous candidates split the vote, none gets majority
- simultaneous candidates 分散了 votes,没人获得 majority。
what happens if an election doesn't succeed?
如果 election 没有成功,会发生什么?
no heartbeats -> another timeout -> a new election for a new term
没有 heartbeats → 再次 timeout → 为新的 term 发起新的 election。
higher term takes precedence, candidates for older terms quit
更高的 term 优先,旧 term 的 candidates 退出。
without special care, elections will often fail due to split vote
如果没有特殊处理,elections 会经常因为 split vote 失败。
all election timers likely to go off at around the same time
所有 election timers 很可能在相近时间到期。
every candidate votes for itself
每个 candidate 都投给自己。
so no-one will vote for anyone else!
所以没人会投给其他人!
so everyone will get exactly one vote, no-one will have a majority
于是每个人恰好得到一票,没人获得 majority。
随机 election timeout
how does Raft avoid split votes?
Raft 如何避免 split votes?
each server adds some randomness to its election timeout period
每个 server 都向自己的 election timeout period 加入一些 randomness。
[diagram of times at which servers' timeouts expire]
[diagram of times at which servers' timeouts expire](原文 diagram 占位,保留在此处。)
randomness breaks symmetry among the servers
randomness 打破 servers 之间的 symmetry。
one will choose lowest random delay
其中一个会选择最短的 random delay。
hopefully enough time to elect before next timeout expires
希望在下一个 timeout 到期前有足够时间完成 election。
others will see new leader's AppendEntries heartbeats and
其他 servers 会看到 new leader 的 AppendEntries heartbeats,
not become candidates
从而不成为 candidates。
randomized delays are a common pattern in network protocols
randomized delays 是 network protocols 中常见的 pattern。
如何选择 election timeout
how to choose the election timeout?
如何选择 election timeout?
- at least a few heartbeat intervals (in case network drops a heartbeat)
- 至少要有几个 heartbeat intervals(以防 network 丢失一个 heartbeat),
to avoid needless elections (which can cause an unnecessary pause)
以避免不必要的 elections(它们可能造成不必要的 pause)。
- short enough to react quickly to failure, avoid long pauses
- 又要足够短,以便快速响应 failure,避免长时间 pause。
- short enough to allow a few re-tries before tester gets upset
- 要足够短,让 tester 发怒之前可以进行几次 retries。
tester requires election to complete in 5 seconds or less
tester 要求 election 在 5 秒内完成。
- random part long enough to let one candidate succeed before next starts
- random part 要足够长,让一个 candidate 能在下一个 candidate 启动前成功。
Old leader 与新 leader 并存?
what if old leader isn't aware a new leader is elected?
如果 old leader 不知道 new leader 已经 elected,会怎样?
perhaps old leader didn't see election messages
也许 old leader 没有看到 election messages。
perhaps old leader is in a minority network partition
也许 old leader 位于 minority network partition 中。
new leader means a majority of servers have incremented currentTerm
new leader 意味着 majority 的 servers 已经 incremented currentTerm。
either old leader will see new term in a AppendEntries reply and step down
要么 old leader 会在 AppendEntries reply 中看到 new term,并 step down,
or old leader won't be able to get a majority of replies
要么 old leader 无法获得 majority 的 replies。
so old leader won't commit or execute any new log entries
因此 old leader 不会 commit 或 execute 任何新的 log entries。
thus no split brain
因此不会 split brain。
but a minority may accept old server's AppendEntries
但 minority 可能接受 old server 的 AppendEntries,
so logs may diverge at end of old term
所以 logs 可能在 old term 的末尾发生 divergence。
日志 divergence 示例
example log divergence
日志 divergence 示例。
a leader crashes before sending AppendEntries to all
某个 leader 在向所有 servers 发送 AppendEntries 之前 crash:
S1: 3
S1: 3
S2: 3 3
S2: 3 3
S3: 3 3
S3: 3 3
(the 3s are the term number in the log entry)
(这里的 3 是 log entry 中的 term number。)
worse: logs might have different commands in same entry!
更糟的是:同一个 entry 中的 commands 可能不同!
after a series of leader crashes, e.g.
例如,在一系列 leader crashes 之后:
10 11 12 13 <- log entry #
10 11 12 13 <- log entry #
S1: 3
S1: 3
S2: 3 3 4
S2: 3 3 4
S3: 3 3 5
S3: 3 3 5
how could this happen?
这怎么可能发生?
S2 is leader in term 3
S2 是 term 3 的 leader。
appends 10 to S1, S2, and S3
它把 10 append 到 S1、S2 和 S3。
appends 11 to S2 and S3 (S1 crashed)
它把 11 append 到 S2 和 S3(S1 crashed)。
S2 crashes, reboots quickly, and leader in term 4
S2 crash,随后快速 reboot,并在 term 4 成为 leader。
appends 12 to its log, and crashes.
它把 12 append 到自己的 log,然后 crash。
S3 becomes leader in term 5 (with help of S1)
S3 在 S1 的帮助下成为 term 5 的 leader。
appends a different entry for 12 to its log
它把另一个不同的 entry 作为 12 append 到自己的 log。
下一讲
next lecture we will see how Raft handles log divergence
下一讲我们会看到 Raft 如何处理 log divergence,
including an additional restriction on leader election
包括 leader election 的一项额外 restriction。
you can pass 3A without this restriction
没有这项 restriction,也可以通过 3A。
机制小结
flowchart LR
Client[Client command] --> Leader[Raft leader]
Leader --> Log[Replicated log]
Log --> Majority[Majority replicas]
Majority --> Commit[Commit]
Commit --> RSM[KV state machine]
RSM --> Reply[Client reply]
Raft 把“选出 leader”“复制 log”和“执行 committed commands”分成不同阶段。Lecture 6 主要处理第一阶段,并说明为什么 majority 和 term 能阻止两个 partition 同时产生有效 leader;日志冲突的收敛规则留到下一讲。
本篇验收
- 已固定官方
l-raft.txt共 219 行,SHA-256 为8bc18871653e77cfc84e76a470946890acffea8040d84dc5d1e163ae1a54bef8。 - 英文非空源行按原顺序逐行译注;RSM diagram、client command time diagram、日志示意和术语均保留。
- 本源文件没有独立 raster image。
- 本篇是 Spring 2026 lecture track 的第 6 篇;下一篇为 Lecture 7《Fault Tolerance: Raft (2)》。
参考资料
评论 (0)