08-03
Chapter 08 · Messages in a world that loses them
Queues versus sets
00Recall
One card from 08-02 on how a set network duplicates, one from 08-01 on the stale-version fix, and one from 03-04 on generated versus distinct states. You will count states by hand in a few minutes.
08-02-a from Loss and duplication for free
In a spec whose network is a set, how do you allow duplicate delivery?
Show answer
Leave the message in the set when it is received. Nothing then forbids receiving it again.
08-01-b from The network is a set
Over a reordering network, what did the replica need in order to end with the newest version?
Show answer
It compares versions and drops a message older than what it holds, while still taking that message out of the network.
03-04-a from Reading the numbers
TLC reports more states generated than distinct states. Did it count something twice by mistake?
Show answer
No. Generated counts every successor it computed, including repeats; distinct counts each state once.
Skip recall Recall is never required.
01Commit
A monitor sends the same message, "ping", twice (Pings = 2). A server takes pings out of the network and counts them. The network is a set, as in 08-01:
Send is defined as
and sent < Pings
and sentprime = sent + 1
and msgsprime = msgs union {"ping"}
and UNCHANGED receivedThe claim NoVanishing says that every ping sent is either counted or still in flight: sent = received + Cardinality(msgs). Cardinality(S) is the number of elements of the set S (module FiniteSets).
Commit a
TLC checks NoVanishing on specs/PingSet.tla. What will it report?
- A violation, right after the second send.
- No violation: each ping is either in flight or counted.
- A violation, but only after the server has counted a ping.
Answer A violation, right after the second send.
- If you picked B
- Count the elements of msgs after two sends. The second send adds "ping" to a set that already holds "ping", and a set cannot hold it twice.
- If you picked C
- The claim breaks before the server does anything. Look at what the second send does to msgs, and compare sent with Cardinality(msgs).
The same spec with a bag instead of a set, specs/PingBag.tla, passes. A bag, or multiset, is a collection that remembers how many copies of each element it holds (module Bags).
Commit b
How many distinct states does TLC find in PingBag with Pings = 2? Count them by hand from sent, the copies in flight, and received.
Your answer is a whole number.
Answer 6
- Exactly right
- Right. The variables are tied together by NoVanishing, so for each value of sent, the split between in flight and counted is all that varies.
- Within 10 %
- Within 10 %. List the states by the value of sent: zero, one and two pings sent. For each, how can the sent pings be split between the bag and the counter?
- Under by more than half
- Under half. Two is the number of sends, and sends are steps, not states. Even before anything is sent there is one state, and one ping sent already gives two more. Keep counting through two pings sent.
- More than double
- More than double. You probably counted every combination of the three variables. Most combinations break NoVanishing, and no action reaches them.
- Anything else
- If you counted one state per value of sent, look again at one ping sent: it may be in flight or already counted, two different states, and two pings sent split in more ways still. Otherwise, list the states by the value of sent; for each value, every split of the pings between the bag and the counter is one state.
02Lab
There is no lab for this lesson. Run both specs and compare:
./tla check chapters/08-messages/specs/PingSet.cfg
./tla check chapters/08-messages/specs/PingBag.cfg08-messages/specs/PingSet08-messages/specs/PingSet PingSet.cfg (Pings=2)
parse ok
model check: invariant NoVanishing violated; trace has 3 states (2 steps); 3 distinct states
expected outcome met: violation NoVanishing
trace (changed variables only; ./tla trace --full shows all):
1 Init sent = 0, msgs = {}, received = 0
2 Send sent: 0 -> 1, msgs: {} -> {"ping"}
3 Send sent: 1 -> 2Reveal for commit a
NoVanishing is violated in a trace of 3 states, two sends and nothing else. In the last state sent equals Pings and msgs is still {"ping"}. ./tla shows only sent changing in the second step, because msgs did not change. That unchanged line is the second ping disappearing.
Reveal for commit b
TLC finds 6 distinct states. With no pings sent there is one state. With one sent, the ping is in the bag or counted. With two sent, both are in the bag, one is in each place, or both are counted. That is 1 + 2 + 3.
03Reason
If you picked "no violation", you trusted the word "send". In your head, sending twice makes two messages. In the spec, sending is msgs \cup {"ping"}, and a set either contains "ping" or it does not. The second send changed nothing about the network. A message vanished, and no action for loss was involved. This is the set's blind spot: it cannot tell one copy from two.
The blind spot is harmless when messages carry something unique: a version, a sequence number, an ID. {1, 2} holds two messages because they differ. The set only bites when two messages that should count separately are equal as values. Then you have three choices: make them differ (add an ID), count copies (a bag), or keep an order (a sequence).
Write something before openinggive each ping a number, so the monitor sends ping 1 and ping 2. Which container does the spec need now?
A set is enough again. {1, 2} has two elements, so the second send adds one, and NoVanishing holds with Cardinality. The problem was never the set; it was two messages that were equal as values but meant to count twice. When you model messages that carry an ID, keep the ID even if your spec never reads it. It is what keeps the set from merging them.
Each container makes some things expressible and others impossible:
| Network as | Reorders | Keeps copies apart | Keeps order | Duplicates |
|---|---|---|---|---|
| set | any order | no | no | if receive leaves the message |
| bag | any order | yes | no | if receive leaves the copy |
| sequence, read from the front | no | yes | yes | only with an explicit action |
| sequence, read anywhere | yes | yes | no | only with an explicit action |
Read the last two rows twice. A sequence is not a promise of order. Reading from the front is. The data structure only stores; the actions decide what the network may do with what it stores. That is why the Core exercise changes one guard in Apply and nothing in the variables, and why reading a spec's variable declarations alone never tells you which faults it models. specs/ConfigPushQueue.tla is 08-01's spec with the set replaced by a sequence, read with Head and Tail, and there FinalIsLatest holds:
Apply is defined as
and chan not equal to tuple of end tuple
and currentprime = Head(chan)
and chanprime = Tail(chan)
and UNCHANGED sentTLC finds no violation in 6 distinct states, against 8 for the set version. The difference is exactly the reordered states. The set version can reach a state with the older version still in flight while the replica holds the newer one, and a state with the network empty and the replica on the older one. The queue cannot reach either, because the older version always leaves first.
Write something before openingthe queue made FinalIsLatest pass with no change to the replica. So is the queue the better model?
It is the better model only of a network that really keeps order. That is a fact about the deployment, not about TLA+. One TCP connection keeps order. Two connections, a load balancer in front of several servers, a retry over a new connection, or a broker with several partitions do not. If you choose a queue for a network that reorders, TLC checks a world you do not live in, and a passing check tells you nothing about the real one.
A bag has its own blind spot: order. It remembers that two pings are in flight, but not which was sent first, so it reorders as freely as a set. If you need copies and order together, you need a sequence.
Which one should you reach for? Start with a set. It gives you reordering, and with removal under your control, duplication and loss, for one variable and very little text. Give messages identities so that the set never merges two you care about. Move to a bag when equal messages must count separately and you cannot give them IDs. Use a sequence when the transport really keeps order and your design depends on it, and write down why you believe it. That sentence is part of the spec.
Write something before openingwith a queue, how would you model a network that may also lose a message? And duplicate one?
A queue read from the front can only hand over messages in order, and cannot skip one. Losing a message while later ones still get through needs an explicit action that removes it from the middle of the queue: chan' = Drop(chan, i) for some position i, with a Drop operator you define (see specs/AlternatingBit.tla). So does duplication: an action that inserts a copy. Losing everything from some point on is still free, as it was in 08-02: the receiver stops taking messages. The Stretch exercise builds exactly that channel, and 08-04 runs a protocol over it.
04In your editor
The specs are chapters/08-messages/specs/PingSet.tla, PingBag.tla and ConfigPushQueue.tla. Check this lesson's exercises with:
./tla check 08-03The bag version prints:
08-messages/specs/PingBag08-messages/specs/PingBag PingBag.cfg (Pings=2)
parse ok
model check: no violation found in 6 distinct states (8 generated, depth 5)
expected outcome met: pass
coverage: all 3 actions fired
witness TwoCopies reached (trace of 3 states)
witness AllReceived reached (trace of 5 states)Both of its witnesses matter. TwoCopies shows the bag really held two pings at once, the case the set could not express. AllReceived shows they were both counted, so the pass is not the server doing nothing. The Stretch, if you take it, builds the channel that 08-04's protocol runs over; its two witnesses play the same role for a loss and a duplicate.
05Practice
Warm-up. The bag version's receive step forgets to remove a copy. Complete it with bag subtraction.
Core. A teammate's "queue" lets the replica take any version in it. Make it hand over the front one, and see FinalIsLatest hold.
Stretch. From a skeleton, write a channel that keeps order but may lose and duplicate, including the cfg. 08-04 needs exactly this channel.
08-03-ex1Warm-up5 min
08-03-ex1 · Count the copies
Tier: warm-up. Time: about 5 minutes.
PingBag.tla, with the server's receive step unfinished: it counts a ping but leaves the bag as it was.
- Run
./tla check 08-03-ex1and read the trace. Note how TLC prints a bag. - Fill gap g1: take exactly one copy of
"ping"out of the bag. The module's comments show the two bag operators you need. - Run the check until it passes with both witnesses:
TwoCopies(two pings in flight at once) andAllReceived.
One wrong answer empties the bag in one go. Before you run it, predict which claim catches it, and after how many steps.
Hints: HINTS.md, under "08-03-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/03-ex1-count-the-copies/; 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-03-ex1 prints "goal met: pass".
./tla check 08-03-ex1
Repo chapters/08-messages/exercises/03-ex1-count-the-copies/
Hints
Hint 1
The trace shows received going up while the bag keeps its copy. The receive step must change msgs.
Hint 2
SetToBag({"ping"}) is a bag with one copy of "ping". Send adds it with (+). Receiving does the opposite.
Hint 3 (nearly the answer)
Set msgs' to msgs with the one-copy bag subtracted, using (-). Subtracting msgs from itself, or using EmptyBag, removes every copy and breaks NoVanishing.
08-03-ex2Core8 min
08-03-ex2 · Head of the line
Tier: core. Time: about 8 minutes.
A teammate replaced 08-01's set with a sequence and wrote in the pull request: "a queue, so the replica sees versions in order." FinalIsLatest still fails.
- Run
./tla check 08-03-ex2. Read the trace forConfigPushQueue.cfg, the first block: from which position ofchandoes eachApplytake its version? - Change gap g1 so that
Applytakes only the version at the front of the sequence. - Run the check until it passes with both witnesses,
LatestAppliedandAllDelivered. If./tlasays TLC threw an unexpected exception, read the raw log it names: TLC's own explanation is there.
The check also runs MCConfigPushQueueGrader.cfg, the course's own test of your Apply with three versions: every step that takes a version out of the channel takes the one at the front, and the replica applies that version. A replica that skips older versions, as in 08-01-ex2, also makes FinalIsLatest pass, but the sequence still reorders, and this test rejects it: the fix this exercise asks for belongs in the queue. Leave that file and MCConfigPushQueueGrader.tla as they are. Both cfgs must print "goal met: pass".
Then write two sentences for the pull request: what made the teammate's sequence reorder, and what your version assumes about the real network.
Hints: HINTS.md, under "08-03-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/03-ex2-head-of-the-line/; 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-03-ex2 prints "goal met: pass".
./tla check 08-03-ex2
Repo chapters/08-messages/exercises/03-ex2-head-of-the-line/
Hints
Hint 1
In the trace, the first Apply takes the version at position 2 of chan. Which position does a queue hand over?
Hint 2
The parameter i is the position taken. Restrict it to the front. The sequence must also be non-empty, or chan[1] has no value.
Hint 3 (nearly the answer)
Replace the placeholder with two conjuncts: i equals the first position, and chan is not the empty sequence. Nothing else in Apply needs to change.
08-03-ex3Stretch40 min · mastery path
08-03-ex3 · A channel that keeps order
Tier: stretch. Time: about 40 minutes. Off the core clock: this is on the mastery path.
A blank page, almost. LossyFifo.tla names the constants, the variables, the actions and two witnesses; every body is a placeholder. You write the rest, including the cfg.
The channel:
- The sender sends messages 1 to
Nin order, each onto the back of the channel, while the channel holds fewer thanCapmessages. - The receiver takes the message at the front and records it in
got. - The channel may lose any message in it.
- The channel may duplicate any message in place, while there is room and fewer than
MaxDupsduplications have happened. - The requirements:
gotnever goes down (OrderKept), and holds only messages that were sent (OnlySent).
Steps:
- Run
./tla check 08-03-ex3. Read the starter error. - Fill gaps g1 to g8 in
LossyFifo.tla, then gap g1 inLossyFifo.cfg: the spec, the constants, and every invariant worth checking. - Run the check until it passes with full coverage and both witnesses:
SawDuplicateandSawGap. - Break
OrderKepton purpose: let the receiver take any message, not only the front one, and confirm TLC catches it. Put it back. Then, from the repository root, run./tla mutate chapters/08-messages/exercises/03-ex3-lossy-fifo-channel/LossyFifo.tlaand decide, for each survivor, whether it is a gap in your properties or a change that does not matter. A survivor that drops a disjunct ofNextremoves behaviors and adds none, so no invariant can kill it; your coverage check does.
The check also runs four cfgs the course wrote. MCLossyFifoAnyOrder.cfg and MCLossyFifoInvent.cfg test your two requirements on broken channels: one whose receiver may take a message out of order after the first, and one whose receiver records a message before it is sent. Your OrderKept must fail on the first and your OnlySent on the second. A requirement that passes there says less than the sentence above it. MCLossyFifoChannel.cfg tests your channel: it never holds more than Cap messages, it hands messages over in the order they were sent (GotInOrder, the course's own copy of the order requirement, checked whatever your cfg names), and it can lose a message from behind another (witness LostBehind). MCLossyFifoChannel.cap3.cfg runs the same checks with Cap = 3, where a copy made in place and a copy put on the back of the channel lead to different records. Leave those seven files as they are.
Hints: HINTS.md, under "08-03-ex3 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/03-ex3-lossy-fifo-channel/; 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.
All five cfgs must meet their goals: "goal met: pass" for LossyFifo.cfg and both MCLossyFifoChannel cfgs, "goal met: violation OrderKept" for MCLossyFifoAnyOrder.cfg, and "goal met: violation OnlySent" for MCLossyFifoInvent.cfg.
Done when: ./tla check 08-03-ex3 prints "goal met: pass".
./tla check 08-03-ex3
Repo chapters/08-messages/exercises/03-ex3-lossy-fifo-channel/
Hints
Hint 1
Write TypeOK and Init first, then one action at a time, running the check after each. A starter error tells you which name TLC tripped over.
Hint 2
Drop and Double are written for you. Lose(i) and Duplicate(i) need i to be a real position in chan; Duplicate also needs room and an unused duplication. Every action must say what happens to all four variables.
Hint 3 (nearly the answer)
OrderKept compares each element of got with the next one, for every position except the last. OnlySent compares each element with sent. The cfg needs SPECIFICATION, the three constants, and an INVARIANT line for TypeOK and for each requirement.
06Explain it
Open NOTES.md and answer the three prompts under 08-03, 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-03 check your explanation" in HINTS.md.
After you writeCheck your explanation
A good answer: "My Apply takes only position 1 of chan, so the replica sees versions in the order they were pushed, and FinalIsLatest holds without any change to the replica. That is only as true as the assumption that the real network keeps order: one connection would; a load balancer or retries over new connections would not. TLC checked two versions over a queue with no loss or duplication; it did not check whether our transport keeps order, which no run can."
A weak answer: "The queue now works like a queue, so the bug is fixed." It hides the assumption: the fix moved the burden of keeping order from the replica to the network and did not justify it.
Done?