Section 8.31

Idempotency

Definition

Idempotency is the property of an operation that applying it twice, or any number of times, has the same effect as applying it once. Setting a value to 10 is idempotent; adding 1 to it is not. The property refers to a chosen set of effects, such as stored values, log entries and messages sent; an operation can be idempotent for one set and not for a larger one. Idempotency lets a sender repeat a request whose outcome is unknown without causing a second, different effect.

The idea

A management server tells a device to install version 9. The device installs it and replies, and the reply is lost. The server cannot tell whether its request or the reply went missing, so it must either give up or send the request again. If the request means “be at version 9”, sending it again is harmless: a device already at version 9 has nothing more to do. If the request means “advance by one version”, a repeat can move the device two versions. Networks lose, delay and duplicate messages, and devices crash between doing something and recording that they did it, so every protocol that retries needs operations that can be repeated safely, or a way to recognize repeats.

A lift (elevator) call button is a fair picture. Pressing it once or ten times brings the lift once, so pressing it is idempotent. Putting coins into a vending machine is not: each coin counts. The picture also shows where the property stops. Pressing the button again after the lift has come and gone calls it a second time. Idempotency is about repeats that follow one another with nothing in between, not about a repeat that arrives after the state has moved on.

How it works

Name the effects. Decide which effects count: the main stored value only, or also log entries, counters, flash erase cycles, restarts and messages sent to other parties. Setting a configuration value to 10 leaves the value at 10 however often it is repeated, but if each repeat also appends a line to an audit log, the operation is not idempotent once the log counts.

Make operations absolute. An operation that names its result, such as “set the value to 10” or “install version 9”, is usually idempotent. An operation that names a change, such as “add 1” or “install the next version”, is not. A change can be made absolute by naming the state it expects as well: “replace key epoch 4 by key epoch 5” is applied only when the current epoch is 4, so a repeat finds epoch 5 and does nothing. This expected-state check is a compare-and-swap §9.9 on stored state, and the State transition guard §8.34 entry treats such checks in general.

Recognize repeats. An operation that is not idempotent, such as a payment, can still be made safe to retry. Each request carries a unique identifier chosen by the sender, and the receiver stores the identifier together with the result. A repeat with a known identifier gets the stored result back and is not executed again. The receiver must store the identifier atomically with the effect, in one commit: if it stores the effect and crashes before storing the identifier, the repeat is executed a second time. The receiver can keep identifiers only for a limited time, so a repeat older than that window is not recognized.

Reply so that a repeat is understood. Idempotency concerns effects, not replies. RFC 9110, which defines the meaning of HTTP (Hypertext Transfer Protocol) requests, calls a request method idempotent when the intended effect on the server of several identical requests is the same as the effect of one [517]. It names PUT, DELETE and the safe, read-only methods such as GET as idempotent; POST is not. A repeated DELETE may receive the reply 404 Not Found where the first one received a success, yet the server’s state is the same after both. In the same way, a device should answer a repeated “install version 9” with a reply that says “already at version 9”, so that the sender can count the retry as a success.

Recovery code. Crash recovery can itself be interrupted by a crash, so it must be idempotent. In write-ahead logging §8.12, recovery replays logged changes into the stored data, and a crash during recovery means the same changes are replayed again. Many database recovery methods make the replay idempotent by storing in each data page the sequence number of the last log record applied to it, and skipping any record whose number is not newer.

Idempotent processing combined with delivery that may repeat a message gives the effect of processing each request once, which the Exactly-once effect §8.32 entry treats.

Worked example

Set versus add. Start with the value 5. The operation “set to 10” gives 10, and repeating it gives 10 again. The operation “add 1” gives 6, and repeating it gives 7, which differs from the 6 that one application produced.

A larger set of effects. Count the length of an audit log as part of the state, and let “set to 10” append one log line each time. From the state (value 5, log length 0), one application gives (10, 1) and a second gives (10, 2). The value behaves idempotently; the operation with its log does not.

