Skip to content
AI360Xpert

Idempotency

Idempotency architecture
Idempotency architecture

Overview

An operation is idempotent when performing it multiple times has the same effect as performing it once. In distributed systems, idempotency is what makes retries safe, so that a client or broker can resend a request after a timeout without causing duplicate side effects such as a double charge.

🧠 Mental model: An elevator button is idempotent - pressing it 10 times has the same effect as pressing it once. A vending machine button is not - pressing it 10 times gives you 10 sodas (and a big bill).

Key Concepts

Some operations are naturally idempotent, and some must be made so:

  • Naturally idempotent: "set balance to 100" or an HTTP PUT that overwrites a resource; repeating it changes nothing after the first success.
  • Not idempotent: "add 100 to balance" or an HTTP POST that creates a new record; repeating it duplicates the effect.

The standard technique is an idempotency key: the client attaches a unique identifier to the request, and the server records the key with the result. If the same key arrives again, the server returns the stored result instead of redoing the work.

Idempotency is the safety net for at-least-once delivery in message queues and publish-subscribe, and it is a common building block inside distributed transactions, where compensating steps may be retried.

HTTP method Idempotent? Note
GET Yes Read-only
PUT Yes Overwrites to a fixed state
DELETE Yes Deleting an already-deleted item is a no-op
POST No Typically creates a new resource each time

Trade-offs

Idempotency keys let you retry freely, but the server must store processed keys and their results, which costs memory or a database table and needs a time-to-live so it does not grow forever. Deduplication windows are finite, so extremely delayed duplicates can slip through. There is also a concurrency concern: two copies of the same request arriving at once must be serialized, usually with a unique constraint or a lock on the key.

Interview Tips

  • State the failure that motivates it: a lost response leaves the client unable to tell success from failure.
  • Propose an idempotency key with a stored result and a TTL as the default design.
  • Map HTTP verbs to their idempotency to sound precise; POST is the odd one out.
  • Mention that duplicate concurrent requests need a unique constraint or lock, not just a lookup.

Summary

  • An idempotent operation produces the same result whether applied once or many times.
  • Idempotency makes retries safe, which is required under at-least-once delivery.
  • Idempotency keys let a server detect and short-circuit duplicate requests.
  • PUT, DELETE, and GET are idempotent by design; POST usually is not.
  • Deduplication state needs a TTL and concurrency control to bound cost and handle simultaneous retries.