Programming
We provide developers and engineers the guides and tutorials they...
Search
We provide developers and engineers the guides and tutorials they...
Content is king. It's not just about how you're going to write, b...
Looking for an engaging connection with thousands of other people...
How idempotency keys stop duplicate charges: why retries are unavoidable, how servers store and replay results, and how to design idempotent APIs.
Quick answer: An operation is idempotent if doing it several times has the same effect as doing it once. Payments are not naturally idempotent: sending "charge £50" twice charges £100. Payment APIs fix this with an idempotency key: the client generates a unique ID for each intended payment and sends it with the request. The server records the key along with the result. If the same key arrives again, because of a retry, a timeout or a double click, the server does not charge again. It returns the saved result of the first attempt.
You tap "Pay". The app sends the request. Then one of these occurs:
From the app's side, all three look identical: a timeout. It cannot tell whether the payment happened.
This uncertainty cannot be removed. It is a basic property of networks, illustrated by the Two Generals' Problem. And retries come from everywhere: impatient users pressing the button again, automatic retries in HTTP libraries and proxies, and message queues that deliver at least once.
Since you cannot avoid retries, you make retries safe.
| Operation | Idempotent? |
|---|---|
| "Set the balance to £100" | Yes |
| "Add £10 to the balance" | No |
| "Delete order 42" | Yes |
| "Create a new order" | No |
HTTP defines this for its methods in RFC 9110:
| Method | Idempotent by definition |
|---|---|
| GET, HEAD | Yes (and safe: no changes) |
| PUT | Yes: replace with this state |
| DELETE | Yes |
| POST | No |
| PATCH | Not necessarily |
Creating a payment is a POST. So it needs something extra.
The pattern, popularised by Stripe and described in its post on designing APIs with idempotency:
POST /v1/payments
Idempotency-Key: 7c1f0b9e-3a52-4d0e-9a41-5d2f1e8b6c33
Content-Type: application/json
{ "amount": 5000, "currency": "gbp", "customer": "cus_123" }
The Idempotency-Key header is being standardised for this purpose.
The key identifies the customer's intent. A new intent ("buy this again") gets a new key. A retry of the same intent reuses the old one.
A payment service usually calls a card network or bank, which is itself an unreliable network call. If your server crashes after the bank charged the card but before you recorded it, a retry could charge again.
So idempotency must be carried all the way down: pass an idempotency key or unique reference to the downstream provider too. Then your retry to them is also safe.
Larger flows are modelled as a state machine with recorded steps (created, authorised, captured, completed). After a crash, a retry looks up how far the operation got and resumes from there instead of starting over.
UPDATE orders SET status = 'paid' WHERE id = 42 AND status = 'pending'.You cannot guarantee that a message is delivered exactly once over an unreliable network. What you can do is:
at-least-once delivery + idempotent processing = exactly-once effect
Retry until you get an answer, and make sure repeats are harmless. Every reliable payment, messaging and order system is built on this combination.
Even with all of this, payment companies do not rely on a single mechanism. They keep an append-only ledger of every money movement and regularly reconcile their records against bank and card network statements. Any mismatch is investigated. Idempotency prevents most duplicates; reconciliation catches whatever slipped through. Amounts are stored as integers in the smallest currency unit, never as floats; see why 0.1 + 0.2 is not 0.3.
A unique value the client attaches to a request so the server can recognise retries of the same operation and avoid performing it twice.
The client. Only the client knows whether two requests represent the same intent or two separate ones.
Not by default. An idempotency key makes a specific POST endpoint safe to retry.
A well-designed API rejects it with an error, because the saved result belongs to a different request.
Networks make it impossible to know whether a timed-out request succeeded, so retries are unavoidable. Idempotency keys make them harmless: the client names its intent, and the server performs each named intent once and replays the answer thereafter. It is a small pattern that every API changing money, inventory or anything else that matters should adopt.