Idempotency as an end-to-end property
Tier 1 gave you the idempotency key as an API design detail. Lesson 6.04 said every consumer must be idempotent. Both are true and both are fragments, because idempotency is not a feature you add at a layer. It is a property of the whole path from the user's tap to the last side effect, and the weakest hop sets the value for the entire chain.
Count the places a duplicate can be born
tap ─▶ client ─▶ gateway ─▶ service ─▶ queue ─▶ worker ─▶ vendor
retry retry retry redeliver retry retry
Six independent retry mechanisms, each written by different people, most of them enabled by default and invisible in code. And here is the property that makes this hard: a retry at one layer is, at the next layer down, indistinguishable from a genuine new request. The service cannot tell whether the gateway is retrying or the user pressed the button twice. It has no information that would let it tell.
Which gives the rule that most designs get wrong:
The deduplication identity must be minted once, by the originator, and propagated unchanged through every hop.
If the gateway generates a fresh id when it retries, the service sees two distinct requests and there is no recovery — the information that they were the same was destroyed one layer up. This is exactly the request-id propagation Tier 9 asks for in order to trace a request, except here the propagation is doing correctness work rather than debugging work. Same plumbing, much higher stakes.
Three ways to be idempotent, cheapest first
- Make the operation naturally idempotent. Assignment is idempotent; increment is not.
SET status = 'shipped'applied twice is applied once.balance = balance - 100applied twice is a bug with a customer attached. Where you control the operation's shape, choose the one that does not need protecting — it costs no storage, no expiry policy, and cannot be got wrong later. - Let a unique constraint do it.
UNIQUE(order_id, attempt), and the second insert fails. The database's atomicity is doing the deduplication for you: no separate store, no expiry, no race. Whenever the operation has a natural business key, this is the right answer and it is nearly free. - Keep a dedup record. Key to outcome, with an expiry, checked before doing the work. Necessary when the operation is not naturally idempotent and has no natural key — which is the common case for payments. Lesson 13.30's dojo is this, and its central point holds in production: the insert must be the check. Look up, then insert, and two simultaneous retries both find nothing and both proceed.
Expiry is a correctness decision
A dedup record you deleted is a duplicate you will accept. So the retention period is not an operational tuning knob — it must cover the longest retry window anywhere in the chain, and that window is usually longer than you think. A mobile client that retries a queued request the next time the app opens can arrive eighteen hours later. A queue's dead-letter reprocessing can arrive days later. Pick the number deliberately, write it down, and make sure nobody's retry policy quietly exceeds it.
The timeout is where double charges actually come from
This is worth stating as bluntly as possible, because it is the single most expensive misunderstanding in this area:
A timeout is not a failure. It is an unknown.
Your request left. The response did not come back. Those are the only two facts. The operation may have completed perfectly, completed and failed on the way back, or never arrived. Treating that as a failure and retrying — without an idempotency key the other side honours — is precisely how a card gets charged twice. Every real double-charge incident has this shape.
So a timeout needs a third code path, not a second: succeeded, failed, and unknown. The unknown case does not retry blindly. It queries — asks the far side what happened — or leaves the operation in an explicit pending state that a reconciliation job (3.05's tool, doing serious work here) resolves later by comparing both systems' records.
The vendor at the end of the chain
Your end-to-end property terminates at the first party who does not participate. If the payment provider accepts no idempotency key, you cannot make the guarantee hold — you can only narrow the window. What you can do is 5.04's anti-corruption layer, earning its keep: classify their errors so that "declined" and "our gateway is unwell" are different things in your code, keep their reference id against your operation, and build the reconciliation path on the assumption that ambiguity is permanent. Ask about idempotency support during vendor selection, because it is much cheaper to choose a provider that has it than to build around one that does not.
Adding an idempotency key to the public API and declaring the system idempotent. The API is now safe, and the queue between the service and the worker still redelivers on every consumer restart — at which point the worker charges the card again, hours after the API's key was consumed and released. The API protected the first hop of six. Trace the whole path, name what deduplicates at each hop, and treat any hop that cannot answer as the actual guarantee your system offers.
The tradeoff sheet
- Problem solved
- Retries — from clients, queues, proxies and users refreshing — turning one intended action into several real ones.
- Rejected, and why
- Hoping the operation is naturally idempotent works for writes with a natural key and fails for anything that creates or charges. Deduplicating at one layer only moves the problem to the layer above.
- The trade
- A key store with a TTL to run, a key generation scheme clients must honour, and a slower write path that checks before acting.
- First to break at 10x
- The TTL window. At 10x the key store is large, and a redelivery arriving after expiry is indistinguishable from a new request.
- What it does to the bill
- Small and steady — a keyspace proportional to write volume times retention. Cheap insurance against a class of bug that is expensive in every other currency.
- Day-2 operations
- Track duplicate-suppression rate as a metric. A sudden rise means an upstream is retrying harder than you think, which is usually the early warning for something else.