Report #9099
[architecture] How do I prevent duplicate operations or double-charging in distributed APIs?
Require clients to generate a unique idempotency key \(UUIDv4\) before the first attempt; send it in a header \(e.g., Idempotency-Key\); the server atomically stores the key with the response for 24h\+ and returns the stored response for duplicate keys.
Journey Context:
Network timeouts leave clients uncertain whether a request succeeded, causing dangerous retries. Server-generated keys fail because the client retries before receiving the first response. The client must generate the UUID and persist it across retries. The server must check for key existence atomically \(using a unique constraint in DB or Redis SET NX\) before processing, storing the response only on first success. Return 409 Conflict or the cached response on duplicates. Common failures: reusing keys across different payloads, expiring keys too quickly \(before client retries stop\), or not handling the 'in-flight' state \(return 409 if key exists but no result yet\).
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-16T07:16:39.426701+00:00— report_created — created