System Design · Concept Library · Free article
Idempotency Keys: How to Make API Retries Safe
An idempotency key is a stable identifier the caller sends with every attempt at one logical write, so a server can tell a retry from a new request and replay the first result instead of doing the work twice.
An operation is idempotent when repeating it has the same intended effect as doing it once. Give one logical write a stable key and reuse it on every attempt. The server claims the key atomically, checks that the payload still matches, and replays a completed result. If the effect crosses into another service, carry the key downstream. Add an outbox when the handoff must also survive a crash. A local transaction cannot make a remote side effect atomic.
The reply disappeared. Did the work happen?
Hatch asks a texting provider to notify a creator. The provider sends the text, but its reply disappears on the way back. Hatch sees a timeout. That same timeout would also appear if the request never reached the provider. The caller cannot tell those cases apart.
Giving up loses real work. Retrying a plain POST can repeat work that already happened. The retry needs to say, "this is another attempt at the same notification," not "create another notification."
Watch one lost reply become two sends
Run the same unreliable handoff with and without a stable key. The network failure does not change. The meaning of the retry does.
The interactive version of this example is available when JavaScript loads.
The failure is identical in both runs, and that is the point: the provider never sees a difference, because from its side a lost reply and a dropped request look the same. What changes is whether the second attempt carries information the server can act on. Without a key the retry is indistinguishable from a fresh request, so a correct server does the work again and the creator gets two texts. With one, the second attempt matches a record the server already holds and is answered from it. The network was never the thing that got fixed.
How does an idempotency key work?
The caller mints one key when the action starts and reuses it on every attempt, and the server stores the outcome under that key so a repeat is answered from the record rather than re-executed. The caller creates an unpredictable key, often a UUID, when the user starts the action. Every retry carries that same key. The server stores the first request fingerprint and outcome under the key. A completed repeat gets the saved status and body instead of running the operation again.
A fresh key on every attempt defeats the design. It tells the server that every retry is new work.
The interactive version of this example is available when JavaScript loads.
A useful server contract
- Validate the request, then calculate a fingerprint of the fields that define the operation.
- Atomically claim the scoped key with a uniqueness rule. One concurrent request can win.
- If the key is complete and the fingerprint matches, replay the stored response.
- If the key is still in progress, return the API's documented wait or retry response.
409,202, polling, andRetry-Afterare all possible designs. - If the same key arrives with a different fingerprint, reject it with a documented client error.
What can actually be atomic?
Local transaction and remote outbox patterns
Two shapes, and the difference between them is exactly the boundary above. When the effect is local, the business change and the key record go in one transaction, so there is no window where one exists without the other. When the effect is remote, no transaction can span it, so the local commit writes an outbox row instead and a dispatcher carries the same key downstream until the provider acknowledges it.
The interactive version of this example is available when JavaScript loads.
Read the two side by side and the asymmetry is the lesson. The local version is finished when the transaction commits. The remote version is never finished by a commit at all: it is finished when something downstream confirms, which is why it needs a retry loop, a stable key to retry with, and a plan for the case where the provider will not honour the key.
How long should an idempotency key be kept?
At least as long as a legitimate retry can arrive, which in practice means hours rather than minutes: Stripe prunes after 24 hours, comfortably past any automated retry window. Keep a key at least as long as a legitimate retry may arrive. Publish the retention window so clients know when reuse stops being safe. Scope keys by tenant and operation, and store a request fingerprint so accidental reuse with different inputs is visible. Decide whether failures are replayed, retriable, or released only after you understand what work may already have happened.
Which operations need an idempotency key?
| Operation | Duplicate effect to prevent |
|---|---|
| Payment request | Charging one purchase twice |
| SMS, email, or push notification | Sending the same notification twice |
| Order or reservation creation | Creating two orders or holding two seats |
| Webhook handler | Applying one provider event more than once |
| At-least-once message consumer | Repeating a state change after redelivery |
Do not promise more than the boundary provides
An unreliable transport cannot by itself prove that a message was delivered exactly once. Systems usually retry so work is not lost, then deduplicate attempts at the place that owns the effect. That can provide a strong once-per-key outcome inside a defined boundary. It does not make every downstream side effect or every failure mode disappear.
Primary references
- RFC 9110: HTTP method semantics and idempotent methods
- Stripe: idempotent request behavior, saved results, payload checks, and key pruning
Retry the attempt, not the business action
By GOAT Engineer. Back to Concept Library. Privacy Policy · Contact.