What it is
An idempotent operation can be performed multiple times and still has the same effect as performing it once.
Why it matters
Distributed systems retry after timeouts, network failures, and worker crashes. Without idempotency, a retry can double-charge a card, send duplicate emails, or create duplicate records.
How it works
Clients send an idempotency key or the server derives a stable operation key. The server stores the first result and returns it again for duplicate attempts.
function handle_request(request):
key = request.headers["Idempotency-Key"]
# check if we already processed this key
cached = store.get(key)
if cached is not None:
if cached.status == "in_progress":
return 409 Conflict # concurrent duplicate
return cached.response # return original result
# mark as in-progress to block concurrent retries
store.set(key, {status: "in_progress"}, ttl=24h)
try:
result = process_business_logic(request)
store.set(key, {status: "done", response: result}, ttl=24h)
return result
except error:
store.delete(key) # allow retry on failure
raise
The in_progress state prevents two concurrent retries from both executing the side effect. The TTL ensures the store does not grow forever.
When to use it
Use idempotency for payments, webhooks, job processing, uploads, provisioning, and any write operation that clients may retry.
Tradeoffs / failure modes
Keys need retention windows, collision protection, and consistent request validation. Reusing a key for a different payload should fail.
Tiny example
POST /payments includes Idempotency-Key: order_123_charge; retries return the original charge result instead of creating another charge.
Related guides and systems
See Payment Processing, Webhooks, Job Queues, File Upload Pipeline, GitHub, Slack, and Gmail.
Seen in deeper dives
Capability
File Upload Pipeline
Chunked and resumable uploads, server-side processing, and CDN delivery at scale
Capability
Job Queues & Background Workers
Async task processing with retries, dead-letter queues, and priority scheduling
Capability
Payment Processing
Idempotent charges, webhook reconciliation, and PCI compliance for online payments
Capability
Webhooks
Reliable event delivery to external systems with retry, HMAC verification, and monitoring
Case Study
Airbnb
A two-sided marketplace for search, booking, payments, and trust
Case Study
GitHub
The world's largest code hosting platform powered by Git at scale
Case Study
Gmail
The world's largest email service with AI-powered organization and search
Case Study
Slack
Channel-based messaging for the enterprise workplace
Related concepts
Retry with Exponential Backoff
Retry transient failures slowly enough that recovery is possible.
Backpressure
Slow producers down when consumers cannot keep up.
Circuit Breaker
Stop calling an unhealthy dependency before failures cascade through the system.