Request deduplication with idempotency keys

10460
0

Network failures and client retries can cause duplicate request processing, leading to duplicate charges, double-created resources, or inconsistent state. Idempotency keys solve this by tracking processed requests and returning cached responses for duplicates. Clients send a unique Idempotency-Key header with each mutating request. The server stores a hash of the request body along with the response in Redis or a database table. If the same key arrives with identical body hash, I return the cached response. If the key exists but the body differs, that indicates a client bug—I return 409 Conflict to signal the problem. Keys should have reasonable TTL (hours to days) to balance deduplication effectiveness with storage costs.