System Design · Architecture under constraints
How do idempotency and retries work together?
Short answer
Retries are necessary because failures are ambiguous, but they can duplicate effects. An idempotency key lets the server recognize a repeated request and return the original result instead of repeating the work.
When a request times out, the client cannot tell whether the server failed before or after committing. Retrying is the only way to make progress, but a naive retry can charge a card twice or create two orders. Idempotency turns a repeated request into the same observable outcome.
Server-side idempotency
- The client sends a unique idempotency key, usually a UUID generated per logical operation
- The server stores the key with a fingerprint of the request and the eventual response
- A duplicate with the same fingerprint returns the stored response
- A duplicate with a different payload is rejected as a conflict
- The record needs a TTL, but it must outlive the maximum retry window
CREATE TABLE idempotency_keys (
key TEXT PRIMARY KEY,
request_hash TEXT NOT NULL,
status TEXT NOT NULL,
response_code INT,
response_body TEXT,
created_at TIMESTAMP NOT NULL,
expires_at TIMESTAMP NOT NULL
);Retry policy matters as much as deduplication. Use exponential backoff with jitter so many clients do not retry in lockstep. Cap the number of attempts and the total deadline. Add a retry budget so retries cannot exceed a fraction of normal traffic during an outage; otherwise a small failure becomes a retry storm.
Follow-ups you should expect
Interviewers use these to check whether the first answer was understanding or memorisation.
- 1What do you store for an idempotency key and for how long?
- 2What should happen if the same key arrives with a different payload?
- 3Why is exponential backoff with jitter important?
- 4How do retries interact with a retry budget?
Say it in the interview
A timeout leaves the client unsure whether the write happened, so retries are necessary but dangerous. I make writes idempotent with a client-supplied key, store a request fingerprint and the response, and return the original result for a duplicate. I back off exponentially with jitter, cap attempts and use a retry budget so failures do not turn into a storm. Exactly-once effects come from this deduplication, not from the network.
Use this as a model, not a script. Saying it in your own words is what makes it survive follow-up pressure.
Related questions
Reading is step one
Now say it out loud.
Understanding an answer and delivering it under pressure are different skills. Rehearse this topic in a live System Design mock interview and get scored feedback on depth, clarity and confidence.