Resilience · Concept
Idempotency key
A unique key sent with a request so that, if it is retried, the server returns the first result instead of doing the work twice.
Stripe accepts an idempotency key as an HTTP header on every POST, and published why it built one. The problem is not that calls fail. It is that one particular failure is indistinguishable from success:
“The call could succeed, but the connection break before the server can tell its client about it.”Brandur Leach, Stripe
Your charge may have gone through. You have no way to know. Retrying risks charging twice; not retrying risks never charging at all.
A unique key the client sends with a request, so that if the request is retried the server recognizes the repeat and returns the first result instead of doing the work twice.
What it is
Two separate ideas, often confused. The first is a property an operation either has or doesn’t:
“A request method is considered “idempotent” if the intended effect on the server of multiple identical requests with that method is the same as the effect for a single such request.”HTTP Semantics, §9.2.2
The second is the mechanism that gives an operation that property when it doesn’t have it naturally. The client makes a key; the server uses it to spot the repeat:
“A client generates an idempotency key, which is a unique key that the server uses to recognize subsequent retries of the same request.”Stripe API reference
What the server does with it is the important half, and Stripe is specific:
“For API v1, Stripe saves the status code and body of the first request made with a given idempotency key after endpoint execution begins. Retries with the same key return the saved response, including `500` errors.”Stripe API reference
Read that last clause twice. The saved result is replayed even when the first attempt failed. An idempotency key does not get you a success. It gets you a single, stable answer.
Note the scope. Stripe documents that behavior for API v1. On API v2 it says retries do not repeat successful operations, and that Stripe instead drives failed or partly failed work to completion and returns an updated response. Check which version your integration is on before relying on replay.
The problem
HTTP already records which methods are safe to repeat, and the list does not include the one you use to create things:
“Of the request methods defined by this specification, PUT, DELETE, and safe request methods are idempotent.”HTTP Semantics, §9.2.2
That distinction exists for exactly our failure:
“Idempotent methods are distinguished because the request can be repeated automatically if a communication failure occurs before the client is able to read the server’s response.”HTTP Semantics, §9.2.2
So a client may safely auto-retry a PUT. For a POST, the specification says a client should not, unless one of two conditions holds:
“A client SHOULD NOT automatically retry a request with a non-idempotent method unless it has some means to know that the request semantics are actually idempotent, regardless of the method, or some means to detect that the original request was never applied.”HTTP Semantics, §9.2.2
An idempotency key is that means. Stripe says so in its own words:
“Including an idempotency key makes POST requests idempotent, which prompts the API to do the record keeping required to prevent duplicate operations.”Stripe docs
Without it, the cost lands on a person. Square describes the sequence from the caller’s side:
“However, your connection fails before you receive success confirmation from Square. You don’t know what happened to your request and therefore you make the request again, charging the customer twice.”Square
And it is not only about money. Amazon EC2 documents the same hazard for machines:
“However, if the original request and the subsequent retries are successful, the operation is completed multiple times. This means that you might create more resources than you intended.”Amazon EC2 API Reference
From first principles
- Why can’t the client just retry? Because a lost response and a failed call look identical from outside. Stripe: “the success of the operation is ambiguous from the perspective of the client, and it doesn’t know whether retrying the operation is safe.”
- Why does the client make the key, not the server? Because the key has to survive the thing that went wrong. A server-generated id arrives in the response, which is exactly what got lost. The client must already hold it before it asks.
- Why must the key be random? So two different operations never collide into one. Stripe: “we suggest using V4 UUIDs, or another random string with enough entropy to avoid collisions.”
- Why does the server check the parameters too, not just the key? Because the same key with a different body is a bug, not a retry, and silently replaying the old answer would hide it. Stripe’s layer “compares incoming parameters to those of the original request and errors if they’re not the same to prevent accidental misuse.”
- Why does the key expire? Because the saved results are storage that grows forever otherwise. The window is a real limit, not a formality: Stripe says “keys expire out of the system after 24 hours.”
The solution
The server keeps a record against the key and consults it before acting. Three cases, and the third is the one people miss.
The first request. The work runs, and the status code and body are saved against the key.
A retry after it finished. The saved answer is returned without redoing the work, and the client can tell this happened:
“To identify a previously executed response that’s being replayed from the server, look for the header Idempotent-Replayed: true.”Stripe docs
A duplicate that arrives while the first is still running. This one cannot be replayed, because there is nothing to replay yet. Stripe refuses it:
“The idempotency key provided is currently being used in another request. This occurs if your integration is making duplicate requests simultaneously.”Stripe, error codes
Powertools for AWS Lambda documents the same refusal, and gives the reason: it is a lock for correctness, because the result of the first call is not known yet, so a second concurrent run cannot be allowed safely.
There are also edges where the guarantee does not apply, and they are worth knowing before you rely on it:
“We save results only after the execution of an endpoint begins. If incoming parameters fail validation, or the request conflicts with another request that’s executing concurrently, we don’t save the idempotent result because no API endpoint initiates the execution. You can retry these requests.”Stripe API reference
Anything that runs before the idempotency layer is outside the promise:
“For example, a request that’s rate limited with a 429 can produce a different result with the same idempotency key because rate limiters run before the API’s idempotency layer.”Stripe docs
Patterns: four real implementations
The idea is standard; the details are not. Nothing here is guesswork, and the gaps are real gaps in those docs rather than gaps in our reading. As of October 2026:
| API | Where the key goes | Length limit | Validity window |
|---|---|---|---|
| Stripe | Idempotency-Key header | 255 characters | 24 hours |
| Adyen | idempotency-key header | 64 characters | 7 to 14 days |
| Amazon EC2 | ClientToken parameter | 64 ASCII characters | not documented on that page |
Square CreatePayment | idempotency_key body field | 45 characters | not documented on that page |
There is no RFC for the Idempotency-Key header, and the name varies between vendors.
EC2 shows the pattern outside payments, and names the failure precisely:
“If you retry a successful request using the same client token, but one or more of the parameters are different, other than the Region or Availability Zone, the retry fails with an IdempotentParameterMismatch error.”Amazon EC2 API Reference
An everyday picture
You hand in a form at a counter with your own reference number written on it. The clerk processes it and files the number. If you are not sure it went through and hand in the form again, the clerk finds your number already filed and hands back the same receipt, instead of processing you twice. The number is yours, written before you hand anything over, which is why it still works when the receipt goes missing.
Do
- Send a key on every POST that changes something.
- Use a v4 UUID or another random string with enough entropy.
- Retry with the same key and the same parameters.
- Check for the replay header before treating a success as new work.
- Generate a fresh key when you change the request.
Don’t
- Don’t put keys on GET or DELETE (Stripe: it “has no effect. These requests are idempotent by definition.”).
- Don’t use personal data as a key (Stripe warns against email addresses and personal identifiers).
- Don’t reuse one token across different API requests (AWS).
- Don’t assume one key protects you everywhere: Adyen does not check keys across regions (Adyen).
- Don’t auto-retry a non-idempotent method without a way to know it is safe (the HTTP specification).
Trade-off
The protection is not free, and the price is a new dependency on the request path. Adyen is unusually honest about it:
“To enable idempotent behavior, Adyen must retain a consistent, stateful data store to compare each request against. Idempotent processing relies on this data store being available.”Adyen Docs
A store on the request path is a store that can be down, and when it is, your choices are all worse than normal.
The window is the second cost. Stripe’s 24 hours sounds generous until a queued job retries on day two with the same key and gets treated as a brand new request.
How it fits with the rest
Backoff with jitter decides when to retry. An idempotency key decides whether retrying is safe at all. You need both, and Stripe’s own post moves straight from one to the other, recommending exponential backoff with randomness right after it describes idempotency keys. Without the key, the retry machinery in retry storm is not just wasteful, it is dangerous: every duplicate that lands is a duplicate charge.
Practise it in two minutes
Predict what happens, watch it move, change the numbers, and get tested again before you forget.
Open Idempotency key in the app →Related
Sources
- Stripe API reference, “Idempotent requests”
- Brandur Leach, “Designing robust and predictable APIs with idempotency”, Stripe, 22 February 2017
- Stripe docs, “Advanced error handling”
- Adyen Docs, “API idempotency”
- Amazon EC2 API Reference, “Ensuring idempotency in Amazon EC2 API requests”
- RFC 9110, HTTP Semantics, §9.2.2
- Stripe docs, “Error codes”
- Square, “Idempotency”
- Square, CreatePayment API reference
- Powertools for AWS Lambda (Python), “Idempotency”