Q6 · How do you design idempotent operations for systems with retries?

Back to 24 Architecture Questions.

The question is not “make retries safe”. The axis it turns on: where the duplicate is detected, and how long a replay stays a duplicate. Retries make duplicates a certainty, not a risk: the broker redelivers, the proxy re-issues, the user clicks twice. An operation is idempotent when applying it twice leaves the same state as applying it once, and that property has to be designed, not hoped for.

Where the duplicate dies A client retries with the same idempotency key. The API consults a deduplication store that sits before the payment side effect: a first visit executes once and stores the response, a replay inside the TTL returns the stored response without executing again. FIGURE 6 WHERE THE DUPLICATE DIES DETECTION POINT Client retry after timeout API reads the key Dedup Store key → result, TTL Payment charged once replay, same idempotency key first visit: execute once, store the response under the key replay inside the TTL: stored response, no second execution

What the answer should contain

Idempotence is a property of the effect, not the request. “We send it only once” is a client promise the server cannot verify. The server must assume every request is a potential replay, because the broker, the proxy, and the user all retry without telling it.

Client supplies the key; the server owns the store. The client generates one key per operation (not per session) and sends it on every attempt. The server keeps key → outcome, with a TTL, and the duplicate check happens at the detection point: before the side effect, atomically with it. The cheapest correct implementation is a unique-constraint insert: the first insert wins and executes, the loser reads the stored outcome. No lock dance.

Store the outcome, not just a seen-flag. A replay must receive the same response as the original attempt, including the original failure. Returning “success” to a replay whose stored outcome was an error is worse than the duplicate: now the caller believes a charge happened that never did.

Know the two kinds of idempotence. Natural: state-setting operations, where the last write wins and order does not matter (PUT the full balance, delete by stable ID). Enforced: key-based, for effect-appending operations (create, charge, transfer). “Add $10” can never be made naturally idempotent; “set balance to $110 under key K” can.

The TTL answers “how long a replay stays a duplicate”. It must cover the full retry window: your retry policy with backoff, broker redelivery, and the delayed replays that networks produce under stress. Generous by default: 24 hours or more for payment-shaped operations. After expiry, a replay is a new operation and is executed again.

Ideas worth mentioning out loud

Follow-ups to expect

Red flags in an answer

Sources