System Design AI

Idempotency in System Design Interviews: When to Use It and What to Say

Idempotency is the property that calling the same operation multiple times yields the same effect as calling it once. In other words, after the first successful execution, any repeat of the request is a no-op.

Why Idempotency Matters

During a design interview you will often be asked how the system handles failures. A common scenario is a client that times out, retries, and ends up sending the same command twice. Without idempotency the second request could create duplicate rows, double-charge a credit card, or trigger a downstream job twice. The system would then need a separate reconciliation process, which is costly and error-prone.

A Concrete Example: Order Service

Imagine an e-commerce checkout service that receives a POST /orders request. The client builds a cart, sends the request, and waits for a 200 response. If the client sees no response after 2 seconds, it retries. The service must ensure that the second request does not create a second order for the same cart.

Step-by-step Idempotent Flow

  1. Client generates an Idempotency-Key – a UUID that uniquely identifies the operation (e.g., order-12345).
  2. Request arrives – the API gateway forwards the request with the key to the order service.
  3. Service checks a durable store – a DynamoDB table keyed by the idempotency key. If a record exists, the service returns the stored response.
  4. If no record, service creates the order – writes the order row, charges the payment, and stores the response (status, order id) together with the key.
  5. Return response – the client receives the same HTTP 200 and order id on both the original and any retry.

The key idea is that the check in step 3 is atomic with the write in step 4. By using a conditional write (PutItem with ConditionExpression attribute_not_exists(key)), the database guarantees that only one request can succeed.

Where Idempotency Shows Up in Classic Design Questions

Design QuestionTypical ComponentIdempotency Role
Payment processingPayment gateway APIPrevent double charge when client retries after a network glitch
Message queue consumerExactly-once processing in a Kafka consumerEnsure that re-processing a message does not duplicate side effects
File upload serviceS3 multipart upload CompleteMultipartUploadGuarantees that completing the same multipart upload twice does not create duplicate files

In each case the idempotency check sits at the boundary where external calls become durable state changes.

Trade-offs and Alternatives

Idempotency is not free. Adding a key store introduces extra latency (a read-modify-write round-trip) and storage cost. It also forces you to design a key-generation strategy that is collision-free and scoped correctly (per user, per operation, etc.).

Alternative: Exactly-once Messaging – Some systems rely on the messaging layer (e.g., Kafka with idempotent producers) to guarantee exactly-once delivery, shifting the burden from the service to the broker. Interviewers may ask you to compare the two approaches:

  • Idempotent API works even when the client is outside your control and can retry arbitrarily.
  • Exactly-once broker reduces per-service code but assumes the whole pipeline uses the same broker and that the broker’s guarantees are sufficient.

When to choose one over the other

  • Use API-level idempotency when the operation is exposed to third-party clients (public APIs, webhooks).
  • Use broker-level guarantees for internal pipelines where you control the producer and consumer and can tolerate the extra complexity of transactional writes.

Common Mistakes Candidates Make

  1. Confusing Idempotent with Safe – Idempotent means repeatable without side effects after the first success; safe (GET) means no side effects at all. Mixing the two leads to suggesting GET for writes.
  2. Omitting the storage of the response – Storing only the key but not the original response forces the retry to recompute the result, breaking the guarantee.
  3. Relying on client-generated keys without validation – If the client can reuse a key across different operations, you may unintentionally block legitimate requests.
  4. Forgetting about expiration – Idempotency keys should eventually expire; otherwise the store grows without bound. Not mentioning a TTL can look incomplete.

30-Second Pitch

"Idempotency means a request can be repeated without changing the outcome after the first successful call. In practice we give the client a unique key, store the key and the response atomically, and on a retry we look up the key and return the stored response. This protects against duplicate charges, double writes, or re-processing a message. The trade-off is a small extra read-write latency and storage, but it simplifies retry logic and avoids costly reconciliation."

How to Practice This

  1. Study the worked design – The System Design AI library includes a worked design for idempotency keys that shows where the idempotency check lives.
  2. Draw your own board – Use the canvas to sketch the request flow, add a durable key store, and annotate the atomic write.
  3. Run a mock interview – Explain the idempotency mechanism, discuss its trade-offs, and answer follow-up questions about key expiration and alternatives.

Questions people ask

What is the difference between idempotent and retry-safe APIs?

Idempotent APIs guarantee the same state after any number of repeats; retry-safe APIs simply handle transient failures without changing the final state. Idempotency is a stronger guarantee that covers client-initiated retries.

Do all HTTP methods need idempotency keys?

No. GET, HEAD, and OPTIONS are already safe and idempotent. PUT and DELETE are idempotent by definition as well, so it is POST (and sometimes PATCH) that usually needs an explicit key.

Can I use a timestamp instead of a UUID for the key?

A timestamp alone can collide if two requests occur within the same granularity. Combining a timestamp with a UUID or a hash of the payload avoids collisions.

How do I clean up old idempotency keys?

Store keys with a TTL (for example, 24 hours) in a database that supports automatic expiration, such as DynamoDB with Time-to-Live enabled.