08-02

Chapter 08 · Messages in a world that loses them

Loss and duplication for free

  • 43 min core
  • Essentials track
  • Skills: hostile-env, evidence

00Recall

One card from 08-01 on why a set reorders, one from 03-03 on what a check with no reachable states shows, and one from 05-02 on why an action updates a function with EXCEPT. Today's Credit is one of those updates.

08-01-a from The network is a set

Why does a set of messages in flight model a network that reorders, without any reorder action?

Show answer

A set has no order, and the receive step may take any element of it, so every arrival order is a behavior of the spec.

03-03-a from Break it on purpose

TLC reports no violation and zero distinct states. What has it shown about your invariants?

Show answer

Nothing. With no reachable states, every invariant holds vacuously.

05-02-b from Functions are total maps

Why does an action update a function with [f EXCEPT ![x] = v] and not with f'[x] = v?

Show answer

f'[x] = v only says what the new function is at x; it says nothing about the other points. EXCEPT names the whole new function: f everywhere, except v at x.

Skip recall Recall is never required.

01Commit

A gift-card app sends top-ups to a ledger, one credit each. specs/DepositLink.tla uses a set network, as in 08-01, with one difference: the ledger's receive step leaves the message in the set.

TLA+
Credit(d) is defined as
    and d in msgs
    and creditsprime = [credits EXCEPT ![d] = @ + 1]
    and UNCHANGED tuple of sent, msgs end tuple

credits[d] counts how often top-up d was credited; [credits EXCEPT ![d] = @ + 1] is the same function with one more at d (chapter 05). The requirement is NoDoubleCredit: no top-up is credited more than once. The spec has no action called Duplicate.

Commit a

TLC checks NoDoubleCredit on DepositLink with one top-up (N = 1). What will it report?

  1. No violation: nothing in the spec duplicates a message.
  2. Nothing: credits can grow forever, so TLC never finishes.
  3. A violation: the same top-up is credited twice.

Answer A violation: the same top-up is credited twice.

Commit b

Which faults can the set network in DepositLink produce, with no extra action written?

  1. Duplication only: that is the one thing leaving the message in the set does.
  2. Duplication, reordering and loss.
  3. Duplication and reordering, but losing a message needs a Lose action.

Answer Duplication, reordering and loss.

02Lab

There is no lab for this lesson; the trace is short enough to read in the terminal.

shell
./tla check chapters/08-messages/specs/DepositLink.cfg
Expected output 08-messages/specs/DepositLink
08-messages/specs/DepositLink  DepositLink.cfg  (N=1)
parse ok
model check: invariant NoDoubleCredit violated; trace has 4 states (3 steps); 4 distinct states
expected outcome met: violation NoDoubleCredit
trace (changed variables only; ./tla trace --full shows all):
  1  Init    sent = 0, msgs = {}, credits = <<0>>
  2  Send    sent: 0 -> 1, msgs: {} -> {1}
  3  Credit  credits: <<0>> -> <<1>>
  4  Credit  credits: <<1>> -> <<2>>

Reveal for commit a

NoDoubleCredit is violated in a trace of 4 states: the app sends the top-up, and the ledger credits it twice. Between the two credits, msgs does not change. The message was never taken out, so it was still there to be handed over again.

Reveal for commit b

All three. Duplication: the message stays in the set. Reordering: any message in the set may be taken next. Loss: nothing forces the ledger to take a message, so every behavior in which a top-up sits in the set forever is already a behavior of the spec.

03Reason

If you picked "no violation", you expected the network to misbehave only where the spec says so. A spec works the other way round. It lists what is allowed, and anything it does not forbid can happen. Credit does not take the message out of msgs, so the spec does not forbid taking it again. That is a duplicate.

A sender that hears nothing resends. A broker that crashes before recording a delivery redelivers. A load balancer retries on another node. The receiver cannot tell any of these from "the network sent it twice", so one model covers them all: the message stays available.

Now loss. You might expect to need an action like this one, from specs/LossyFairLink.tla:

TLA+chapters/08-messages/specs/LossyFairLink.tla
Lose is defined as
    and "hello" in msgs
    and msgsprime = msgs set minus {"hello"}
    and UNCHANGED tuple of sent, got end tuple
