Skip to content

Engineering

Idempotency Keys: Making Retries Safe

How a single client-supplied key turns a dangerous retry into a harmless duplicate, and what servers must do to honour it.

3 min read

Networks fail in the worst possible place: after the server has done the work, but before the client hears about it. The client retries, and the payment is charged twice, the order is created twice, the email is sent twice. Idempotency keys are the standard fix — a small protocol agreement that makes retries safe.

An idempotent operation produces the same result no matter how many times it is executed. GET and DELETE are naturally idempotent; POST usually is not, because each call creates something new. An idempotency key restores the property: the client generates a unique key (typically a UUID) for each logical operation and sends it with the request, usually in an Idempotency-Key header. The server records the key alongside the result of the first execution. If a retry arrives with the same key, the server returns the stored result instead of executing again.

A correct implementation has three parts:

  1. Key storage. On first seeing a key, the server stores it with the operation’s result (or its in-progress state) before or atomically with the side effect. The store must survive restarts — a database table with a unique constraint on the key is the usual choice.
  2. Replay. A repeat request with the same key returns the stored response, with the same status code and body, without re-executing anything.
  3. Expiry. Keys are kept for a bounded window — Stripe, for example, retains idempotency keys for 24 hours — after which a reused key is treated as new. Clients should therefore generate a fresh key per logical operation, never reuse one across different operations.

The atomicity requirement is the subtle part. If the server executes the side effect and then crashes before storing the key, a retry will execute twice. The standard solution is to claim the key first (insert it in a “processing” state inside the same transaction as the work, or with a unique constraint that makes the claim atomic), and only then perform the effect.

Retries are unavoidable in distributed systems: load balancers time out, connections reset, clients crash. Without idempotency, every retry policy is a gamble — retry too little and operations fail spuriously; retry too much and you duplicate effects. Idempotency keys decouple the two concerns: clients can retry aggressively, and servers guarantee at-most-once effect even though the request is delivered at-least-once. This is the same idea behind exactly-once processing in stream systems, implemented at the API boundary.

An idempotency key does not make an operation safe to retry with different parameters — a second request with the same key but a different body is a client bug, and servers typically reject it with a 422. Nor does the key help if the client generates a new key per attempt: the key must be stable across retries of the same operation, which means generating it once, before the first attempt, and keeping it until the operation is confirmed. A natural key can also work — an order number or a client-generated UUID stored as the record’s primary key — but an explicit header keeps the mechanism separate from business data.