Idempotency, Retries and Timeouts

Make operations safe to retry with idempotency keys, and retry correctly with timeouts, exponential backoff and jitter.

Intermediate · about 3 hours · difficulty 3/5

Learn first: API Design: REST, gRPC and GraphQL, Relational Databases and SQL for Design

Networks fail in the worst way: the request succeeded, but the response was lost. The client cannot tell, so it retries. If "create payment" is not idempotent, the user is charged twice.

Idempotency

An operation is idempotent if doing it many times has the same effect as doing it once. GET, PUT and DELETE are idempotent by design; POST usually is not.

Idempotency keys

  • The client generates a unique key (a UUID) per logical operation and sends it in a header such as Idempotency-Key.
  • The server stores key, request fingerprint, status and response. On a repeat with the same key, it returns the stored response instead of executing again.
  • Store the key in the same transaction as the side effect, or use a unique constraint, so two concurrent retries cannot both run.
  • Expire keys after a sensible window (Stripe keeps them for 24 hours).

UPI and card payments rely on this idea: every transaction carries a unique reference so duplicates are detected.

Timeouts

Every network call needs a timeout; without one, a slow dependency ties up threads until the whole service hangs. Set timeouts from the dependency's p99 plus margin, and make the overall request budget shrink as it flows downstream (deadline propagation).

Retrying well

  • Retry only transient failures (timeouts, 503, connection resets), not 400s.
  • Use exponential backoff with jitter (wait 100 ms, 200 ms, 400 ms, each randomised) so clients do not retry in lockstep.
  • Cap retries and use a retry budget (for example retries may add at most 10 percent load). Retrying at every layer multiplies traffic: 3 retries across 4 layers can mean 81 calls.

Pitfalls

  • Retrying non-idempotent operations blindly.
  • Retries without jitter creating synchronized waves that keep a recovering service down.
  • Infinite timeouts in HTTP clients (several popular libraries default to none).

You are ready to move on when you can design an idempotent "place order" API, including what is stored, what a duplicate request returns and how concurrent duplicates are handled.

Key concepts

  • Idempotent operations
  • Idempotency keys
  • Handling concurrent duplicates
  • Timeouts and deadlines
  • Exponential backoff with jitter
  • Retry budgets and amplification

Mini project

Build a payment-intent API that accepts an Idempotency-Key, stores results in Postgres with a unique constraint, and returns the cached response on replays. Write a client that retries with backoff and jitter while you randomly drop responses, and prove no double charges occur.

Check yourself

Two identical requests with the same idempotency key arrive 5 ms apart on different servers. How does your design make sure only one executes?

Free resources