
Understand it
Retries can repeat an operation whose first outcome is unknown. A sound idempotency design binds a key to the request and outcome, coordinates deduplication with the business effect and defines expiry. Adding a header alone does not create exactly-once execution.
A retry may repeat an operation when the caller cannot tell whether the first attempt succeeded. A timeout is ambiguous: the server might have committed the change and lost the response. Idempotency means repeating the same logical request has the same intended effect as performing it once. Setting a value to a fixed state can be idempotent; incrementing a balance or charging a card usually is not. Do not assume the network itself always delivers at least once or that every read has no side effects.
Give each logical operation a stable idempotency key. The server records the key and outcome atomically with the operation where possible. A retry with the same key returns the recorded outcome rather than repeating the effect. Define how long keys live, what happens if the same key is reused with different parameters, and how concurrent duplicates race. A key stored after charging but before saving the result leaves a dangerous gap; the transaction boundary matters.
For an interview, draw the timeline: request sent, server commits, response lost, caller retries. Then explain how the dedupe record makes the second request safe. Add exponential backoff with jitter so clients do not all retry at once under overload. If an external provider is involved, propagate a stable key when it supports one and reconcile uncertain results. The goal is a recoverable outcome under ambiguity, not a promise that the network will behave exactly once.
Interview follow-up: What happens if the server crashes between charging and recording the key? Define atomicity, key reuse checks and the retention window.
Read the code
let response = client
.post(url)
.header("Idempotency-Key", request_id)
.json(&payment)
.send()
.await?;HTTP client sketch: the server must implement the key contract; a header alone does not deduplicate a side effect.
Practice starter
POST /accounts/7/balance
{"add": 10}
// The response is lost. The server may or may not have committed.Try it
Make a non-idempotent endpoint safe to retry.
What success looks like: A duplicate request returns the original result and charges once.
Check your understanding
After a timeout on this relative update, what must the client assume without a deduplication contract?
Reveal the answer
- Sending the same body automatically deduplicates every HTTP POST.
- Retrying can apply the addition twice; a timeout does not prove the first attempt failed.
- A lost response necessarily rolls back the server’s committed operation.
An idempotency key only helps when the server validates request identity and durably/atomically records its relation to the effect and response.
Go deeper
Common misconception: A lost response necessarily rolls back the server’s committed operation.
A lost response says nothing about the commit. The server may have applied the addition before the response vanished, so the outcome stays unknown until something confirms it.
- Identify the contract: A retry is only safe if the operation is idempotent.
- Trace the concrete example: The key is what makes the second attempt safe.
- Test the changed constraint: What happens if the server crashes between charging and recording the key? Define atomicity, key reuse checks and the retention window.
Transfer challenge: What happens if the server crashes between charging and recording the key? Define atomicity, key reuse checks and the retention window.
Connections
- CAP in Practice: What happens during a partition
- Caching and Invalidation: Staleness budgets and stampedes
Learn this lesson with AI
Paste this into Claude, Codex, Cursor or another agent connected to Crabrix Academy. It contains only this lesson’s public details.
I'm learning Rust with Crabrix Academy. Teach me this lesson with the Crabrix Academy tools. Lesson: Idempotency and Retries (courseId "interview", lessonId "q-idempotency") Link: https://crabrix.com/learn/interview/q-idempotency/ Goal: An idempotency key needs an enforced request/outcome contract, not just a header. Practice: give me the lesson's exercise (crabrix_academy_get_exercise); I will write and run the code in Crabrix. Explain it step by step for my level, let me write the code myself, give hints before any solution, and do not mark anything as passed: only Crabrix checks do that.