HTTP Idempotency Keys and Safe Request Retries
An HTTP idempotency key is a client-generated unique value attached to a request so the server can recognize later attempts as the same operation. The server records the operation's first result and returns that result again when the same key is retried.
The problem: one operation, several requests
Networks fail in inconvenient places. A client might send POST /payments, the server might charge the card, and then the connection could break before the client receives the response. From the client's point of view, the request failed. Retrying it may charge the card twice.
This is especially dangerous for operations that create or change something. HTTP GET is intended to be safe to repeat, and PUT is generally idempotent: repeating it should leave the resource in the same state as doing it once. But POST commonly means “perform this new operation,” so two identical POST requests can create two orders or make two payments.
An idempotency key adds an application-level identity to the operation. The client generates a key such as a UUID and sends it with the request, often in an Idempotency-Key header:
POST /payments
Idempotency-Key: 8f4b1c2e-...
Content-Type: application/json
{"amount": 2500, "currency": "USD", "card_token": "tok_..."}
The key does not make the network reliable and does not make arbitrary code execute exactly once. It gives the server a durable way to connect retries to an earlier attempt.
What the server stores
For each key, the server needs a record in durable storage, such as a database. A typical record contains:
- the key and the scope in which it is unique, such as the account or API client;
- a fingerprint of the request, meaning a recorded representation of its important inputs;
- the operation's status, such as processing, succeeded, or failed;
- the HTTP status, response body, and sometimes relevant response headers from the first completed attempt;
- timestamps for creation and expiration.
The fingerprint matters because a key should not silently identify two different operations. If a client first uses a key to pay 25 dollars and later reuses it to pay 50 dollars, the server should reject the second request, commonly with a conflict response, rather than return an unrelated payment result.
The server also needs a uniqueness constraint on the key and its scope. For example, two customers may both use the same UUID without colliding if keys are scoped to the customer. A globally unique key is simpler, but the scope must be explicit in the design.
The normal request flow
A useful implementation proceeds like this:
- Read the key and validate it. Requests for operations that require deduplication should usually be rejected if the key is missing.
- Atomically create a record for the key with status
processing. “Atomically” means that competing requests cannot both successfully claim the same new key. - Compare the incoming request with the stored fingerprint. A matching key with different inputs is an error.
- If this request created the record, perform the operation.
- In a durable transaction, or through an equivalent reliable arrangement, store the final status and response.
- Return that response to the caller.
On a later request with the same key, the server looks up the record instead of performing the operation again. If the first attempt completed, it returns the stored status and body. This is the important distinction between an idempotency key and merely checking whether a resource already exists: the server is deliberately replaying the first operation's result.
A replay may therefore return an error. If the first attempt completed with a validation error or a business-rule failure, that result can be the result associated with the key. Whether a service stores client errors, server errors, or only successful outcomes is a policy choice, but it must be consistent and documented. Re-running after a transient failure can be useful; re-running after a side effect has already happened is dangerous.
What happens when requests race
Suppose the original request is slow and the client retries before it finishes. Both requests find no completed record. Without an atomic claim, both can begin the payment, defeating the mechanism.
With a uniqueness constraint or an equivalent atomic insert, one request creates the processing record and becomes the owner. The other discovers that the key is already present. The service can then:
- wait for the owner to finish and replay its result;
- return a response such as “still processing,” asking the client to retry later; or
- return a conflict that tells the client a request with this key is in progress.
The choice depends on latency and API conventions. What matters is that the losing request does not run the operation independently. A lock held only in one application process is insufficient: another server instance must observe the same state through shared durable storage.
When the response is lost
The most valuable case is also the one that causes the incident report: the server finishes the operation, but the response disappears. The client retries with the same key. The server finds the completed record and returns the original result, so the client learns the payment or order's outcome without creating another one.
This works only if the result was saved durably before the server considered the operation complete. There is still a difficult crash window if the external side effect happens first and the idempotency record is saved afterward. A process crash in that gap can leave the server unable to tell whether the effect occurred. Reliable designs reduce this window by making the idempotency record and local business change part of one database transaction, or by using an operation-specific mechanism such as a provider-side idempotency key, an outbox, or reconciliation.
An idempotency key is therefore not a universal exactly-once guarantee. It protects retries only to the extent that the server can durably associate the key with the side effect and its result.
Expiration and operational rules
Records cannot necessarily live forever, so services usually assign a retention period. A retry after expiration may be treated as a new request; clients must retry within the documented window. The period should cover realistic network delays and client retry behavior.
Keys should be unpredictable enough that one customer cannot guess another customer's operation, and lookup must still enforce authentication and authorization. A key is not permission to retrieve any result.
The next time a log says “duplicate idempotency key,” “request already in progress,” or “idempotency key reused with different parameters,” it is describing this state machine: claim one operation, preserve its outcome, and make every later attempt refer back to that same outcome.