Question presented to candidate:
"We keep hearing that an API or a message handler should be idempotent. What does idempotency actually mean, why do distributed systems need it, and how would you make a non-idempotent operation like 'create an order' safe to retry?"
What a strong answer should cover:
The definition: doing an operation many times has the same effect as doing it once (the response may differ, the state must not)
Why it matters: timeouts make the outcome unknown, so clients retry; queues deliver at-least-once; without idempotency, retries create duplicates or double charges
HTTP semantics: GET, HEAD, OPTIONS, PUT and DELETE are idempotent by definition (RFC 9110); POST and PATCH are not
The fix for non-idempotent operations: a client-generated idempotency key, a server-side table that stores key, request fingerprint and saved response, enforced with a unique constraint
Details that separate strong answers: reject the same key with a different payload, handle a second request while the first is still in flight, set a retention window, and dedup by message ID in consumers
Idempotency plus at-least-once delivery is how systems get "effectively once" behaviour
Code / implementation expected: Optional โ sketching the key lookup, the unique insert and the replayed response is a plus; the example simulates a lost response and a duplicate-delivering queue.
idempotency
2
Try it yourself
Sketch your approach before you read the solution. That is what the interview tests.
Stuck? Get a hint.
A short nudge toward the approach. It will not give the answer away.
3
Solution
Target Audience: Beginners who have built or called a web API and want to understand why retries are dangerous and how real systems make them safe.
Difficulty: Medium
Verified with: Node 22.22 and Python 3.12 (both print identical output). HTTP method semantics are cited from RFC 9110; Stripe behaviour from the Stripe API reference ("Idempotent requests"); the Idempotency-Key header from the IETF httpapi working-group Internet-Draft.
How to read this doc: Every idea is explained in plain language first, then tagged with ๐ Interview term: so you also learn the words an interviewer expects. Every number comes from running the example; the Verified section quotes its full output.
1. Why This Even Matters โ A Story First
You press the lift button. Nothing seems to happen, so you press it again, and again. The lift still comes once. Pressing the button is id
The solution is hidden so you can try first
16 min read ยท 11 sections
4
Read the code
Idempotency in miniature: PUT vs POST, an idempotency-key store surviving a lost response, payload mismatch, and a dedup consumer