Theory
Design Idempotency
Idempotency ensures that performing an operation multiple times produces the same result as performing it once. This is critical in distributed systems where retries are inevitable.
- Problem â Network failures cause duplicate requests
- Solution â Idempotency keys for deduplication
- Guarantee â Exactly-once processing semantics
Idempotency is the foundation of reliable distributed systems: if retries are safe, the system is resilient.
What Is Idempotency?
Idempotency Key Design
Implementation Pattern
def process_payment(user_id, amount, idempotency_key):
# Check if key exists in Redis
if redis.exists(f"idemp:{idempotency_key}"):
return redis.get(f"idemp:{idempotency_key}")
# Set key with TTL (24 hours)
redis.setex(f"idemp:{idempotency_key}", 86400, "processing")
try:
result = charge_payment(user_id, amount)
redis.setex(f"idemp:{idempotency_key}", 86400, result)
return result
except Exception as e:
redis.delete(f"idemp:{idempotency_key}")
raise
Exactly-Once Semantics
Deduplication Window
Practice Exercises
- Design: Implement an idempotent payment API that handles concurrent duplicate requests.
- Storage: Design a deduplication system for 1M requests/second with 24-hour TTL.
- Migration: How would you add idempotency to an existing non-idempotent API without downtime?
- Consistency: Design a system where idempotency keys are generated client-side vs server-side.
What to Learn Next
-> Saga Pattern Distributed transactions with idempotency.
-> Outbox Pattern Reliable event publishing.
-> Retry Patterns Resilient retry with backoff.
-> Circuit Breaker Preventing cascade failures.
-> Back Pressure Load management.
-> Design Amazon Idempotent checkout.