Write something before openingtake any behavior in which a Lose step removes a message. Is there a behavior without Lose that looks the same to the sender and the receiver?

Yes. Delete the Lose step and leave the message in the set. Every later step is still allowed, as long as no step of the sender or the receiver needs a message to be absent. None of the steps that change the sender's or the receiver's variables does; Finished, which waits for an empty network in some specs, only repeats the state. A timeout, where a spec needs one, is a step that may happen at any moment, not a test for an empty inbox. In every state, the sender's and the receiver's variables have the same values as before. Only msgs differs: it holds one extra message that nobody ever takes.

Adding Lose only adds states in which msgs is smaller, which matters for a requirement that mentions msgs. FinalIsLatest in 08-01 speaks only once every version is pushed and nothing is in flight. With an explicit Lose, the fixed replica can apply version 1, lose version 2, and end on version 1 with the network empty. Without Lose, version 2 stays in the set and the requirement never speaks. A requirement guarded by "nothing in flight" needs an explicit Lose to be tested against loss.

There is one place where this stops being true, and chapter 10 lives there. The question "must the message eventually arrive?" is not an invariant. Without any assumption, the answer is no, because the message may sit in the set forever. Chapter 10 adds fairness. Its simplest form, weak fairness, assumes that an action which stays enabled from some point on eventually takes a step. Under weak fairness on delivering each message (\A m : WF_vars(Deliver(m))), a message that stays in the set must eventually be taken, and loss is no longer free. Fairness on "some delivery" is not enough when delivered messages stay in the set or new ones keep arriving: some delivery is then always possible, so one message can wait forever while others are taken.

You do not need fairness yet. You need the warning: when you add fairness in chapter 10, check that you have not quietly made your network reliable.

Write something before openingTLC stopped after a few steps. But credits can grow forever, so the model is infinite. What would a passing run of this spec look like?

It would not finish. TLC explores level by level; in an infinite graph there is always another level. TLC found this violation at depth 4, and stopping there is why it finished. The Core exercise makes the ledger idempotent, and the model becomes finite as a side effect: each credits[d] stops at one.

Write something before openingthe app sent the top-up once, and the ledger credited it twice. Whose bug is it: the network's, the app's or the ledger's?

The ledger's. The network did nothing a real network does not do, and the app did nothing wrong. Duplicates are part of the environment. The receiver is the only party that sees every copy, so it is the only place where "this one again" can be recognised. That is the Core exercise, and 08-05 builds the same idea into a payment API.

When should a receive step take its message out of the set, then? When something in the real system makes a second delivery impossible, and you can name it. "We use TCP" is not enough on its own: TCP stops duplicate packets inside one connection, but a client that reconnects and resends, or a server that crashes after receiving and before acting, produces a second delivery that TCP never sees.

So a set network gives you three faults from one variable. Removal on receive is the one switch you control: take the message out, and you have said "this channel never duplicates".

04In your editor

The spec is chapters/08-messages/specs/DepositLink.tla; the fairness demonstration is FairLink.tla and LossyFairLink.tla in the same folder. Check this lesson's exercises with:

shell
./tla check 08-02

That command also runs every spec cfg this page mentions, including the three fairness runs. Those use PROPERTY and <> (read "eventually"), which chapter 09 introduces; read only their verdicts for now. The one this lesson starts from:

Output 08-messages/specs/DepositLink
08-messages/specs/DepositLink  DepositLink.cfg  (N=1)
parse ok
model check: invariant NoDoubleCredit violated; trace has 4 states (3 steps); 4 distinct states
expected outcome met: violation NoDoubleCredit
trace (changed variables only; ./tla trace --full shows all):
  1  Init    sent = 0, msgs = {}, credits = <<0>>
  2  Send    sent: 0 -> 1, msgs: {} -> {1}
  3  Credit  credits: <<0>> -> <<1>>
  4  Credit  credits: <<1>> -> <<2>>

05Practice

Warm-up. A ledger whose network hands over each top-up once and then forgets it. Make it honest: leave the message in the set, and watch the requirement fail.

Core. Keep the honest network and make the ledger credit each top-up once, however often it arrives.

