Topic 038 / Core
Idempotency keys and safe API retries
Chapter 3.5 · API Contract Mechanics
What and why
A client that sends a mutating request (→ 28, e.g. POST /payments) and
then hits a network timeout has no way to know whether the server actually processed it
before the connection dropped; a naive retry risks double-charging or double-creating. An
idempotency key is a unique token the client generates and sends with a mutating
request; the server stores that key together with the outcome of the first successful
processing, and on any retry carrying the same key, returns the stored outcome instead of
processing the request again. This topic is the primary home of API-level idempotency;
consumer-side idempotency for message processing is → 116. Builds on: → 28 (HTTP's
method semantics already make GET/PUT/DELETE idempotent — this topic makes a non-idempotent
method like POST safely retryable too).
Diagram
client ──POST, key=K──► server: no record of K → process, store (K → result)
│
connection drops before the response arrives
│
client ──POST, key=K (retry)──► server: K already recorded → return stored result
(side effect is NOT repeated)
How it works
- The client generates a unique key once per logical operation — not once per HTTP attempt.
- The client sends the key with the mutating request, commonly as a header.
- Before doing any side-effecting work, the server checks whether that key is already recorded.
- If not recorded, the server processes the request and records the key together with the outcome, atomically with the side effect itself.
- If recorded, the server returns the stored outcome without repeating the side effect.
- If a retry arrives while the first attempt is still genuinely in flight, the server must reject it or make it wait, rather than let both run concurrently.
- Keys expire after a bounded retention window sized to how long a client might plausibly retry.
Practical example
(illustrative). A client calls POST /payments with
Idempotency-Key: abc123; the server charges the card and records
abc123 → {status: succeeded, charge_id: ...}, but the response is lost in transit. The
client retries the identical request with the same key inside the retention window and
receives the same stored result — not a second charge.
When to use it
- Use when: the mutating endpoint's side effect is expensive, externally visible, or financial — payments, order creation, anything a client might legitimately retry after a timeout (Project 8).
- Avoid when: the operation is already naturally idempotent under → 28's method
semantics (a
PUTthat simply overwrites a value is already safe to retry without a key) — only non-idempotent operations likePOSTneed one. - Failure scenarios: generating a new key per retry attempt instead of per logical operation defeats the mechanism entirely; storing the key without the outcome still lets a retry repeat the side effect; forgetting to expire keys lets the store grow without bound.
- Simpler alternative: none for genuinely non-idempotent, high-stakes mutations — this is the simpler alternative to building custom reconciliation after the fact.
Practice
(pattern kind — implement minimal, spot the misuse; 20–30 min, no lab
services). Build a POST /orders handler that accepts an Idempotency-Key header,
stores the key with its result, and returns the stored result when the same key repeats
with an identical body. Then spot the misuse: send the same key with a different body,
and decide what the server should do (reject the request rather than silently reusing or
overwriting the stored result). Done when a repeated identical request returns the stored
result unchanged, and a repeated key with a different body is rejected rather than
guessed at.
Key takeaway
An idempotency key turns "retry on timeout" from a correctness risk into a safe default, by making the server — not the client's luck — decide whether a request has already happened. Done when you can: implement the store-and-check pattern and explain what happens to a retry that arrives mid-processing. Used next in → 116 (idempotent consumers do the same job for message delivery); Project 8.
References
Stripe, "Designing robust and predictable APIs with idempotency" — see references.md.
Exercise: Write the OpenAPI contract for a URL shortener (Project 2) before any code: endpoints, status codes, error schema, cursor pagination, a versioning plan, and an idempotency-key design for link creation.
Source directory