Idempotency-Key Header Behavior Configuration for JSON POST APIs
0 reputation · 09 Jan 2025, 22:07 UTC
0 reputation · 09 Jan 2025, 22:07 UTC
Goal: Define the server’s response when a JSON POST request includes an Idempotency-Key that matches a previously processed request, ensuring that retries do not cause duplicate writes while preserving clear client‑side error handling.
Uncertainty: Whether the server should signal a conflict with HTTP 409, silently replay the prior response, or convey duplication through a custom header, especially considering client expectations, possible key collisions, and storage cleanup policies.
Should the server return 409 Conflict on a duplicate Idempotency-Key? Should it return the original response with a 200 status and an Idempotency-Replayed header? Should it treat the duplicate as a normal request when the key is missing or invalid?
29775 reputation · 10 Jan 2025, 03:05 UTC
When a JSON POST request contains an Idempotency-Key that the server has already processed, the recommended practice is to return the original successful response (HTTP 200 or 201) along with an Idempotency-Replayed header. A 409 Conflict should be used only when the duplicate request would create a resource that already exists and cannot be safely replayed. If the key is missing or fails validation, the server should reject the request with 400 Bad Request and should not treat it as a duplicate.
409 Conflict is appropriate only when the duplicate would result in a distinct error state (e.g., duplicate unique constraint) that cannot be recovered by replaying the previous result.400 Bad Request.If you currently return 409 Conflict for every duplicate key, you will cause unnecessary retries and may mislead clients into thinking the operation truly failed. Switching to a replay strategy reduces network traffic and aligns with client expectations for idempotent POSTs.
400 Bad Request.
Idempotency-Key to the stored response and status code.
Idempotency-Replayed: true.
409 Conflict with an error payload explaining the duplicate.
Do you require the server to treat a duplicate Idempotency-Key that would normally succeed but create a duplicate resource as a normal request (i.e., not return 409) or do you want to enforce strict uniqueness and return 409? This choice affects whether you should replay or reject duplicates in that specific scenario.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.