08-02-ex1Warm-up5 min

08-02-ex1 · Let it stay

Tier: warm-up. Time: about 5 minutes.

This ledger takes each top-up out of the network when it credits it, so no top-up can arrive twice. It passes. Real networks do not promise that.

  1. Run ./tla check 08-02-ex1.
  2. Change gap g1 so that the message stays in the set when it is credited.
  3. Run the check and read the trace. The goal is the violation of NoDoubleCredit: you are making the model honest, not fixing the ledger.

Afterwards, look at Finished. It fired in the starter. Why can it never fire now?

Hints: HINTS.md, under "08-02-ex1 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/02-ex1-let-it-stay/; open it after you have a passing answer of your own.

Done when: ./tla check 08-02-ex1 prints "goal met: violation NoDoubleCredit".

./tla check 08-02-ex1

Repo chapters/08-messages/exercises/02-ex1-let-it-stay/

Hints

Hint 1

The gap's line takes the top-up out of the network. A network that may hand it over again does not do that.

Hint 2

Leaving a variable as it is can be written msgs' = msgs, or by adding it to the UNCHANGED tuple. Either is fine here, but the UNCHANGED sent line is outside the gap, so the first is simpler.

Hint 3 (nearly the answer)

Replace the line with one that says msgs keeps its value. Then Finished waits for msgs = {}, which can no longer happen once a top-up is sent.

08-02-ex2Core8 min

08-02-ex2 · Credit once

Tier: core. Time: about 8 minutes.

The lesson's ledger, with two top-ups (N = 2): the network may hand a top-up over again, and the ledger credits it again. Make the ledger idempotent: crediting a top-up twice must have the same effect as crediting it once.

  1. Run ./tla check 08-02-ex2 and read the trace for DepositLink.cfg, the first block.
  2. Change gap g1 so that Credit credits a top-up only the first time it arrives. IgnoreDuplicate, already written, handles every later copy. The ledger may read credits and the top-up it receives, not the app's sent.
  3. Run the check until it passes with the witness AllCredited and Send, Credit and IgnoreDuplicate all firing.

The check also runs MCDepositLinkGrader.cfg, the course's own test of your ledger with three top-ups: no top-up is credited twice, and a top-up not yet credited can always be credited when it arrives. A ledger that reads sent, or that credits only the newest top-up in flight, can strand an older one and still reach AllCredited along another order; this test catches that. It cannot see which variables Credit reads, so a read of sent that changes nothing passes, and is still not an answer. Leave that file and MCDepositLinkGrader.tla as they are. Both cfgs must print "goal met: pass".

Fix the ledger, not the network: the message must stay in the set. If you try a fix that takes it out, TLC reports a deadlock. Before you undo it, work out why: which state has no step left, and which action would have taken one?

Hints: HINTS.md, under "08-02-ex2 hint 1" to "hint 3". The solution is in solutions/chapters/08-messages/exercises/02-ex2-credit-once/; 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-02-ex2 prints "goal met: pass".

./tla check 08-02-ex2

Repo chapters/08-messages/exercises/02-ex2-credit-once/

Hints

Hint 1

credits[d] already records whether top-up d has been credited. What should be true of it before Credit(d) is allowed?

Hint 2

The gap is a guard: one conjunct with no prime in it. IgnoreDuplicate already handles the copies your guard turns away.

Hint 3 (nearly the answer)

Replace TRUE with a condition saying that credits[d] is still zero. Do not touch msgs.

06Explain it

Open NOTES.md and answer the three prompts under 08-02, about the Core exercise.

output
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-02 check your explanation" in HINTS.md.

After you writeCheck your explanation

A good answer: "Credit now requires that the top-up has not been credited, and IgnoreDuplicate takes every later copy without effect, so crediting is idempotent while the network keeps handing copies over. It relies on top-up IDs being unique and on the ledger remembering every ID it credited; if it forgot after a restart, a copy would be credited again. TLC checked two top-ups with duplication, reordering and free loss; it did not check that every top-up is eventually credited, which is a liveness question."

A weak answer: "I made the ledger skip duplicates, so there is no double credit." It does not say how duplicates are recognised, what that depends on, or what the check left out.

Done?