Section 8.32

Exactly-once effect

Definition

An exactly-once effect means that an accepted request changes the receiver’s state exactly once across retries. This assumes a copy eventually reaches a receiver that can commit it. The sender retries using the same identifier. The receiver records the identifier and the change together in storage that survives a reset. If a reply is lost, another copy with that identifier gets the recorded outcome. The sender can still be unsure what happened until a reply arrives.

The idea

A management tool asks a device to “add 1 to the stored counter” and waits for a reply. No reply comes. Three histories end in the same silence: the request was lost; the device carried it out and the reply was lost; or the device carried it out, stored the new counter and reset before replying. The tool cannot tell which one happened. If it gives up, the change may never have happened. If it builds the request again and sends it, the counter may rise by 2.

Count arrivals at the receiver separately from changes to its state. At-most-once execution means that one request causes at most one change. The sender can still retry if the receiver recognizes a duplicate and suppresses its effect. At-least-once delivery means that some copy eventually arrives, assuming the sender keeps trying and the channel eventually delivers one. The receiver can see several copies. For example, if the first acknowledgment is lost, the sender may send request 42 again. An exactly-once effect combines eventual completion of an accepted request with protection against a second change for that identifier.

A numbered bank check is a fair picture. A bank that records each check number it has paid can pay check 1042 once and refuse a copy of it, so a payer whose check seems lost in the post can send a copy without fear. The picture stops at one point: the bank writes the payment and the note “1042 paid” into one ledger in one step. A device must itself arrange that the change and the record of its identifier are stored together.

How it works

Each step must stay correct if either side resets at any moment.

  1. Name the operation. The sender gives the operation an identifier that it never gives to any other operation, for example a sequence number that increases by one per operation. The same identifier always travels with the same content. Where an attacker can send messages, one authentication §1.6 tag, a short value computed over the bytes with a secret key, covers the identifier and the content together.

  2. Record the intent before the first send. The sender writes the exact request bytes, identifier included, to its own persistent storage, which keeps its content through a reset or a loss of power. This stored copy is called an intent journal: a note of a request that is under way. If the sender crashes, it reads the journal after restarting and resends the identical bytes. It must not build a new request with a new identifier: the receiver may already have carried out the first one, and a new identifier would make the same change look like a second operation.

  3. Retry until confirmed. After each timeout §9.34 without an acknowledgment naming the identifier, the sender resends the same bytes.

  4. Check the identifier at the receiver. The receiver looks the identifier up in its deduplication record, the stored list of completed operations with their outcomes. If the identifier is there, the receiver leaves its state alone and resends the stored outcome. Otherwise this copy may proceed only if it wins the right to commit that identifier. Two copies arriving together must not both pass the test and apply the effect.

  5. Commit the effect and the identifier together. The receiver stores the changed state and the entry “identifier done, with this outcome” in one atomic commit, after which both are stored or neither is. A transaction §8.7 (a group of writes that takes effect entirely or not at all), write-ahead logging §8.12 (recording the intended writes in a log before making them), or a single record that holds both values provides that step.

  6. Acknowledge after the commit. The acknowledgment carries the identifier and the outcome. Sent before the commit, it could announce a change that a reset then wipes out.

  7. Close the journal. On receiving the acknowledgment, the sender deletes its journal entry.

Step 5 is the heart of the construction; the worked example shows what goes wrong when the effect and the identifier are stored by two separate writes. When one sender issues operations one at a time with sequence numbers that increase by one, the deduplication record can shrink to one stored number per sender, the highest sequence number completed, with its outcome.

Security protocols add a related rule. To defeat a replay attack §1.26, in which an attacker records a valid message and sends it again later, the receiver stores a freshness value, such as a monotonic counter §8.28 (a stored number that only ever increases) or a nonce §5.18 (a number used once), and rejects any message whose value it has already seen. Some designs store the new freshness value before applying the change, an order called nonce-first persistence. A reset between the two steps loses the change, and a resend of the same bytes is then rejected as a replay. Nothing is applied twice, but the result is an at-most-once effect: the sender must ask for the current state to learn the outcome, and must issue a new request with a new freshness value if the change was lost. Storing the freshness value, the change and the outcome in one commit restores the exactly-once effect: a resend is recognized as a completed operation and answered with its stored outcome, without changing the state.

