NewWork mode: your Claude Code or Codex on work calls, answering from your project's codeSkip to content
Middle+0 views

Where do we store the idempotency key, and how do we process it?

Practice

AI answer

The client usually sends the idempotency key in a request header, such as Idempotency-Key, and the server stores it in durable storage—often a database or Redis with a TTL. The record associates the key with a user or scope, a hash of the request parameters, the processing status, and the result.

For the first request, the server atomically reserves the key, performs the operation, and saves the result. A retry with the same key and the same parameters receives the saved result rather than executing the operation again. If the key is reused with different parameters, the server should return an error. Concurrent requests must be synchronized using a unique constraint or an atomic operation; for processing that has not completed, the server can return an “in progress” status or safely retry after a timeout. Choose the TTL based on the retry window, and for financial operations, the record is often retained for longer.