Skip to content
We areTOP 1%

What is idempotency, and how do you make a POST request safe to retry?

MediumConceptAPIs & Communication
Track your progress
First attempt
Re-solve after a few days
Final pass before interviews

Answer

An operation is idempotent if performing it several times has the same effect as performing it once. Retries are unavoidable in distributed systems, because a timeout does not tell the client whether the server processed the request, so non-idempotent operations such as payments need protection against duplicates.

  • Idempotency key: the client generates a unique ID (for example, a UUID) per logical operation and sends it in a header such as Idempotency-Key.
  • The server stores the key with the result; a repeated request with the same key returns the stored result instead of executing again.
  • Use a unique constraint or an atomic insert on the key so that two concurrent retries cannot both run.
  • Expire stored keys after a suitable window, such as 24 hours.

More in APIs & Communication

Sign in to track your progress

Your checkboxes, streak and activity heatmap are saved to your account and sync across devices. It is free.
Sign in with email

New here? Create an account