13-05
Chapter 13 · Distributed systems, honestly
Raft-lite log replication
00Recall
One card from 13-03 (how terms make an old leader harmless), one from 13-02 (why overlap alone is not enough) and one from 12-04 (what the small-scope hypothesis claims, and what it does not). All three meet in this lesson's model.
13-03-b from Leader election, simplified
A leader of an old term still thinks it leads. How does a Raft-style system stay correct anyway?
Show answer
Everything is stamped with a term, and servers that have seen a newer term refuse anything older, so the old leader cannot get a majority to agree to anything.
13-02-a from Agree on one value
Two majorities of the same acceptors always share a member. Why is that not enough to keep two ballots from choosing different values?
Show answer
The shared acceptor's old vote must change what the new leader proposes. Without the rule that the leader adopts its quorum's newest vote, nobody listens to it.
12-04-b from Simulation and honest claims
What does the small-scope hypothesis claim, and what does it not?
Show answer
That many bugs show up in small instances, so small models are the right place to start. It is an empirical observation, not a proof: a bug that needs four of something cannot appear in a model with three.
Skip recall Recall is never required.
01Commit
A client writes a key, and the store answers OK. A minute later the leader changes, and the write is gone from every replica. The replicas still agree with each other, entry for entry. The store said OK, and the write vanished. This lesson is about the one election rule that prevents it.
A replicated store needs more than one value. It agrees on a sequence of them, a replicated log, where each entry is a command, and every server applies the same commands in the same order. Raft is one well-known way to keep such a log. specs/RaftLite.tla is a cut-down version that TLC can search.
A leader appends entries stamped with its term and copies them to followers, one entry at a time. A follower that disagrees with the leader drops the conflicting entries and takes the leader's. Once a majority holds an entry of the leader's own term, the leader marks it, and every entry before it, committed: the system has told a client that this command will happen. An entry is only its term number; the commands themselves are abstracted away. An election is one step; 13-03 looked at elections on their own, and here only the vote rule matters. That rule is Raft's: a voter backs a candidate only if the candidate's log is at least as up to date as its own.
UpToDate(a, b) is defined as
or LastTerm(a) > LastTerm(b)
or LastTerm(a) = LastTerm(b) and Len(a) greater than or equal to Len(b)Two properties. LogMatching: if two logs hold the same term at the same index, they agree on everything up to it. StateMachineSafety: never two different entries committed at the same index.
Commit a
StaleVoteRaft is RaftLite with UpToDate removed from Elect. Three servers, MaxTerm = 2, MaxLog = 2. What does TLC report?
- No violation. A commit needs a majority, an election needs a majority, and two majorities share a server.
- StateMachineSafety is violated: a new leader without a committed entry replaces it with its own.
- LogMatching is violated: a leader with the wrong history leaves logs that disagree about earlier entries.
Answer StateMachineSafety is violated: a new leader without a committed entry replaces it with its own.
- If you picked A
- The shared server is there. It holds the committed entry and votes in the next election. Without the up-to-date check, it votes for a candidate that lacks the entry anyway, and what it knows changes nothing.
- If you picked C
- Replicate makes a follower drop whatever conflicts with the leader, so the logs keep matching each other even while a committed entry disappears from them. The logs are consistent; they are consistently wrong.
Commit b
EagerCommitRaft drops a different condition: a leader may commit an entry from an older term by counting the servers that hold it. Same bounds as RaftLite.cfg. What does TLC report?
- StateMachineSafety is violated: an old entry committed by counting copies is later replaced.
- No violation, every action fires, and CommittedAcrossTerms is reached.
- LogMatching is violated: committing an older entry changes the leader's log.
Answer No violation, every action fires, and CommittedAcrossTerms is reached.
- If you picked A
- That bug is real, and it needs more terms than this model allows. With two terms, the behavior that breaks it does not exist in the model, so TLC cannot find it.
- If you picked C
- Commit changes only committed; its last line leaves the logs unchanged. A step can do only what its action says.
02Lab
./tla check chapters/13-distributed-systems/specs/StaleVoteRaft.cfg13-distributed-systems/specs/StaleVoteRaft13-distributed-systems/specs/StaleVoteRaft StaleVoteRaft.cfg (Servers={s1, s2, s3}, MaxTerm=2, MaxLog=2)
parse ok
model check: invariant StateMachineSafety violated; trace has 9 states (8 steps); 1709 distinct states
expected outcome met: violation StateMachineSafety
trace (changed variables only; ./tla trace --full shows all):
1 Init term = (s1 :> 0 @@ s2 :> 0 @@ s3 :> 0), role = (s1 :> "follower" @@ s2 :> "follower" @@ s3 :> "follower"), log = (s1 :> <<>> @@ s2 :> <<>> @@ s3 :> <<>>), committed = {}
2 Elect term: (s1 :> 0 @@ s2 :> 0 @@ s3 :> 0) -> (s1 :> 1 @@ s2 :> 1 @@ s3 :> 0), role: (s1 :> "follower" @@ s2 :> "follower" @@ s3 :> "follower") -> (s1 :> "leader" @@ s2 :> "follower" @@ s3 :> "follower")
3 ClientRequest log: (s1 :> <<>> @@ s2 :> <<>> @@ s3 :> <<>>) -> (s1 :> <<1>> @@ s2 :> <<>> @@ s3 :> <<>>)
4 Replicate log: (s1 :> <<1>> @@ s2 :> <<>> @@ s3 :> <<>>) -> (s1 :> <<1>> @@ s2 :> <<1>> @@ s3 :> <<>>)
5 Commit committed: {} -> {<<1, 1>>}
6 Elect term: (s1 :> 1 @@ s2 :> 1 @@ s3 :> 0) -> (s1 :> 2 @@ s2 :> 1 @@ s3 :> 2), role: (s1 :> "leader" @@ s2 :> "follower" @@ s3 :> "follower") -> (s1 :> "follower" @@ s2 :> "follower" @@ s3 :> "leader")
7 ClientRequest log: (s1 :> <<1>> @@ s2 :> <<1>> @@ s3 :> <<>>) -> (s1 :> <<1>> @@ s2 :> <<1>> @@ s3 :> <<2>>)
8 Replicate log: (s1 :> <<1>> @@ s2 :> <<1>> @@ s3 :> <<2>>) -> (s1 :> <<2>> @@ s2 :> <<1>> @@ s3 :> <<2>>)
9 Commit committed: {<<1, 1>>} -> {<<1, 1>>, <<1, 2>>}Reveal for commit a
StateMachineSafety is violated, with a trace of 9 states. An entry of the first term is committed. A server whose log does not hold it wins the second term, appends its own entry at the same index, replicates it over the committed one, and commits it.
./tla check chapters/13-distributed-systems/specs/EagerCommitRaft.cfg13-distributed-systems/specs/EagerCommitRaft13-distributed-systems/specs/EagerCommitRaft EagerCommitRaft.cfg (Servers={s1, s2, s3}, MaxTerm=2, MaxLog=2)
parse ok
model check: no violation found in 1222 distinct states (4852 generated, depth 10)
expected outcome met: pass
coverage: all 4 actions fired
witness CommittedAcrossTerms reached (trace of 8 states)Reveal for commit b
No violation in 1,222 distinct states, with full coverage and the witness reached. RaftLite itself reports 1,222 distinct states with the same bounds. Hold on to that comparison.
03Reason
If you picked "no violation" for StaleVoteRaft, you made 13-02's argument, and the overlap is really there. The majority that committed the entry and the majority that elected the new leader share a server, and that server holds the entry. It voted anyway.
Write something before openingthe voter that holds the committed entry cannot know it is committed. What can it check instead, and is that enough on its own?
It can compare its own log with the candidate's: last term first, then length. That compares only the ends of two logs, and on its own it is not enough. A candidate with log [1, 3] is more up to date than a voter with [1, 2, 2], and lacks two of the voter's entries. What the check does buy is narrower: the winner holds every entry that was committed under Raft's rule that a leader commits only entries of its own term. The induction over terms needs three things together: the up-to-date check, quorum overlap, and that commit rule. LogMatching helps, because a shared entry vouches for everything before it, but 13-05-ex3 shows what breaks without the third.
Now the second wrong answer. StaleVoteRaft.matching.cfg checks the broken cluster for LogMatching alone:
./tla check chapters/13-distributed-systems/specs/StaleVoteRaft.matching.cfg13-distributed-systems/specs/StaleVoteRaft.matching13-distributed-systems/specs/StaleVoteRaft.matching StaleVoteRaft.matching.cfg (Servers={s1, s2, s3}, MaxTerm=2, MaxLog=2)
parse ok
model check: no violation found in 2575 distinct states (8017 generated, depth 15)
expected outcome met: pass
coverage: all 4 actions fired
witness TwoCommitsClash reached (trace of 9 states)No violation, and the witness TwoCommitsClash, the very state StaleVoteRaft.cfg reports, is reached. Logs match in every reachable state, including the one where a committed entry is gone.
Write something before openinghow can every log match every other log while a promise to a client has been broken?
Because LogMatching is about the logs as they are now, and a promise is about the past. The new leader overwrote the old entry everywhere it went, cleanly. Nothing in the logs remembers the old entry any more. Only committed does: it records every entry a leader ever told a client about, and no action removes anything from it. That is a history variable, and without it you could not even state StateMachineSafety. To check a promise about the past, keep the past in the state.
Then the second commit. EagerCommitRaft passes, and with the same number of distinct states as RaftLite. Is that a coincidence?
So the check of the condition could not have failed. That is not evidence that the condition is unnecessary; it is evidence that two terms are too few to need it. The condition protects against a leader that commits an older entry that a later leader can still overwrite, and setting that up takes more terms than two. The stretch, 13-05-ex3, asks you to find how many. Meanwhile, here is the claim you are entitled to make about RaftLite, and no more:
Further reading. Experience reports and primary sources, each with one line on why it is here:
- Diego Ongaro and John Ousterhout, In Search of an Understandable Consensus Algorithm, USENIX ATC 2014. Raft itself, with the safety argument RaftLite cuts down. Ongaro's own TLA+ spec is at github.com/ongardie/raft.tla.
- Chris Newcombe and others, Use of Formal Methods at Amazon Web Services, 2014. Engineers on several storage and coordination services report what TLA+ found and what it cost.
- A. Jesse Jiryu Davis, Max Hirschhorn and Judah Schvimer, eXtreme Modelling in Practice, PVLDB 2020. A replication protocol's spec checked against its implementation, and where that was hard.
- Finn Hackett, Joshua Rowe and Markus Alexander Kuppe, Understanding Inconsistency in Azure Cosmos DB with TLA+, ICSE-SEIP 2023. A spec written to explain a production system's consistency levels to the people who use it.
04In your editor
Check this lesson's exercises, and the models above, with:
./tla check 13-05Before you trust a pass on a protocol with terms, ask what the smallest bad behavior needs: how many leaders, how many entries, and in what order. If the model's bounds cannot hold that many, the pass says nothing about that bug.
Then ask it of your own protocol. What is its smallest bad behavior, and do your bounds hold it?
05Practice
Warm-up. State the claim Raft's safety argument rests on, LeaderCompleteness, and watch it fail on broken clusters.
Core. Commit is a placeholder. Write it, and the model the cfg's checks run on, and survive graders with more terms and more servers than yours.
Stretch. EagerCommitRaft passes with the chapter's bounds. Find the smallest bounds under which it does not, and say what each term in the trace contributes.
13-05-ex1Warm-up5 min
13-05-ex1 · The new leader has it all
Tier: warm-up. Time: about 5 minutes.
StateMachineSafety says no two committed entries clash. Raft's argument for it rests on a stronger claim about leaders: once an entry is committed, every later leader already holds it. Write that claim down.
- Run
./tla check 13-05-ex1. Every cfg passes: the starter'sLeaderCompletenessisTRUE, and TLC warns that it is a constant-level formula. - Fill gap g1 in
RaftLite.tla: a leader of the newest term holds every committed entry, at its index.committedholds pairs<< index, term >>. - Run the check.
RaftLite.cfgmust pass on the real protocol. The sevenMCRaftLitecfgs run broken clusters, and on each your claim must fail. The comment at the top of each cfg says what its cluster breaks.
Leave MCRaftLite.tla and its cfgs as they are. Before you fill the gap, decide why the claim is about a leader of the newest term, and not about every leader.
Hints: HINTS.md, under "13-05-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/13-distributed-systems/exercises/05-ex1-the-new-leader-has-it-all/; open it after you have a passing answer of your own.
Every cfg must meet its goal: "goal met: pass" for RaftLite.cfg, and "goal met: violation LeaderCompleteness" for the seven others. If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.
Done when: ./tla check 13-05-ex1 prints "8 of 8 checks met their expected outcome".
./tla check 13-05-ex1
Repo chapters/13-distributed-systems/exercises/05-ex1-the-new-leader-has-it-all/
Hints
Hint 1
Lesson 13-03 showed that an old leader can still believe it leads. Which leader must hold every committed entry, and which one cannot be expected to?
Hint 2
"A leader of the newest term" is a leader whose term is at least every server's term. "Holds every committed entry" means: for each pair in committed, its log is long enough, and has that term at that index.
Hint 3 (nearly the answer)
Quantify over servers l. The left side of an implication says l is a leader and no server's term is greater than l's. The right side quantifies over e \in committed and uses e[1] with Len(log[l]) and log[l][e[1]] with e[2].
13-05-ex2Core8 min
13-05-ex2 · Commit on a majority
Tier: core. Time: about 8 minutes.
RaftLite.tla replicates entries and never commits any. Commit(l, i) is a placeholder.
- Run
./tla check 13-05-ex2. The two graders pass with a coverage gap: with nothing committed,StateMachineSafetyhas nothing to compare. Your cfg stops with an error: a constant has no value. - Fill gap g1 in
RaftLite.tla. Leaderlmarks its firstientries committed, not only entryi. It needs a quorum of servers, each holdingl's entry at indexi. Entryiitself must be froml's own term: an older entry is committed only by committing a newer one after it. - Fill gap g1 in
RaftLite.cfg. Its checks are fixed below the gap; you write the model: the behavior spec, three servers,MaxTerm = 2,MaxLog = 2, and your deadlock setting, with a reason about the system. - Run the check until all three cfgs pass with full coverage and
CommittedAcrossTermsreached.
RaftLite.grader.cfg is the course's check of your Commit. It allows four terms, not two, so a Commit without the own-term condition can pass your cfg and still fail there; the stretch, 13-05-ex3, asks why the bigger model matters. It also checks CommittedIsPrefix, which a Commit that marks only entry i breaks. RaftLite.five.cfg runs five servers, where the leader and one follower are not a majority. Leave both as they are.
Two conditions are yours to add or leave out, and the graders pass either way: that l is still a leader, and that each quorum member is in l's term. The lesson's specs/RaftLite.tla has a Commit with both; write yours before you open it.
Hints: HINTS.md, under "13-05-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/13-distributed-systems/exercises/05-ex2-commit-on-a-majority/; open it after you have a passing answer of your own.
All three cfgs must meet their goal, "goal met: pass". If TLC finds no violation but an action never fires or a witness is not reached, ./tla prints "goal not met" and exits with code 1.
Done when: ./tla check 13-05-ex2 prints "goal met: pass".
./tla check 13-05-ex2
Repo chapters/13-distributed-systems/exercises/05-ex2-commit-on-a-majority/
Hints
Hint 1
Commit changes one variable. Which one, and by how much: one entry, or more?
Hint 2
Entry i must exist in l's log and be from l's own term, and some quorum must hold l's entry at index i. Then every entry of l's up to i is committed, not only entry i. A guard that l still leads is customary, and optional here.
Hint 3 (nearly the answer)
The quorum condition is \E Q \in Quorums followed by \A f \in Q over two facts about f: its log's length, and its entry at i. The update adds the pairs << k, log[l][k] >> for k in 1..i. The cfg's gap is the model: the spec, the constants, and the deadlock setting with its reason.
13-05-ex3Stretch35 min · mastery path
13-05-ex3 · How big is big enough?
Tier: stretch. Time: about 35 minutes.
A teammate removed one condition from Commit: the entry being committed no longer has to come from the leader's own term. "A majority holds it, so it is safe." They checked EagerCommitRaft.tla with the chapter's bounds, three servers, MaxTerm = 2 and MaxLog = 2, and TLC found no violation.
Your job is to find out whether that pass means anything.
- Run
./tla check 13-05-ex3. The teammate's bounds pass. - Fill gap g1 in
EagerCommitRaft.cfg. Keep three servers and chooseMaxTermandMaxLog. You want the smallest model in which TLC reports a violation ofStateMachineSafety. Before each run, write down what you expect and why. - Read the trace of the smallest model that fails. For each term in it, say what that term contributes. Why could the teammate's model never contain this behavior?
- In
NOTES.md, under 13-05, write the claim the teammate was entitled to make, in the course's form: "TLC found no violation of … in the model with …".
Do not change EagerCommitRaft.tla. Its missing condition is the subject, not the bug to fix.
Hints: HINTS.md, under "13-05-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/13-distributed-systems/exercises/05-ex3-how-big-is-big-enough/; open it after you have an answer of your own.
Done when: ./tla check 13-05-ex3 prints "goal met: violation StateMachineSafety".
./tla check 13-05-ex3
Repo chapters/13-distributed-systems/exercises/05-ex3-how-big-is-big-enough/
Hints
Hint 1
What could go wrong when a leader commits an older term's entry by counting copies? A later leader whose log has a different entry at that index must still be able to win an election.
Hint 2
Tell the story with leaders. One leader creates an entry and fails to spread it. A leader of a later term creates a different entry at the same index. The first one wins again, spreads its old entry to a majority and commits it. Then the owner of the competing entry wins once more. Count the terms.
Hint 3 (nearly the answer)
The whole story fits at a single index, so keep MaxLog small. Raise MaxTerm one step at a time, and note the first value at which TLC reports StateMachineSafety. Then read the trace term by term.
06Explain it
Open NOTES.md and answer the three prompts under 13-05, about the Core exercise.
What I built, and why it is correct:
Which assumption, if false, breaks it:
What TLC did not check here:Compare your answers with "13-05 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "Commit marks entries up to i committed once a quorum holds the leader's entry i, and only if that entry is from the leader's own term, so an older entry is committed only underneath a newer one. It relies on the up-to-date check in elections; without it a leader lacking a committed entry can win and overwrite it. TLC found no violation of LogMatching or StateMachineSafety with three servers, MaxTerm = 2 and MaxLog = 2, and the grader with four terms also passed; no messages, crashes or liveness were checked."
A weak answer: "A majority has the entry, so it is committed." It drops the own-term condition, which only a model with more terms can show is needed, and says nothing about what was checked.
Done?