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.
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
- “A retry is a duplicate only if something remembers the first attempt. The design names the memory, its owner, and its lifetime: the key store, the server, the TTL.”
- The dedup store is a dependency on the critical path. Give it a timeout, a retry cap, and its own connection pool like any other: if it is down, fail closed rather than execute unprotected.
- Idempotence does not give ordering. Retries can arrive out of order; if order matters, that is a separate contract to state and enforce per key.
Follow-ups to expect
- Isn’t this exactly-once? No: delivery stays at-least-once; the effect becomes effectively-once. Exactly-once delivery is a claim nobody can keep.
- What if the client cannot send a key? Fall back to a canonical hash of the request, with a short dedup window. Weaker: it collides on two genuinely identical actions taken close together.
- What about concurrent retries racing? The unique constraint is the arbiter: one wins, the rest read the outcome.
Red flags in an answer
- “Our client does not retry, so we do not need this.” The broker does, the proxy does, the user does.
- Deduplication after the effect (“we check whether we already charged”).
- A TTL shorter than the retry window.
- A replay answered with success when the stored outcome was failure.
- Dedup trusted to the client alone.
Sources
- Idempotent requests, Stripe API Reference: the production design this answer describes, including the 24-hour result retention that answers how long a replay stays a duplicate for payment-shaped operations.
- The Idempotency-Key HTTP Header Field, IETF HTTP API Working Group draft: the same design being standardized, with the stored-response and error-replay semantics spelled out.