The same reasoning applies when the device holds data that a reader collects, such as a queue of measurement records. Deleting a record as soon as it is read goes wrong in two ways. If the reply is lost, the record is gone. If the reader retries the read, or reads a large record in several fragments, each extra read deletes another record, so records are skipped. The peek-and-acknowledge pattern avoids both. Each record carries a sequence number that increases by one. A read, called a peek, returns the oldest record and leaves it in place, so a repeated or fragmented read returns the same record. The device deletes the record only when the reader, having stored it, sends an acknowledgment naming its sequence number; an acknowledgment for a record already deleted changes nothing. A gap in the sequence numbers reveals a lost record: if the reader holds record 41 and next receives 43, record 42 was lost, for example because the queue was full.

Worked example

A device holds a counter with the value 5. Operation 42 says “add 1 to the counter”. The correct final value is 6 whatever happens. Compare three ways of storing the work, each with a reset at the worst moment followed by one resend of request 42.

Now count the messages in one run of the third design. The sender sends request 42 three times. The first copy arrives and is committed, but its acknowledgment is lost. The second copy is lost on the way. The third copy arrives, is recognized as a duplicate, and its acknowledgment arrives. Sends: 3. Deliveries: 2. Acknowledgments received: 1. Effects: 1. Only the effect count must equal 1; the others depend on the channel.

Stated precisely

For an operation with identifier ii, let DiD_i be the number of times the receiver receives it and EiE_i the number of times the receiver applies its effect. An exactly-once effect is this contract: the receiver decides operation ii once, at the first attempt that commits, to accept it or to refuse it, and stores that decision with its outcome. If the decision is to accept, then Ei=1E_i = 1, however large DiD_i is; if it is to refuse, then Ei=0E_i = 0. Every later delivery of ii is answered with the stored outcome and changes nothing. If Di=0D_i = 0, then Ei=0E_i = 0.

The contract rests on four assumptions.

  1. Unique, bound identifiers: the sender never gives one identifier to two different requests, and where an attacker can send messages, the identifier and the content are authenticated together.

  2. One conditional commit per identifier: the check that the identifier is new, the effect, and its deduplication entry form a single transaction that excludes a second winner. The acknowledgment follows durable commit.

  3. Retention: the receiver keeps the entry for ii for as long as a copy of request ii can still arrive.

  4. No rollback: the receiver’s storage is never returned to older contents.

Those four give the safety property §1.11, a promise that something bad never happens: no operation takes effect twice, and no accepted operation is lost. The liveness property §1.12, a promise that something good eventually happens, here that the sender learns the outcome, needs two more assumptions: the sender’s intent journal survives the sender’s own crash, and the channel delivers some copy and some acknowledgment after a finite number of attempts. Without the last one the sender may wait forever, and safety still holds.

Limits and common mistakes

A local commit cannot include an effect outside the device’s own storage. If the operation opens a valve or sends a message to a third system, and the device resets after acting but before committing, the resend acts again. The outside system must recognize the identifier itself, or the action must be harmless to repeat (see the idempotency §8.31 entry), or the device must commit before acting and accept that a reset can lose the action.

The deduplication record is finite. An entry that is evicted to make room, or that expires, turns a late copy of its request into a new operation. Keep the entry as long as a copy can still arrive. With one outstanding request per sender, consecutive sequence numbers, and acknowledgments before advancing, a stored maximum and its outcome cover the current retry. If independent requests can arrive out of order, recording 42 as the maximum does not tell the receiver whether it has ever processed 41.

Restoring the receiver’s storage from an older backup also restores an older deduplication record, and requests completed since then will be applied again.

An identifier that is not bound to its content lets two different requests share it, and the second is silently dropped as a duplicate. Storing a hash of the content (a short fingerprint computed from its bytes) beside the identifier lets the receiver detect the mismatch and refuse the second request openly.

Questions answered

Can a reset occur after commit but before the acknowledgment is received?

Yes. The commit is a write to the device’s persistent storage, and the acknowledgment is a separate message sent afterwards; a reset of the device, or the loss of the acknowledgment on the way, can fall between the two. The sender then sees only silence, which looks exactly like a request that never arrived. The protocol must therefore treat the outcome as unknown, not as failed. The sender resends the identical request with the same operation identifier. Because the device stored the effect and the record of that identifier in one atomic commit, it recognizes the resend as a duplicate, does not apply it again, and answers with the stored outcome. A timeout on its own proves neither that the effect happened nor that it did not.

Related entries: Idempotency (§8.31), Transaction (§8.7), Retransmission and duplicate delivery (§7.36), Replay attack (§1.26), Monotonic counter (§8.28), Nonce (§5.18).