Module 7: Raft II: Safety Proofs and the Figure 8 Trap
Goal
State the five safety properties of Raft (paper Figure 3) with quantifiers, prove each one from the handlers of Module 6, and explain with paper Figure 8 why the commit rule must count only entries of the leader’s current term.
Module 6 gave the mechanisms: RequestVote with the up-to-date test, AppendEntries with the consistency check, and the commit rule. This module asks the question the course exams care about: why do these few rules guarantee that no two servers ever apply different commands at the same log index, whatever the crashes, delays and partitions? The answer is a chain of five invariants. Each link is short, but the links are easy to mix up, and one condition of the commit rule (the entry must be from the current term) is the hinge on which the whole chain turns.
The proofs follow §5.3 and §5.4 of the extended Raft paper (1), in particular the safety argument of §5.4.3, written out with every step, and Ongaro’s thesis (2) for the formal version. Lecture 08.03 lists the five properties and asks the class to work out, for each, which rule guarantees it; lecture 08.04 returns to Leader Completeness and corrects a claim made in 08.02. Lecture 08.03 also walks through paper Figure 7. Kleppmann’s notes cover the same algorithm in §6.2 (3) with different variable names (the translation table is in Module 6). Election Safety was proved in Module 5; we use it here as a black box.
The five properties, stated formally
The question: what exactly does Raft promise? Lecture 08.03 puts it this way: if you wrote Raft as a TLA+ specification, these are the invariants the model checker would check in every reachable state.
We fix one execution of Raft with servers and a fixed configuration. Time is global and only used for reasoning (no server can read it). For a server and time we write for the log of at time , and for its entry at index , a pair (term, command). Two entries are equal when they have the same term and the same command. Since a term has at most one leader (Theorem 5.10), we write for the leader of term when it exists.
Raft’s notion of commitment was introduced in Definition 6.1 and made operational by the commit rule (Definition 6.8). For proofs we need a precise version that refers to the execution, not to what some server believes.
Definition 7.1 (Directly committed, committed in a term).
Let be an entry at index with term .
is directly committed if at some time , while still leader of term , knows that a majority of servers store an entry with index and term (in the protocol: and a majority of values, the leader included, are at least ).
An entry at index is committed in term if at some time the log of holds at index and also holds, at some index , an entry that is directly committed.
An entry is committed if it is committed in some term. Being stored on a majority is not enough: only an entry of the leader’s own term can be directly committed.
The commit rule of Module 6 sets exactly when the entry at becomes directly committed, so every index up to a leader’s holds an entry committed in that leader’s term. We repeat the rule here because it is the hypothesis everything below depends on:
The five properties of paper Figure 3, with quantifiers. In each line range over servers, over times, over terms and over log indices.
Property
Formal statement
Election Safety
Leader Append-Only
leader of at is a prefix of
Log Matching
same term at index in two logs equal entries at all
Leader Completeness
committed at in term , holds at
State Machine Safety
applies at , applies at
In words: one leader per term; a leader only appends to its own log; logs that agree on one entry agree on everything before it; a committed entry is in every later leader’s log; and no two servers ever apply different entries at the same index. Note that State Machine Safety talks about an index, not a term: lecture 08.03 phrases it as “in term four everyone applies x = 5”, but the guarantee is per position in the log.
The dependency structure is a short chain. Leader Completeness is the deep one; it uses all three properties above it, and State Machine Safety follows from it in a few lines.
To prove
We use
Log Matching
Election Safety, Leader Append-Only, the consistency check
Leader Completeness
Log Matching, Leader Append-Only, Election Safety, the vote test, the commit rule
The question: what do the handlers guarantee about logs before we think about commitment at all? The answer is three structural facts, each proved by looking at which lines of code can change a log.
Election Safety (Theorem 5.10). A candidate needs votes from a majority, two majorities of the same servers intersect (Lemma 5.8), and a server grants at most one vote per term, a promise kept across crashes because votedFor is on stable storage (Lemma 5.9). A server in the intersection of two winning vote sets would have voted twice. We also use one consequence: a server is leader of term during one contiguous time interval at most, because after a crash it restarts as a follower, and becoming leader again requires a new election, which increases its term.
Leader Append-Only (Proposition 6.7). The log changes in only three places: the leader appends a client command at the end; the AppendEntries receiver deletes a conflicting suffix; the receiver appends at the end. A leader of term never runs the deleting step, since that needs a request of term at least from a different leader, which would be either a second leader of term (impossible by Election Safety) or a higher term (which turns the leader into a follower first). Module 6 gives the proof in full.
We need two more simple facts about terms inside logs.
Lemma 7.2 (Terms inside logs).
At every time and for every server :
every entry of has term at most ;
the terms along are non-decreasing: for all ;
entries with term are created only by , and every entry in any log is a copy, at the same index, of an entry created this way.
Proof
We argue by induction over the steps of the execution. Initially all logs are empty, so the claims hold. Between crashes the in-memory only increases (Lemma 5.5), so apart from restarts claim 1 can only be broken by a step that adds entries.
Leader appends a client command. The new entry gets term and goes to the end, index . Claim 1 holds. By claim 1 before the step, every earlier entry has term at most , so claim 2 holds. The entry is created by , which is the leader of its term, so claim 3 holds.
Follower accepts AppendEntries. The request has term and sets . The entries in the request are copies of the sender’s log, and the sender is the leader of term , so by claim 1 for the sender their terms are at most ; claim 1 holds afterwards. By Lemma 6.5 the new log consists of the old positions (with ), then the request’s entries at indices , then possibly old entries after that were not in conflict. The request’s entries are consecutive entries of the sender’s log, so they are non-decreasing by claim 2 for the sender. At the seam, , which is the sender’s term at index , and that is at most the sender’s term at . If old entries survive after , no position in was in conflict, so those positions kept their old terms and the old log is unchanged up to its end; it was non-decreasing before. So claim 2 holds. Entries are copied to the same index, which with the induction hypothesis gives claim 3.
Crashes and restarts do not change stable storage. A restart may set back to its stored value (Remark 5.6), but the handlers of Module 6 persist every change of at once (an adopted term at the adopt step, before any entry of that term is appended), so the stored log never holds an entry above the stored term and claim 1 survives the restart. With these handlers the in-memory term equals the stored term after every step, so in this module it never decreases, even across restarts. Deleting a suffix keeps claims 1 to 3.
Log Matching
The question: if two logs agree at one position, how much else must they agree on? The answer is: everything before it.
Theorem 7.3 (Log Matching).
For all servers , all times and every index : if and both have an entry at index and these entries have the same term, then
the two entries at index are equal (same command), and
for every .
In words: an (index, term) pair identifies an entry and its entire history. The statement compares any two logs that ever existed, including two snapshots of the same server at different times.
Proof
Part 1. By Lemma 7.2 (claim 3) every entry with term at index , in any log at any time, is a copy of an entry that created at index . By Theorem 5.10 there is one such leader, and it is leader during one interval. During that interval it creates entries only by appending at the end of its log and never deletes (Proposition 6.7), so its log grows by one index per created entry and it never creates two entries at the same index. Hence there is exactly one entry with (index , term ) in the whole execution, and all copies are equal.
Part 2. Call a log snapshot old if it existed before the current step. We show by induction over the steps of the execution that the following holds for every pair of snapshots that have existed so far:
Base: initially every log is empty and (LM) holds vacuously. A step that does not change any log creates no new snapshot. We check the two steps that create a new snapshot of some server.
A leader appends a new entry at index , so . For indices , agrees with the old snapshot on , and (LM) for gives the claim. For : by part 1, is the first entry ever with its (index, term), so no other snapshot so far has that term at index ; the only pairs to check are with itself, which is trivial. (Later snapshots containing copies of are handled when they are created.)
A follower accepts AppendEntries with and entries, sent by the leader when its log was the old snapshot . Let be the follower’s old snapshot. The check passed, so (or ), and by (LM) for the old pair , . By Lemma 6.5 the new log satisfies and has the terms of at ; by part 1, equal terms at equal indices mean equal entries, so . Positions after , if any, survive only when nothing in conflicted, in which case is equal to up to the old length of and to on . So for every index of the prefix equals the prefix of some old snapshot (namely or ).
Now take and any snapshot with . Choose the old snapshot with . Then , and (LM) for the old pair (or trivially if ) gives for , hence . Deleting a suffix only shortens a log and creates no new (index, term) pair, so it preserves (LM) by the same argument with .
Every step preserves (LM), so it holds for all pairs of snapshots of the execution.
Intuition
The paper calls the consistency check an induction step. A follower only attaches new entries right behind a position where it agrees with the leader, and the leader’s log is itself consistent, so agreement can only ever spread from the left. Part 1 needs Election Safety: if two leaders could share a term, they could create two different entries with the same (index, term) and the whole argument would collapse.
The most used consequence: after AppendEntries with and entries succeeds, the follower’s log equals the leader’s log at sending time on (the step in the proof above). This is why the leader may set .
Leader Completeness
The question: why does a newly elected leader already hold every committed entry, without asking anybody? This is the property that lecture 08.02 got wrong and 08.04 put right.
Theorem 7.4 (Leader Completeness).
Assume every leader advances its commit index only by rule . For all terms , every index and every entry : if is committed at index in term and term has a leader, then at every time at which is leader, .
In words: a committed entry is present, at the same index, in the log of every leader of every later term.
The proof has two parts. Part A, the heart of §5.4.3, handles an entry that was directly committed by the leader of its own term. Part B extends this to entries committed indirectly, using Log Matching.
Proof
Part A: directly committed entries. Let be an entry with index and term , directly committed by . We prove that every later leader holds it. Suppose not. Among all terms whose leader does not hold at index at some time of its leadership, let be the smallest. So every leader of a term with holds at throughout its leadership (for , from the moment it created onward, by Proposition 6.7).
lacked when it was elected. A leader never deletes or overwrites (Proposition 6.7), and it never receives entries from others while leader. If it lacks at some time of its leadership, it lacked at index at the moment it won.
There is a voter. Let be the majority that stored when counted it, and the majority that granted votes to in term . By Lemma 5.8 there is a server (Figure 7.1). Call it the voter. (If is itself, voting for itself, then steps 3 and 4 below show that it held when it won, which already contradicts step 1.)
The voter stored before it voted. stored by accepting an AppendEntries of term from (or , which holds from its creation in term ). Granting a vote in term is acting on term , so at every later time, including after restarts (Lemma 5.5, claim 2). After the vote, would reject any AppendEntries of term . So the storing came first.
The voter still stored when it voted. Between storing and voting, changes its log only by accepting AppendEntries. Each such request has a term with , and : term has only one leader (Theorem 5.10), and it sends AppendEntries only after it has won, hence after voted. A follower deletes entries only from the first index where its entry and the request’s entry have different terms. The request’s previous entry and its entries are copied from the log of at sending time. By Leader Append-Only, is a prefix of a log of that holds at : for take , which holds by the choice of ; for take any log of after it created (the request may have been sent before existed and arrive late). Since and both hold at , Log Matching (Theorem 7.3) says they agree on , so no index is a conflict, whether or not the request reaches index , and is not deleted. Crashes do not touch stable storage. So holds at when it votes, and in particular .
The vote test. granted its vote, so ‘s log was at least as up-to-date as ‘s (Definition 6.3). Write for the last term and last index of ‘s log at election and for those of ‘s log at the vote. Then , or and . Two cases, both contradictions.
Case . Both last entries have term , so both were created by (Lemma 7.2), at indices . When created its entry at index , its log already held its entry at index (it creates entries at increasing indices and never deletes). Log Matching applied to ‘s log and that snapshot of ‘s log (same term at index ) shows that holds ‘s entry at ; this is also ‘s last entry (same index, same term). Log Matching for and at index then gives that holds ‘s entry at , which is . Contradiction with step 1.
Case . The voter’s log holds , of term , at index , and terms along a log are non-decreasing (Lemma 7.2), so . Hence . Also : entries of term are created only by , after its election. So , and by the minimality of , held at index throughout its leadership, in particular when it created the entry at index that ends ‘s log; and because that entry was appended behind . Log Matching for ‘s log and that snapshot (same term at index ) shows that holds at index . Contradiction with step 1.
Both cases are impossible, so no such exists: every leader of a later term holds at index .
Part B: all committed entries. Let be committed at index in term : some log of holds at and a directly committed entry at some index . The entry is directly committed by the leader of its own term (Definition 7.1). Since is in a log of , (Lemma 7.2, claim 1), so Part A applies with in place of : holds at index . The log of and that log of have the same term at index , so by Log Matching they agree on , and in particular holds at . This is how entries of older terms, such as index 2 in Figure 8 (e), are protected: indirectly, through a later current-term entry.
Intuition
Two majorities meet in at least one server, the voter. The voter has the committed entry and only votes for candidates whose log is at least as up-to-date as its own. “At least as up-to-date” means either the same last term and at least as long, in which case the candidate’s log contains the voter’s log, or a later last term, in which case the candidate’s last entry came from a more recent leader, who (by the smallest-counterexample choice) already had the entry. Either way the candidate has it.
commit quorum: stores (i, T)vote quorum: elected leader of US1has (i, T)S2has (i, T)S3has, votesS4votesS5votesFigure 7.1. The commit quorum of an entry from term T and the vote quorum of the leader of a later term U overlap; here the overlap is S3 (the voter), the key server of the Leader Completeness proof.
Remark 7.5 (Where the current-term condition is used).
Read step 7 again. It needs , the voter’s last term is at least the term of the committed entry, and this holds because the committed entry is from term, the term in which it was counted. Suppose instead that counted replicas of an older entry with term . The same argument only gives , and a candidate whose last term satisfies is not covered by the induction (its leader came before and may never have seen the entry). That is exactly server S5 in paper Figure 8: its last term is 3, between the entry’s term 2 and the counting leader’s term 4. The hypothesis of Theorem 7.4 is not a technicality; the next section shows the theorem is false without it.
Lecture correction: a new leader does not learn committed entries from followers
Lecture 08.02 (and once more 08.03) says that a committed entry may survive on a single live server that does not become leader, and that the new leader must therefore learn committed entries from followers and propagate them. Lecture 08.04 retracts this: by Leader Completeness, any server that can win an election already holds every committed entry. Raft has no follower-to-leader transfer at all; entries flow only from leaders to followers (paper §5.4.1). The 08.04 argument (if a majority stored the entry and all but two of them crashed, no candidate can collect a majority without one of those two, and they refuse anyone less up-to-date) is the special case of steps 2 and 5 above. The repair mechanism that 08.02 describes is how Paxos works; Module 10 puts the two side by side.
Lecture correction: Leader Completeness is a safety property
Lecture 08.03 calls Leader Completeness “log completeness” and says it is critical for liveness. It is a safety property: it says something bad (a later leader without a committed entry) never happens. Liveness, that commands eventually get committed, is a separate matter, discussed at the end of this module.
State Machine Safety
The question: how does a statement about leaders’ logs become a statement about what every server executes? Two more steps: what a server applies is committed, and committed entries never disagree.
Lemma 7.6 (Everything up to commitIndex is committed).
At every time, for every server and every index , the entry is committed in some term , and it stays at index of forever after.
Proof
Committed. By induction over steps. A leader raises to only by rule ; then the entry at is directly committed and every entry at an index of the leader’s log is committed in its term. A follower raises to only after a successful AppendEntries from the leader of term ; by Log Matching (consequence after the theorem) its log agrees with the leader’s log at sending time on , and by the induction hypothesis every index up to the leader’s held an entry committed in some term . So every holds a committed entry.
Stays. Only the follower’s delete step removes entries. Suppose holds at , committed in term , and later accepts AppendEntries from . Then . The request’s previous entry and its entries are copied from the log of at sending time, and by Leader Append-Only is a prefix of a log of that holds at . If , take , which holds at by Theorem 7.4. If , take any log of from a time at which it holds at (one exists by definition) or later; this covers a delayed request sent before that time. Since and both hold at , Log Matching gives agreement on , so no index is a conflict, whether or not the request reaches index , and is not deleted.
Theorem 7.7 (State Machine Safety).
For all servers and every index : if applies entry at index and applies entry at index (at any times), then . Since every server applies indices in the order , all servers apply the same sequence of commands, each server a prefix of the longest one.
Proof
A server applies index only when , so by Lemma 7.6 is committed at in some term and is committed at in some term . Without loss of generality .
By the definition of “committed in term ”, the log of holds at index at some time of its leadership. If , Theorem 7.4 says that holds at index at every time of its leadership. If , the definition says holds at at some time. In both cases one leader holds and at the same index during its leadership, and by Leader Append-Only its entry at never changes while it is leader. Hence .
For the second sentence: increases by one at a time, so a server that has applied index has applied exactly the entries at , in order, and by the first part these are the same entries on every server.
The second sentence is the bridge back to Modules 1 and 4: the sequence of applied commands is a total order broadcast that every replica delivers in the same order, so deterministic state machines stay in the same state.
The Figure 8 trap
The question: why can a leader not commit an entry from an older term once it sits on a majority, when that seems like the obvious rule? Paper Figure 8 is a five-server run in which the obvious rule loses a committed entry. Lecture 08.03 states the commit rule without the current-term condition, which is exactly this bug (Module 6 already flagged it).
Try it: tick “commit older-term entries by counting replicas (unsafe)”, choose future (d) and step to the end. S1 applies the term-2 entry at index 2 while it leads term 4, S5 then overwrites index 2, and the State Machine Safety and Leader Completeness rows turn to VIOLATED. Untick the box and replay (d): S1 never commits index 2 and nothing is violated. Finally choose future (e): S1 first replicates a term-4 entry, which commits index 2 indirectly, and S5 can no longer win.
S1S2S3S4S51231212111(a)S1 leads t212312121113(b)S5 leads t31231241212113(c)S1 leads t41231313131313(d)S5 leads t5future 1123124124124113(e)S1 leads t4future 2Figure 7.2. Raft paper Figure 8 redrawn. Each box is a log entry labelled with its term; the dot marks the leader of the panel; the shaded box is the term-2 entry at index 2. Panels (d) and (e) are two alternative futures of panel (c), not consecutive steps.
Each panel, with the votes that make it possible. Recall that a server grants a vote only if the candidate’s log is at least as up-to-date as its own (last term first, then length).
Panel
What happens
Votes and why
(a)
S1 leads term 2, appends entry (2, term 2), reaches only S2
S1 won term 2 earlier
(b)
S1 crashes; S5 wins term 3 and appends its own entry at index 2
S3, S4, S5: their logs end in term 1, as does S5’s; S1, S2 (last term 2) would refuse
(c)
S5 crashes; S1 restarts, wins term 4, copies (2, term 2) to S3, appends (3, term 4)
from S2, S3, S4 (last terms 2, 1, 1, none above S1’s 2); S5 (last term 3) would refuse
(d)
S1 crashes; S5 wins term 5 and overwrites index 2 on every server
S2, S3, S4: their last term is at most 2, below S5’s 3; only S1 (last term 4) would refuse, and it is down
(e)
instead, S1 first copies (3, term 4) to S2 and S3
S5 can no longer win: S1, S2, S3 end in term 4 and refuse; S4 and S5 are two of five
S5 needs to reach term 5 in (d): its term was 3, its first timeout takes it to term 4, which S2 and S3 reject because they are already in term 4 (they reply with term 4), so it times out again and campaigns in term 5. This detail does not matter for safety; only the logs do.
The unsafe rule. Suppose the commit rule had only its first two conditions. In panel (c), S1 (leader of term 4) sees the entry (index 2, term 2) on S1, S2, S3, three of five, and marks index 2 committed. It applies the command at index 2, say x := A, and tells its client “done”. Now continue with future (d): S1 crashes, S5 wins term 5 with the votes of S2, S3, S4 and replicates its entry (index 2, term 3), say x := B, to everyone, then commits and applies it. S1 has applied x := A at index 2 and S5 has applied x := B at index 2. State Machine Safety is broken, and the client that was told “done” lost its write.
What the correct rule does. In panel (c), S1 counts three replicas of index 2, but , so rule does not fire and nobody applies index 2. Future (d) is still possible, and it is harmless: S5 overwrites an entry that was never committed. In future (e), the term-4 entry at index 3 reaches S1, S2, S3; now , S1 commits index 3, and index 2 is committed with it. From then on any candidate needs a vote from S1, S2 or S3, all of which end in term 4, so a server with last term 3, such as S5, cannot win.
Intuition: why older entries need a current-term companion
A replica count of an old entry says “a majority has this entry”, but not “a majority would refuse every candidate that lacks it”. The vote test compares last terms, and an old entry can sit under a log whose last term is lower than a rival’s. A current-term entry on a majority changes the last term of a majority of logs to the newest possible term, and then nobody without it can collect votes. Raft keeps the original term numbers of entries (paper §5.4.2), which is why it needs this rule; protocols that re-stamp old entries with the new leader’s number avoid it at the cost of rewriting them (Module 10).
Two practical consequences. First, an entry of an old term is committed only when a later current-term entry is; if no client sends a command, it may stay uncommitted. That is why a new leader appends a blank no-op entry at the start of its term (paper §8, Module 6). Second, the rule is conservative: paper §5.4.2 notes that an old entry stored on every server could safely be committed (Homework 7), but Raft keeps the simpler rule.
Exam trap: "(d) and (e) happen one after the other"
Panels (d) and (e) are alternative futures of panel (c), not consecutive steps. In (e) S5 cannot win any more, so (d) cannot follow (e). A related wrong answer: “Figure 8 shows that Raft can lose a committed entry.” It shows that the naive rule could; in correct Raft, index 2 in panel (c) is not committed, so overwriting it in (d) loses nothing a client was promised.
Reading Figure 7: who could have produced these logs, and who could win
The question: how different can followers’ logs be when a new leader takes over, and how do we check a claimed log state for consistency with the invariants? Lecture 08.03 walks through paper Figure 7: seven servers, a freshly elected leader of term 8, and six possible follower logs (a) to (f).
Try it: read the table of possible winners before stepping. Only the leader, (a), (c) and (d) pass the up-to-date test with enough votes; compare this with the analysis below. Then let the term-8 leader run and watch nextIndex walk back for each follower until every log equals the leader’s.
123456789101112leader1114455666term 8, just elected(a)111445566missing entry; votes yes(b)1114missing entries; votes yes(c)11144556666extra entry; votes no(d)111445566677extra entries; votes no(e)1114444missing and extra; yes(f)11122233333missing and extra; yesFigure 7.3. Raft paper Figure 7 redrawn: the log of the term-8 leader (shaded) and six possible follower logs in a 7-server cluster. The right column says how each follower differs and whether it could have voted for this leader.
How each follower log could arise. The paper’s caption gives one story for (f); here is one for each. Each story must respect Log Matching and the fact that only the leader of term creates term- entries.
Log
Last (term, index)
One way it could arise
(a)
(6, 9)
missed the last term-6 append (crashed, or a lost message)
(b)
(4, 4)
crashed early in term 4 and stayed down
(c)
(6, 11)
received one more term-6 entry than the term-8 leader; the term-6 leader (perhaps c) appended index 11, then lost leadership
(d)
(7, 12)
led term 7 (or was its only follower to receive anything), appended 11 and 12, crashed before replicating them
(e)
(4, 7)
took entries 6 and 7 from the term-4 leader (or was it), which lost leadership before replicating them; meanwhile term 5 overwrote index 6 elsewhere
(f)
(3, 11)
led term 2, appended 4 to 6, crashed; restarted, led term 3, appended 7 to 11, crashed again before committing any of them
Lecture 08.03 tells the (e) and (d) stories as “e was the leader of term 4” and “d was the leader of term 7”. Both are possible, but the logs alone do not prove it: a follower that received entries from that leader would look the same.
Who voted for the leader of term 8? Seven servers, so a winner needs four votes, its own included. The leader’s log ends in (term 6, index 10). A follower grants its vote only if that is at least as up-to-date as its own last (term, index):
(a) (6, 9): same last term, shorter, so yes. (b) (4, 4): lower last term, yes.
(c) (6, 11): same last term but longer, so no. (d) (7, 12): higher last term, no.
(e) (4, 7) and (f) (3, 11): lower last term, yes, even though (f) is longer.
So the leader can have collected the votes of a, b, e, f and itself (five, at least four needed), as lecture 08.03 says. Note what length does not buy: (f) has eleven entries and still loses the comparison.
Who else could win an election? Suppose the term-8 leader crashed at once and everyone campaigned in term 9 with the logs shown. Ordering the last (term, index) pairs gives d (7, 12) above c (6, 11) above the old leader (6, 10) above a (6, 9) above e (4, 7) above b (4, 4) above f (3, 11). A candidate gets votes from itself and every server ranked at or below it:
Candidate
Votes it can get
Can win (needs 4)?
(d)
all 6 live servers
yes
(c)
all live but d: 5
yes
(a)
a, e, b, f: 4
yes, just
(e)
e, b, f: 3
no
(b)
b, f: 2
no
(f)
f: 1
no
Is it dangerous that (d) or (c) could win and later overwrite entries of the old leader? Only if those entries were committed. Every committed entry is on a majority of four servers. Index 10 (term 6) is on the leader, c and d only, three servers, so it cannot be committed; (a) winning and dropping it is fine. Indices 1 to 9 of the leader’s log are on at least four servers and may be committed, and indeed every candidate that can win, (a), (c), (d), holds all of them. This is Leader Completeness seen from the data.
After the election. The leader initializes for every follower, so its first request checks index 10. For (a) and (b) the walk-back stops at their last index (9 and 4) and the missing entries are appended. For (c) and (d) the check at index 10 succeeds at once, because both hold the leader’s term-6 entry there; their extra entries after index 10 are not in conflict with anything sent, so they survive as a stale, harmless suffix until the leader appends its own index 11 and the next request deletes them (step 2 of the receiver in Module 6). They are never applied, since a follower’s commit index is bounded by the last entry the leader verified. For (e) the walk-back stops at index 5 and for (f) at index 3; everything after those positions is replaced by the leader’s entries.
Worked example: the proof on a concrete run
Five servers A, B, C, D, E. We follow the Leader Completeness proof step by step on one run, so that each step can be checked against concrete logs. Logs are written as lists of terms.
Term 2, leader A. A appends a client command at index 3, giving A the log (1, 1, 2). It sends AppendEntries with , ; B and C accept. Now A, B, C hold (1, 1, 2). The entry at index 3 has term 2 = A’s current term and is on three of five servers, so by rule A sets . The commit quorum is .
A crashes. D and E still hold (1, 1). D times out and campaigns in term 3 with last (term, index) = (1, 2).
Who votes for D? E: same last term, same length, yes. B and C: their last term 2 is above D’s 1, no. So D gets two votes, D and E, short of three. D cannot win. The proof’s prediction: any winning vote set intersects , and every member of that set refuses D.
C times out and campaigns in term 4 with last (2, 3). D and E (last (1, 2)) grant; B (last (2, 3), same) grants. C wins with or any three of them. Leader Completeness holds trivially: C has the entry.
Map to the proof. Suppose someone claimed a leader of a term without the entry. Step 2 picks a voter in , here B or C. Step 3: B stored the entry in term 2 before voting in term 4, since after voting it would reject term-2 AppendEntries. Step 4: nothing removed it. Step 5: the candidate’s last (term, index) must be at least B’s (2, 3). Step 6: if the candidate’s last term is 2, it was created by the term-2 leader A at an index at least 3, so its log contains A’s entry at 3. Step 7: if the candidate’s last term is larger, say 3, then a term-3 leader must have created that entry, and by minimality that leader already held the entry at index 3, so the candidate does too. In this run term 3 has no leader at all (D failed and nobody else ran), so no log can even end in term 3.
The run also shows the role of minimality: in step 7 the argument leans on the leaders of the intermediate terms (here term 3, which had none), and the smallest counterexample guarantees those leaders are already known to be fine.
Safety and liveness
The question: everything above holds in a fully asynchronous network, with arbitrary delays. Then why does Raft need timeouts at all? Because safety and liveness are different promises.
Lecture 08.03 makes the point with a slogan: a safe system can be a dead system. A cluster in which every server refuses every request never violates any of the five properties. None of the proofs in this module mentions time, message delay, or the election timeout; they hold in every execution of the asynchronous model, including executions with partitions, message reordering and arbitrarily slow servers. That is the meaning of “safety does not depend on timing” (paper §5.6, Kleppmann slide 107).
Liveness, that a client command is eventually committed, needs more: a majority of servers up and connected, and a stable leader for long enough. FLP (Module 4) says no deterministic algorithm can guarantee this in a purely asynchronous system. Raft gets it under partial synchrony with randomized election timeouts and the timing requirement of paper §5.6,
where broadcastTime is one parallel round trip to all servers (0.5 to 20 ms) and MTBF the mean time between failures of one server (months). If elections keep splitting votes, or a leader keeps being deposed, Raft stays safe and makes no progress.
Figure 8 has a liveness side too. With the current-term rule, an old entry is not committed until the new leader commits an entry of its own term, so a leader that receives no client commands could leave old entries uncommitted indefinitely. The no-op entry at the start of each term removes that gap; it is a liveness fix, not a safety fix.
Summary: the invariants and where they come from
The labels in the last column refer to the handlers of Module 6 (paper Figure 2).
Property
Statement (short)
Guaranteed by
Handler line
Election Safety
at most one leader per term
majority votes, one persisted vote per term
RequestVote: grant only if votedFor null or same
Leader Append-Only
a leader only appends to its log
deletion only in the follower path
client request: append at end
Log Matching
same (index, term) means same prefix
one leader per term + consistency check
AppendEntries step 1 (check), step 2 (delete only on conflict)
Leader Completeness
committed entry is in every later leader
quorum overlap + up-to-date vote + current-term commit
Majority intersection by itself is not the argument. It only produces a server (the voter) that has seen both events; the protocol rules then force that server to transmit what it knows: it rejects stale AppendEntries after voting (term monotonicity), it keeps the entry (Log Matching and the induction), and it refuses candidates that lack it (the vote test). Remove any one rule and the proof breaks at a specific step.
What breaks
The proofs assume crash-recovery with stable storage: currentTerm, votedFor and log[] must be on disk before a server replies. If a server forgets its vote after a restart, Election Safety fails (Module 5); if it loses log entries, the voter in step 4 may no longer hold the committed entry. They assume a fixed configuration: changing the set of servers naively can create two disjoint majorities (paper §6, joint consensus). And they assume non-Byzantine servers: lecture 08.02 stresses that a single server sending forged AppendEntries can make every follower delete its log, and a single bit flip on the leader can spread to all replicas. Finally, the guarantees are only safety; under a partition without a majority, or permanent timing chaos, nothing gets committed.
Why it matters
Leader Completeness is the reason a Raft leader can be “the truth”: it never has to ask anyone about the past, and entries only flow from leader to followers. State Machine Safety is the property that turns the replicated log into a correct state machine replication service (Module 4). Module 8 proves the analogous fact for Paxos (a chosen value stays chosen), where the new leader instead learns the past from a quorum during Phase 1, and Module 9 does the same per slot. Module 10 shows that both proofs are instances of one lemma about intersecting quorums.
Further reading
The Raft paper, §5.3 to §5.4, Figures 3, 7, 8, 9 (1).
Ongaro’s thesis, chapter 3 and appendix B (the TLA+ specification and the full proof) (2).
Kleppmann’s notes §6.2 for the same algorithm in his notation (3).
Howard and Mortier compare Raft’s election restriction with Paxos’s log repair (4).
Ongaro D, Ousterhout J. In Search of an Understandable Consensus Algorithm. In: Proceedings of the USENIX Annual Technical Conference (ATC). 2014. p. 305–19.
2.
Ongaro D. Consensus: Bridging Theory and Practice [phdthesis]. Stanford University; 2014.
3.
Kleppmann M. Distributed Systems: Lecture Notes [Internet]. University of Cambridge, Computer Science Tripos Part IB; 2021. Available from: https://www.cl.cam.ac.uk/teaching/2122/ConcDisSys/dist-sys-notes.pdf
4.
Howard H, Mortier R. Paxos vs Raft: Have We Reached Consensus on Distributed Consensus? In: Proceedings of the 7th Workshop on Principles and Practice of Consistency for Distributed Data (PaPoC). 2020.