Medium system-design fundamentals

How do idempotency keys make retries safe for a payment API?

The 30 second answer

The client generates a unique key, like a UUID, and sends it in an Idempotency-Key header with the POST. The server stores the key with the result of the first request; a retry with the same key gets the stored result instead of charging again. If the same key comes with a different body I reject it, and if the first request is still running I return a conflict.

Could you say this in an interview?

Why HTTP methods alone are not enough. RFC 9110 defines PUT, DELETE and the safe methods as idempotent, so a client can repeat them after a network failure. POST is not, yet "create a charge" or "place an order" is usually a POST. If the response is lost, the client cannot tell whether the charge happened. The method rules themselves are covered on API, REST, gRPC and MCP compared.

How Stripe does it. In Stripe's API v1, all POST requests accept an idempotency key, sent as the Idempotency-Key header. They suggest V4 UUIDs or another random string with enough entropy, allow keys up to 255 characters, and advise against putting personal data in keys. For API v1, Stripe saves the status code and body of the first request with a key once the endpoint starts executing, and returns the saved response to retries, including 500 errors. Keys can be pruned after they are at least 24 hours old. If a retry's parameters differ from the original, Stripe returns an error, to catch accidental key reuse.

The IETF draft standard. The HTTPAPI working group's Idempotency-Key header draft recommends a 422 response when a key is reused with a different payload, and 409 Conflict when a retry arrives while the original request is still being processed.

Server-side design.

  • Storage: a table or key-value entry keyed by (client or account, idempotency key), with a hash of the request, a status (in progress or done) and the stored response, plus an expiry.
  • Atomic claim: insert the key with a unique constraint before doing the work, so two concurrent retries cannot both proceed.
  • Same transaction where possible: record the result in the same database transaction as the business change, so a crash cannot leave a charge without a stored result.
  • Same answer: AWS describes returning a semantically equivalent response for a repeated request, as EC2 does with its ClientToken, so the caller's retry logic stays simple.

Retries need both pieces. Stripe's guidance pairs idempotency keys with exponential backoff and randomness on the client, so retries are safe and don't overload the server.

Common mistakes interviewers listen for.

  • Generating the key on the server, or a new key per retry; it must come from the client and stay the same across retries.
  • Checking "does the key exist" and then inserting, which races; use an atomic insert.
  • Deduplicating only in the API and forgetting events: consumers of an outbox or Kafka topic need their own dedup, see idempotent Kafka processing.
Sources (5)
  1. Stripe API reference: Idempotent requestsdocs.stripe.com
  2. Stripe: Designing robust and predictable APIs with idempotencystripe.com
  3. RFC 9110: HTTP Semantics (Idempotent methods)rfc-editor.org
  4. IETF draft: The Idempotency-Key HTTP header fielddatatracker.ietf.org
  5. AWS Builders' Library: Making retries safe with idempotent APIsaws.amazon.com