A version install repeated. A device runs version 8, and its version floor, the lowest version it may install or start, is 8. It receives “install version 9”, installs it and starts it on trial, and the new version confirms itself, which raises the floor to 9. The reply is lost and the server sends the request again. Three designs answer differently.

A delayed duplicate. Request aa says “set to 10”, and request bb, sent later, says “set to 20”. A copy of aa is delayed in the network and arrives after bb. The value ends at 10, although the latest request asked for 20. Each request is idempotent, yet the late repeat undid a newer change. A version number in each request, with the rule that a request older than the last one applied is refused, keeps the value at 20.

Stated precisely

Let SS be the set of states, where a state records every effect that counts, and let the operation be a function TT from SS to SS. The operation is idempotent if T(T(s))=T(s)for every s∈S.T(T(s)) = T(s) \quad \text{for every } s \in S. Applying TT once more to both sides, repeatedly, shows that every number n≥1n \ge 1 of applications gives T(s)T(s). In the example, “set to 10” gives T(5)=10T(5) = 10 and T(T(5))=T(10)=10T(T(5)) = T(10) = 10, so the two sides agree. “Add 1” gives T(5)=6T(5) = 6 but T(T(5))=T(6)=7≠6T(T(5)) = T(6) = 7 \ne 6.

The definition is a property of the function. It carries over to a real system only under three assumptions.

  1. Complete state: SS includes every effect anyone relies on, including logs, counters, messages sent and wear.

  2. No interleaving: no other operation runs between the repeats. If an operation UU runs in between, the result is T(U(T(s)))T(U(T(s))), which in general differs from U(T(s))U(T(s)), the state the repeat should have left alone, as the delayed duplicate shows.

  3. Crash completion: if a crash interrupts TT and leaves a partial state pp, the retry must still reach the intended result, T(p)=T(s)T(p) = T(s). Copying a complete image over a half-written copy meets this; appending a record after a half-written record does not.

Limits and common mistakes

Idempotent does not mean free of effects. An operation with no effect at all, such as a read, is called safe in HTTP terms, or nullipotent; an idempotent operation may change the state, but only the first time.

Idempotent does not mean authorized or fresh. An attacker who recorded a genuine “set to 10” can send it again later, after the value has been changed, and change it back. That is a replay attack §1.26, and an idempotent operation needs a freshness check against it just as any other operation does.

Idempotent does not mean that the replies are equal. A sender must be able to tell “done now”, “already done” and “refused” apart.

Two idempotent operations in sequence can fail to be idempotent together. Take a state of two numbers xx and yy. The operation “set xx to yy” is idempotent, and so is “set yy to x+1x + 1”. Applying both, in that order, to x=0x = 0 and y=0y = 0 gives x=0x = 0 and y=1y = 1; applying the pair again gives x=1x = 1 and y=2y = 2, a different state.

Recognizing repeats by request identifier works only as long as the identifiers are kept. A device with little storage keeps few, so it needs another defense against repeats that arrive later, such as a version number in every request.

Questions answered

Is applying the same version twice rejected, harmless, or state-changing?

It can be any of the three: the device’s acceptance rule decides, and a design must choose one on purpose and document it. Applying the same version twice usually happens when a reply is lost and the sender retries.

The safe choice is the harmless one, or a rejection whose reply says that the version is already in place. A reinstallation to repair a damaged copy should be a separate, explicit command, not a side effect of a repeated update request. Whatever the choice, it must survive a crash between installing the version and recording the installation: if the record is written last, a retry after the crash finds no record and installs again, so the installation and its record must be committed together.

Related entries: Exactly-once effect (§8.32), Retransmission and duplicate delivery (§7.36), Replay attack (§1.26), Write-ahead logging (§8.12), State transition guard (§8.34), Compare-and-swap (§9.9). Further reading: [517].