How does an idempotency key stop a duplicate request?
Sketch your own answer first, then compare it with the one below.
A strong answer
The client generates a unique key for one logical operation and sends it with every attempt, including retries. The server stores the key with the result of the first run. If a request comes with a key it has seen, it returns the stored result and does nothing else.
The key must identify the operation, not the attempt. A new random key on each retry defeats it. Store the key and the effect in one transaction, so a crash cannot leave one without the other.
Sources
- Jayadeba Jena, Sanjay Dalal (2024). The Idempotency-Key HTTP Header Field.
- Martin Kleppmann (2017). Designing Data-Intensive Applications.