Consensus with Raft
How Raft elects one leader per term, replicates a log, decides when an entry is committed, and what a leader stranded in a minority can and cannot do.
35 min8 steps
By the end you can
- Compute majority sizes and the number of failures a Raft cluster tolerates
- Explain how terms, single votes and randomised timeouts elect at most one leader per term
- Decide which log index is committed from the followers' match indexes, including the current-term rule
- Predict what a leader cut off in a minority partition can and cannot do
Why replicas need consensus
Same commands, same order
- Every replica reaches the same state
- The hard part is the order of slot 42
FLP (1985)
- No always-terminating algorithm
- Asynchronous, even one crash
Paxos and Raft
- Safety always
- Liveness when timeouts work
Majorities, and why clusters are odd
- 3 servers1 failure
- 4 servers1 failure
- 5 servers2 failures
- 7 servers3 failures
Loading question…
Terms are Raft's logical clock
Term 4
Leader A
Term 5
Split vote, no leader
Term 6
Leader B elected
Rule
Higher term seen: adopt it, step down
Elections and randomised timeouts
Select normal election, then split vote in Script.
Loading question…
Replicating the log
Put the steps of a successful client write in order.
- 1The leader replies to the client
- 2The leader advances its commit index and applies the command
- 3The leader appends it to its own log
- 4The client sends the command to the leader
- 5A majority, counting the leader, has stored the entry
- 6The leader sends AppendEntries to the followers
When is an entry committed?
Committed when
majority
of followers store it
Count replicas for
current term
only own-term entries
Older entries
indirect
commit behind a new one
Select normal election in Script and compare log length with commit index.
Loading question…
A leader stranded in a minority
Select minority partition, then partition heal with log repair in Script.
Check your mastery
Take
5 match indexes
include the leader's own
Sort, pick
3rd highest
Score 80% or more. In the operational sandbox, kill a database leader and compare the write outage with this lesson's timeouts.
Mastery check
5 questions on fresh variants. One attempt each, no time limit. Score 80% or more to master the lesson.
Answer this step to continue.