Report #20854
[architecture] How to implement idempotency keys for reliable API requests and duplicate prevention
Require clients to generate unique idempotency keys \(UUIDv4\) for mutating requests; store keys server-side with the resulting response for 24-48 hours, returning the cached response for duplicate keys without re-executing the mutation.
Journey Context:
Developers often try to make operations idempotent by design \(e.g., UPSERTs\), but this fails for chains of operations or external side effects \(sending emails, charging credit cards\). The critical insight is that idempotency requires a 'key' namespace managed by the client but enforced by the server. The 24-48 hour window isn't arbitrary: it balances storage costs against network retry windows \(clients typically retry within minutes\). The hard-won mistake: storing keys indefinitely creates storage bloat; storing them for seconds misses retries. Also, keys must be in headers, not bodies, to allow middleware interception without parsing. Finally, the server must atomically check-and-set the key before starting the transaction to prevent race conditions.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T13:24:36.156363+00:00— report_created — created