08-01
Chapter 08 · Messages in a world that loses them
The network is a set
00Recall
Two cards from 03-02, on why an invariant's trace stops at the bad state and why the course's traces are shortest ones, and one from 06-01 on what \E c \in Clients says about who runs next. Today the same \E decides which message arrives next.
03-02-a from Your first counterexample
Why does a TLC trace for an invariant violation stop at the bad state instead of showing a whole infinite behavior?
Show answer
Once a state breaks an invariant, nothing later can repair it, so the path to that state is all the evidence needed.
03-02-b from Your first counterexample
Why are the course's TLC traces always shortest ones?
Show answer
The course runs TLC with one worker, so the search is strictly breadth-first and the first violation found is at the smallest depth.
06-01-b from Productive failure
In a spec, what does Next == \E c \in Clients : Check(c) \/ Book(c) say about who runs next?
Show answer
Nothing fixed: every step is a step of some client, any client. So the behaviors include every interleaving of the clients' steps, and TLC visits every state any of them reaches.
Skip recall Recall is never required.
01Commit
A config service pushes numbered versions of one setting to a replica: version 1, then version 2 (N = 2). Each push puts a message into msgs, the set of messages in flight. The replica takes any message out of msgs and applies it:
Apply(v) is defined as
and v in msgs
and msgsprime = msgs set minus {v}
and currentprime = v
and UNCHANGED sent\ is set difference: msgs \ {v} is msgs without v. Nothing in specs/ConfigPush.tla mentions order, delay or reordering. The requirement, FinalIsLatest, says that once both versions are pushed and nothing is in flight, the replica holds version 2.
Commit a
TLC checks FinalIsLatest on ConfigPush with N = 2. What will it report?
- A violation: the replica ends up holding version 1.
- No violation: the versions go out in order, and nothing in the spec reorders them.
- An error: Apply could take either message, and TLC needs to know which one comes next.
Answer A violation: the replica ends up holding version 1.
- If you picked B
- The spec never says the messages stay in order either. A set has no "next": Apply may take any element, and TLC tries every one.
- If you picked C
- TLC does not need to know. When a step can go several ways, TLC explores every way.
Commit b
If TLC finds a violation, how many steps (not states) does its trace take?
Your answer is a whole number.
Answer 4
- Exactly right
- Right: two pushes and two applies, newest first. No shorter path ends with both versions applied in the wrong order.
- Within 10 %
- Within 10 %. Count the moves: every push is one step and every apply is one step.
- Under by more than half
- Under half. A bad end needs both versions pushed and both taken out of the network. Count the steps that requires.
- More than double
- More than double. The trace is one path, the shortest one, not a tour of every order.
- Anything else
- If you said one more than TLC's count, you counted states, not steps: the first state comes before any step. Otherwise, write the shortest bad run as a list of moves and count them.
02Lab
FinalIsLatest violated
This is a teaching view. Real checking happens with TLC on your machine.
Trace
- State 1 Initial state msgs = {} current = 0 sent = 0
- State 2 Push msgs {} becomes {1} sent 0 becomes 1
- State 3 Push msgs {1} becomes {1, 2} sent 1 becomes 2
- State 4 Apply(2) msgs {1, 2} becomes {1} current 0 becomes 2
- State 5 Apply(1) msgs {1} becomes {} current 2 becomes 1
Text view: every state and step
| State | sent | msgs | current | Steps (action → state) |
|---|---|---|---|---|
| initial 1 | 0 | {} | 0 | Push → 2 |
| 2 | 1 | {1} | 0 | Push → 3; Apply(1) → 4 |
| 3 | 2 | {1, 2} | 0 | Apply(1) → 5; Apply(2) → 6 |
| 4 | 1 | {} | 1 | Push → 5 |
| 5 | 2 | {2} | 1 | Apply(2) → 7 |
| 6 | 2 | {1} | 2 | Apply(1) → 8 |
| 7 | 2 | {} | 2 | |
| 8 | 2 | {} | 1 |
Step through the cards. At the first Apply, the network holds two versions. Before you go on, say which one you would have taken. Then see which one TLC took, and why that choice is the one that breaks the requirement.
Or in the terminal:
./tla check chapters/08-messages/specs/ConfigPush.latest.cfg08-messages/specs/ConfigPush.latest08-messages/specs/ConfigPush.latest ConfigPush.latest.cfg (N=2)
parse ok
model check: invariant FinalIsLatest violated; trace has 5 states (4 steps); 8 distinct states
expected outcome met: violation FinalIsLatest
trace (changed variables only; ./tla trace --full shows all):
1 Init sent = 0, msgs = {}, current = 0
2 Push sent: 0 -> 1, msgs: {} -> {1}
3 Push sent: 1 -> 2, msgs: {1} -> {1, 2}
4 Apply msgs: {1, 2} -> {1}, current: 0 -> 2
5 Apply msgs: {1} -> {}, current: 2 -> 1Reveal for commit a
FinalIsLatest is violated. The trace has 5 states. Both versions are pushed, then the replica takes version 2 and applies it, then takes version 1 and applies that. The network is empty and the replica holds the older version.
Reveal for commit b
The trace has 4 steps: two pushes, then two applies with the newer version first. Its 5 states include the initial one, which comes before any step.
03Reason
If you picked "no violation", you read the spec as a network diagram: an arrow from the service to the replica, and messages travelling along it in single file. The spec has no arrow and no file. It has a set.
Look at what a set is. {1, 2} and {2, 1} are the same set. There is no first element, no back of the line. When Apply(v) says v \in msgs, any element of msgs qualifies, and TLC tries each one. So the spec allows version 2 to be taken first. Nobody wrote a reorder action. Nobody needed to.
Write something before openinghow would you write a network that reorders messages, if you had to describe it as a program?
You would need a buffer, a rule for which message leaves next, and a way to make that rule arbitrary: a random index, a shuffle, a priority queue with random priorities. All of that is machinery for producing every order. A set skips the machinery. It states what may happen (any message in flight may arrive next) and leaves out how. TLC then enumerates every possibility, which is what the machinery was trying to imitate.
\E is read "there exists". The course quantifies over the constant set 1..N and puts v \in msgs inside Apply, the pattern every spec in this chapter uses.
Here is the trace again, as ./tla prints it:
- State 1 Initial state sent = 0 msgs = {} current = 0
- State 2 Push sent 0 becomes 1 msgs {} becomes {1}
- State 3 Push sent 1 becomes 2 msgs {1} becomes {1, 2}
- State 4 Apply msgs {1, 2} becomes {1} current 0 becomes 2
- State 5 Apply msgs {1} becomes {} current 2 becomes 1
Read the msgs column. After the two pushes it holds both versions. The first Apply takes version 2 out and sets current to 2. The second takes version 1 out and sets current back to 1. Nothing malfunctioned. Every step did what its action says.
Write something before openingTLC printed one shortest trace. Is there another trace with the same number of steps that also breaks FinalIsLatest?
No. To end with version 1 applied and nothing in flight, version 1 must be the last one applied, so version 2 was applied before it. Version 2 can be applied only after it is pushed, and it is pushed only after version 1. So both pushes come first, then version 2, then version 1. There is one way, and TLC found it.
So what is the model of a network, in this course? A variable holding the set of messages in flight. Sending is set union: msgs' = msgs \cup {m}. Receiving is choosing any element. In this lesson the receiver also takes the message out of the set, so each message arrives at most once. The next lesson asks what happens when it does not.
Two consequences follow, and the rest of the chapter needs both.
First, every protocol you specify this way is checked against every arrival order, for free. If a checked property depends on order, TLC finds an order that breaks it, as it did here. The replica's design relied on it: "apply whatever arrives" is correct only when what arrives last is newest.
Second, the fix has to live where the knowledge lives. The network cannot be told to behave, so whatever the fix is, it goes in the replica.
Write something before openingthe replica sees the number of every version it takes. What should it do when that number is lower than the one it holds? And should that message still leave the network?
It knows something the network does not: which of two versions is newer. So it can refuse to go backwards. Whether the stale message must still leave the network is the part people get wrong. Look at Finished and ask what happens to a message that nothing ever takes. The Core exercise has you write the answer in TLA+, and the hints say what TLC prints if you choose wrong.
A real system does the same with version numbers, timestamps from one clock, or sequence numbers, and each one comes with its own assumption. Version numbers assume one service hands them out in order; timestamps assume one clock, or clocks you trust to agree.
Write something before openingin the reveal, the replica briefly held version 2. Why doesn't TLC stop there?
FinalIsLatest is an implication. While a message is still in flight, its left side is false and the implication is true, whatever current holds. The claim is only about the moment when everything has arrived. TLC stops at the first state where the claim is false, and that is the state after the last Apply.
04In your editor
The spec is chapters/08-messages/specs/ConfigPush.tla, with ConfigPush.latest.cfg for the requirement and ConfigPush.cfg for the passing check of types and witnesses. Check this lesson's exercises with:
./tla check 08-01The command also runs both spec cfgs. The passing one prints:
08-messages/specs/ConfigPush08-messages/specs/ConfigPush ConfigPush.cfg (N=2)
parse ok
model check: no violation found in 8 distinct states (11 generated, depth 5)
expected outcome met: pass
coverage: all 3 actions fired
witness LatestApplied reached (trace of 4 states)
witness TwoInFlight reached (trace of 3 states)Its witness TwoInFlight is the evidence that reordering was possible at all: a network that never holds two messages cannot reorder them.
Write something before openingConfigPush.cfg reports its distinct states. Count them by hand from sent, msgs and current.
Group the states by sent. With nothing pushed there is one state. With version 1 pushed, it is in flight or applied: two states. With both pushed, the network holds both, or one of them while the replica holds the other, or neither while the replica holds either version. That is 1 + 2 + 5, and TLC agrees: 8 distinct states. Notice the state with the network empty and the replica on version 1: it is reachable, and it is the bad one.
05Practice
Warm-up. The push forgets to put the version into the network. Complete it and watch the replica wake up.
Core. Make the replica ignore stale versions, so that FinalIsLatest holds over a network that reorders. Keep the network as it is.
08-01-ex1Warm-up5 min
08-01-ex1 · Push into the network
Tier: warm-up. Time: about 5 minutes.
ConfigPush.tla again, with one line of Push missing: the version never goes into the network.
- Run
./tla check 08-01-ex1. Read the outcome: which action never fires, and why can it not? - Fill gap g1: the pushed version joins the set of versions in flight, next to whatever is already there. Delete the
\* GAPmarker on the line you change, or./tlareports the gap as unfilled. - Run the check. It must pass with every action firing and both witnesses reached:
LatestAppliedandTwoInFlight.
Before you run it, predict: is TwoInFlight reachable if your push replaces the network's contents instead of adding to them? Try it if you are not sure, then put the right answer back.
The check also runs MCConfigPushGrader.cfg, the course's own test of your push with three versions: no step may put any version into the network except the one being pushed. Leave that file and MCConfigPushGrader.tla as they are. Both cfgs must print "goal met: pass".
Hints: HINTS.md, under "08-01-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/01-ex1-push-into-the-network/; open it after you have a passing answer of your own.
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 08-01-ex1 prints "goal met: pass".
./tla check 08-01-ex1
Repo chapters/08-messages/exercises/01-ex1-push-into-the-network/
Hints
Hint 1
Apply needs a version in msgs. Look at what Push does to msgs in the starter, and you have the reason Apply never fires.
Hint 2
Sending is set union: the new set is the old set plus one element. \cup is read "union".
Hint 3 (nearly the answer)
Replace the right-hand side with the union of msgs and a one-element set holding the version being pushed. That version is one more than sent before the push.
08-01-ex2Core8 min
08-01-ex2 · Ignore stale versions
Tier: core. Time: about 8 minutes.
The replica from the lesson applies whatever arrives, and FinalIsLatest fails when version 2 overtakes version 1. Fix the replica, not the network: the set stays a set.
- Run
./tla check 08-01-ex2and read the trace. Which step should the replica have refused? - Change gap g1, and only the line that sets
current, so thatFinalIsLatestholds however the versions arrive. - Run the check until it passes with both witnesses,
LatestAppliedandAllDelivered.
A tempting fix adds a guard so that Apply is not possible for a stale version at all. Try it and read what TLC says. Then explain, in one sentence, why a stale message still has to be taken out of the network.
The check also runs MCConfigPushGrader.cfg, the course's own test of your replica with three versions: it holds only versions that were pushed and have arrived, it applies every version newer than the one it holds, and it never moves to an older version. Leave that file and MCConfigPushGrader.tla as they are. Both cfgs must print "goal met: pass".
Hints: HINTS.md, under "08-01-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/01-ex2-ignore-stale-versions/; open it after you have a passing answer of your own.
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 08-01-ex2 prints "goal met: pass".
./tla check 08-01-ex2
Repo chapters/08-messages/exercises/01-ex2-ignore-stale-versions/
Hints
Hint 1
Read the trace's last step. The replica takes version 1 while it already holds version 2. What should it do with a version older than its own?
Hint 2
The message leaves the network either way, so keep msgs' = msgs \ {v}. Only the new value of current depends on how v compares with current. A guard that forbids taking a stale version leaves it in the set forever, and the system can never finish: TLC reports that as a deadlock.
Hint 3 (nearly the answer)
Write current' with IF ... THEN ... ELSE ...: the new version if it is greater than current, otherwise current as it was.
08-mxMixed15 min · mastery path
08-mx · Whichever arrives
Tier: mixed. Time: about 15 minutes. Off the core clock: this is on the mastery path.
A sports app's server sends numbered score updates to a phone over a set network. A teammate wrote the phone's receive step with CHOOSE and says it "takes whichever update arrives". The requirement ShowsFinalScore passes with Updates = 3, and both witnesses are reached: all three updates can be in flight at once.
- Run
./tla check 08-mx. The teammate'sScoreFeed.cfgpasses, and so do two of the course's threeMCScoreFeedcfgs. The third,MCScoreFeed.any.cfg, fails: read its trace. - Read
Show. Does this model allow every arrival the network allows? - Rewrite gap g1 so that the phone may take any update in flight, as the teammate meant.
- Run the check again. The goal for
ScoreFeed.cfgis the counterexample: the phone ends on an old score.
You are not fixing the phone. You are making the model allow what the network allows, and the counterexample is the review comment.
The three MCScoreFeed cfgs are the course's check of your Show. MCScoreFeed.any.cfg checks two things. AnyUpdateCanArrive says that in every state, each update in flight can be the next one the phone takes. ShowsArrived says that every step of the phone takes an update that is in flight and removes it from the network, so the phone never shows an update that has not arrived. The other two each claim that a state is never reached, and only an out-of-order arrival, which a set network allows, reaches it: NeverMiddleFirst and NeverOldestLast. A phone that takes whichever update arrives meets the checks in MCScoreFeed.any.cfg and breaks the other two claims, so yours must. Leave those files as they are.
Hints: HINTS.md, under "08-mx hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/mx-score-feed/; open it after you have a passing answer of your own.
All four cfgs must meet their goals: "goal met: violation ShowsFinalScore" for ScoreFeed.cfg, "goal met: pass" for MCScoreFeed.any.cfg, and "goal met: violation NeverMiddleFirst" and "goal met: violation NeverOldestLast" for the other two.
Done when: ./tla check 08-mx prints "goal met: violation ShowsFinalScore".
./tla check 08-mx
Repo chapters/08-messages/exercises/mx-score-feed/
Hints
Hint 1
When updates 1, 2 and 3 are all in flight, which one does Show take? Does it take a different one in another behavior? Chapter 05 has what you need about CHOOSE.
Hint 2
To let any update be taken, the receive step needs "there exists an update in flight such that ...". That is \E, not CHOOSE.
Hint 3 (nearly the answer)
Replace the LET ... CHOOSE line with an existential quantifier over inFlight, and keep the two indented conjuncts under it. A different CHOOSE condition, or a choice between two CHOOSEs, still leaves some update in flight that can never be taken next, and MCScoreFeed.any.cfg says so.
06Explain it
Open NOTES.md and answer the three prompts under 08-01, 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 "08-01 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "My replica applies a version only if it is newer than the one it holds, and takes every version out of the network either way, so a late older version cannot overwrite a newer one, and the system still finishes. It assumes version numbers are assigned in push order by one service; with two services numbering independently, 'newer' would mean nothing. TLC checked FinalIsLatest with two versions, and the course's grader with three, over a network that reorders, with both witnesses reached; it did not check duplicates, more than three versions, or several replicas."
A weak answer: "I added an if statement so the version doesn't go backwards, and TLC passed." It says what was typed, not why it is right, and says nothing about the numbering assumption or what the pass covers.
Done?