Idempotency, Deduplication, and Safe Replays
Design idempotent API operations and deduplication records so clients can retry after timeouts without creating duplicate payments, jobs, or updates.
Idempotency keys let clients retry after a timeout without repeating a payment, job, or other business effect. The guide explains how to scope keys, compare request fingerprints, reserve work atomically, handle in-progress requests, and retain results for the right retry window. It also covers provider-side references and reconciliation for cases where an external effect may have succeeded before the response was lost.
Idempotency, Deduplication, and Safe Replays
Introduction
A payment request can reach the server and charge the card even if the response never reaches the client. If the client retries after a timeout, a new operation may create a second charge. For an endpoint that documents idempotency-key support, the client gives both attempts the same operation identity:
curl -X POST https://api.example.com/payments \
-H "Idempotency-Key: ${IDEMPOTENCY_KEY}" \
-H 'Content-Type: application/json' \
--data-binary @request.json
Set IDEMPOTENCY_KEY to a fresh value when starting an operation, then reuse it only for retries of that operation. request.json stands in for a local request-body file; the example contains no real key or payload. If the response is uncertain, the client sends the same key and request again, and the service returns the existing result instead of charging twice. The key belongs to one logical operation and should not contain personal or payment data.
Key lifecycle and storage
Treat an idempotency key as the identity of one logical operation, not as a globally unique secret. Scope the lookup to the authenticated caller and operation, then store a canonical request fingerprint alongside the key so a retry with changed input can be rejected.
Reserve the key atomically before starting work. A new reservation moves from in_progress to complete; a matching replay can return the stored result or an in-progress status, while a mismatched fingerprint is a conflict. Keep records for at least the documented retry window, with longer retention for costly or irreversible effects. If the side effect runs in another system, carry a stable provider reference and reconcile uncertain outcomes.
stateDiagram-v2
[*] --> Absent
Absent --> InProgress: Atomic reservation wins
InProgress --> Complete: Outcome known and result persisted
InProgress --> InProgress: Matching retry while work runs
Complete --> Complete: Matching retry replays stored result
InProgress --> Conflict: Same scoped key, different fingerprint
Complete --> Conflict: Same scoped key, different fingerprint
Complete --> Expired: Retention policy removes record
Expired --> InProgress: Later request reserves new work
note right of Conflict
Conflict is a response path, not a stored lifecycle state.
end note
The stored result makes a retry a lookup rather than a second business operation. If the original work is still running, return its documented in-progress response; do not start the effect again. Expiry removes deduplication protection, but it does not undo the original effect. Keep active reservations out of the normal completed-record expiry path and define separate recovery for work abandoned mid-operation.
Capacity planning example
Estimate capacity from the retry window and measured request shape. For one illustrative workload, assume 10 million new idempotent operations per day, a seven-day retention period, and an average durable record of 1.5 KB including the response reference and metadata. This yields about 70 million live keys (10 million × 7 days) and 105 GB of raw record data (70 million × 1.5 KB, using decimal units). If indexes, storage overhead, and replicas together require 2.5 times the raw space, budget roughly 263 GB. Measure the actual record and deployment overhead before provisioning.
At 10 million operations per day, average arrival rate is about 116 operations per second (10 million ÷ 86,400). If a load test shows a peak-to-average ratio of 8, plan for about 930 lookups per second at peak (116 × 8); a further 2× capacity margin would set the initial lookup target near 1,860 per second. This is a starting estimate: include retries, status polling, and hot-key behavior in the load test. The seven-day TTL is appropriate only if it covers the documented client retry window and any longer reconciliation period the business requires.
Implementation sketch
The reservation must be atomic. The business effect and idempotency record may span separate systems, so a transaction alone may not cover both. Use the downstream provider’s idempotency facility where available, persist operation state, and reconcile ambiguous outcomes rather than assuming failure.
async function createPayment(
key: string,
input: PaymentInput,
): Promise<PaymentResult> {
const fingerprint = hashCanonicalJson(input);
// Resolve by authenticated caller, operation, and key, not key alone.
const existing = await store.reserveOrRead(key, fingerprint);
if (existing.kind === "conflict")
throw new Error("key reused with different request");
if (existing.kind === "complete") return existing.response;
if (existing.kind === "in_progress")
return { status: "processing", id: existing.operationId };
const result = await provider.charge(input, { idempotencyKey: key });
await store.complete(key, result);
return result;
}
When to use and when not to
Use idempotency for payments, order creation, provisioning, job submission, and any operation clients may retry after an ambiguous timeout. Use event IDs for duplicate message or webhook deliveries. It is less useful for naturally repeatable reads, though read endpoints should still avoid surprising side effects. Do not accept keys without limits: validate syntax and length, scope them to a principal, and define expiration and collision behavior.
| Strategy | Benefit | Trade-off |
|---|---|---|
| Client supplied key | Retries preserve the caller’s operation identity | Clients must reuse the same key for a retry |
| Server generated operation ID | Easy status lookup after acceptance | Client may not receive the ID if the response is lost |
| Payload hash deduplication | Can detect exact repeated messages | Similar payloads may represent separate legitimate actions |
| Downstream provider key | Extends protection across a boundary | Provider retention and semantics may differ |
Design choices
| Decision | Safer default | Alternative | Trade-off |
|---|---|---|---|
| Key scope | Authenticated caller + operation + key | Global key namespace | Scoped keys reduce collisions and prevent one caller from retrieving another caller’s result; global lookup is simpler but risks cross-operation collisions. |
| Durable key store | Database record with a unique constraint and atomic reservation | Cache-only record | A durable store survives restarts and coordinates workers; a cache is faster to expire but eviction can allow a duplicate effect. |
| Request fingerprint | Hash canonical input with a stored fingerprint version | Hash raw serialized bytes | Canonical, versioned hashing handles field order and future canonicalization changes; raw bytes are simpler but equivalent requests can hash differently. |
| Retention window | Cover the documented retry window, extending it for costly effects | Short fixed TTL | Longer retention uses storage and may complicate cleanup; early expiry can turn a late retry into a second operation. |
| Retry behavior | Return the stored result or an in-progress status for a matching key | Start the operation again | Reusing the existing operation avoids duplicate work; callers need a documented way to check pending outcomes. |
| Replay safety | Reauthorize the caller and reuse downstream provider references | Trust possession of the key | Authorization and stable downstream references limit data exposure and duplicate external effects; key-only trust can leak stored responses or repeat side effects. |
Retry walkthrough
Assume one logical operation has a scoped key and a canonical request fingerprint. The response for each case is part of the API contract; the important rule is that a retry must not start a second effect while the original record still protects it.
| Request pattern | Server behavior | Client next step |
|---|---|---|
| Same key and same payload after completion | Return the saved result; do not run the operation again | Treat the response as the outcome of the original operation |
| Same key with a different payload | Return a conflict response; leave the original record and result unchanged | Use a new key only for a new logical operation |
| Two matching requests arrive concurrently | Atomic reservation lets one request start; the other sees the in-progress operation | Wait or check the operation status, then replay with the same key if needed |
| Same key arrives after its record expired | The server may treat it as a new operation because it no longer has the prior record | Do not retry beyond the published retention window; reconcile critical effects by operation or provider reference |
The active HTTPAPI Idempotency-Key Internet-Draft describes one possible convention for in-progress and mismatched requests; it is not a published standard. Choose and document response codes and expiry behavior for your API. Stripe has its own idempotency behavior, including key pruning and parameter comparison.
Observability checklist
- Count new, replayed, in-progress, conflict, and expired-key requests.
- Record operation IDs and key hashes, never raw sensitive request bodies.
- Alert on a rise in in-progress records beyond expected processing time.
- Track reconciliation outcomes for ambiguous external effects.
- Preserve correlation IDs through retries and asynchronous work.
Security notes and pitfalls
An idempotency key is not authorization. Authenticate and authorize every replay just as you would a new request; otherwise a leaked key could expose another user’s stored response. Avoid predictable keys and store only hashes if that fits the lookup design. Protect responses containing payment or personal data. A key prevents duplicate execution only when every relevant side effect is covered; partial protection can still create inconsistent records.
Quick recap
- Reuse one idempotency key for retries of the same logical action.
- Compare a canonical request fingerprint to catch key reuse with different input.
- Reserve keys atomically and persist the operation outcome.
- Handle in-progress work and downstream side effects explicitly.
Interview Questions
Further Reading
- API Keys, Sessions, and Service Credentials — How caller identity and credentials shape key scope and replay authorization.
- Sync, Async Messaging, and Webhooks — Apply deduplication to repeated webhook and message deliveries.
- Stripe: Idempotent requests — A production example of key scope, replay behavior, and retention.
- RFC 9110: HTTP Semantics — The intended idempotency semantics of HTTP methods.
- HTTPAPI Idempotency-Key Internet-Draft — Current work in progress for a request header that supports safe replays.
Conclusion
A timeout leaves the client guessing. A stable idempotency record lets the service recognize a retry and return the existing outcome. Scope keys to the caller and operation, reserve them atomically, and authorize every replay.
Next: retries and deadlines
Continue with Retries, Timeouts, Backoff, and Circuit Breakers to fit safe retries inside a caller-visible deadline.
Category
Related Posts
API Clients, Servers, and Network Boundaries Explained
Understand what API clients and servers each own, how network boundaries fail, and how timeouts, retries, and trust boundaries shape reliable integrations.
Partial Failure, Ordering, and Eventual Consistency
Understand partial API failures, message ordering, and eventual consistency, then design status models and recovery paths clients can reason about.
Retries, Timeouts, Backoff, and Circuit Breakers
Set API deadlines, bounded retries, exponential backoff, and circuit breakers so transient failures recover without multiplying load or hiding outages.