Skip to content
System Design Explorer
reliability

Idempotency Keys

By Ankur NayakReviewed

An idempotency key is a unique value the client sends with a request that changes state, so the server can recognise a retry and return the original result instead of doing the work twice. It is what makes it safe to retry a payment after a timeout.

Idempotency Keys diagramComponents: Client, API, Idempotency Store, Payment Network. Connections: Client to API (POST + Idempotency-Key); API to Idempotency Store (lookup / save result); API to Payment Network (charge once).ClientClientAPIServiceIdempotency StoreSQL databasePayment NetworkExternal API
Retry with an idempotency key

How it works

Networks fail in the worst place: after the server has done the work but before the client hears back. The client cannot tell whether the charge happened, so the only safe options are to give up or to retry. A retry is only safe if doing the operation twice has the same effect as doing it once.

The client generates a key (Stripe suggests a V4 UUID or another random string with enough entropy) and sends it with the request. The server stores the key with the outcome of the first request. A later request with the same key gets the stored status code and body back instead of running again.

Stripe's API accepts idempotency keys on every POST request. It saves the status code and body of the first request made with a key once the endpoint starts executing, compares the parameters of any retry with the original and returns an error if they differ, and can prune keys once they are at least 24 hours old.

Clients pair keys with retries that back off exponentially and add random jitter, so a fleet of clients retrying after an incident doesn't arrive at the same moment (the thundering herd).

Key points

  • The client chooses the key; the server stores key → result
  • Retries with the same key return the stored result
  • Reject a reused key whose parameters differ
  • Keys can expire (Stripe: after at least 24 hours)
  • Retry with exponential backoff and jitter

When to use it, and pitfalls

Use it for

  • Any API call that moves money or creates something users would notice twice
  • Consumers of at-least-once queues (outbox relays, webhooks) that may see a message more than once
  • Mobile clients on unreliable networks

Watch out for

  • Storing the key after the side effect: a crash in between still double-charges. Record the key and the work atomically, or record progress steps
  • Reusing keys across different operations or users
  • Keeping keys forever: they need an expiry
  • Concurrent requests with the same key: decide which one wins (Stripe does not save a result for a request that conflicts with one already executing)

Trade-offs

Server-side keys vs. naturally idempotent operations

Use keys for operations that create things; design updates to be naturally idempotent ("set status to paid") where you can.

Pros: Works for any operation, including ones with external side effects; Clients can retry blindly

Cons: Extra storage and a lookup on every write; Needs an expiry policy

Used in

Frequently asked questions

What is an idempotency key?

A unique value, usually a random UUID, that a client sends with a request that changes state. The server stores the outcome under that key, so if the client retries with the same key it gets the original result instead of the operation running again.

Who generates the idempotency key, the client or the server?

The client. It has to exist before the first attempt is sent, because the whole point is that the client can retry an attempt whose outcome it never saw. Stripe recommends V4 UUIDs or another string with enough randomness to avoid collisions.

How long should idempotency keys be kept?

Long enough to cover any realistic retry window. Stripe's API v1 documentation says keys can be removed automatically once they are at least 24 hours old; a request with a pruned key is treated as a new request.

Sources