How long should you keep idempotency keys, and what goes wrong if you drop them too soon?
Sketch your own answer first, then compare it with the one below.
A strong answer
Keep a key at least as long as the longest period a client can retry the same operation, plus the time a request can sit in a queue. A common choice is a day.
If the key expires before a late retry arrives, the retry looks new and runs again. That is a duplicate. A key table that grows without end costs storage and index size, so expiry is needed, and the lifetime is a trade between table size and how late a retry can safely be.
Also decide what a repeat does while the first request is still running. Return a conflict, or wait for it, but do not run it twice.
Sources
- Jayadeba Jena, Sanjay Dalal (2024). The Idempotency-Key HTTP Header Field.
- Martin Kleppmann (2017). Designing Data-Intensive Applications.