What is idempotency and why is it important?
Why Interviewers Ask This
Foundational questions like this help interviewers calibrate the rest of the interview. A confident, accurate answer signals that you have solid System Design basics — a prerequisite for any developer role.
Answer
Idempotency means that performing the same operation multiple times produces the same result as performing it once. An idempotent operation can be safely retried without side effects. In distributed systems, where failures and retries are common, idempotency is critical for correctness. HTTP methods: GET, PUT, DELETE are idempotent (multiple identical requests have the same effect as one); POST is NOT idempotent by default (each POST may create a new resource). Why it matters: in distributed systems, you cannot always know if a request succeeded — the network may fail after the server processed the request but before the response arrived. Without idempotency, retrying a failed payment request could charge the user twice. Implementing idempotency: (1) Idempotency keys: client generates a unique ID per operation; server stores the result keyed by this ID; if the same key is received again, return the stored result without re-processing; (2) Natural idempotency: "SET balance = 100" is idempotent; "ADD 10 to balance" is not; rewrite as "SET balance = current + 10 where balance = current" (compare-and-swap); (3) PUT over POST: use PUT (replace) instead of POST (create) for updates; (4) Idempotent consumers: message consumers must handle duplicate messages (deduplicate using message ID + database unique constraint). Examples: Stripe's idempotency keys for payments, database upsert operations, S3 PUT operations.
Common Mistake
Rushing to answer is a common mistake. Take two seconds to structure your response: definition → example → trade-off. This structure makes complex System Design answers easy to follow.