Idempotency in HTTP APIs: Safe Retries with Idempotency-Key Headers
Prevent duplicate credit card charges and double-orders with Idempotency-Key headers in REST APIs. Architecture design and atomic storage rules.
By Cometflow API Engineering Team • Updated 2026-10-06
In HTTP semantics, GET, PUT, and DELETE are naturally idempotent, whereas POST is not. If a user submits a payment and their mobile connection drops before the response arrives, retrying a naive POST /v1/charges endpoint will charge their credit card twice unless an Idempotency-Key is enforced.
1. How Idempotency-Key Request Processing Works
POST /v1/payments HTTP/1.1
Host: api.example.com
Idempotency-Key: 7f9c2b10-4e8a-4b2a-9c1d-5e8f3a1b2c3d
Content-Type: application/json
{ "amountCents": 4900, "currency": "USD" }
- Client Generates UUID v4: The client creates a unique key per logical user action (not per network retry).
- Atomic Lock & Payload Hash Check: The server hashes the request body and checks if the key exists. If a retry sends the same key with a different amount, return
HTTP 422. - Cached Response Replay: If the key already succeeded within the 24-hour retention window, return the stored HTTP status and JSON body without re-executing the charge.
Frequently Asked Questions (FAQ)
What should happen if a duplicate request arrives while the first request is still in-flight?
Return HTTP 409 Conflict with a Retry-After: 2 header so the client waits briefly for the initial transaction to commit.