The double charge on retry
A customer pays ₹2,000. The charge goes through, but the response never reaches their phone, so the app retries. How much did they pay?
In 30 seconds
- The problem
- Customers get charged twice for one order, and nothing in the logs looks wrong.
- Why it happens
- The response is lost on the way to the phone, the app retries, and the server can't tell a retry from a new payment. It charges again.
- The fix
- An idempotency key per payment, claimed with INSERT … ON CONFLICT DO NOTHING before the charge. A retry gets the stored result back.
- In an interview
- A lost response looks exactly like a failed payment, so retries will happen. Give every payment an idempotency key the client reuses on retry, claim it in one INSERT before calling the bank, and answer a retry from what you stored.
Why it matters
Every step of the naive version is right: charge, save, respond. It passes every test, because in tests the response always arrives.
In our run, 50 customers paid ₹2,000 at once and a flaky network dropped every 5th response. The 10 apps that retried got their customers charged twice: ₹20,000 taken that nobody owed. With an idempotency key, the same 10 retries got the stored PAID back, and every customer paid exactly once.
Words you need
- Idempotent
- Safe to repeat. A retried request ends in the same state as the first attempt, instead of an error or a second effect.
- Idempotency key
- A unique id the client creates once per payment and sends with every retry of it. The server uses it to recognise a retry and return the first result instead of charging again.
- Timeout
- Giving up on waiting for a reply. It tells you the reply did not arrive in time, not whether the work was done.
- Unique index
- A database rule that rejects a second row with the same value. A safety net: it turns a silent duplicate into an error.
Picture it
Priya pays ₹2,000. The bank charges her card, but the response is lost on the mobile network.
Charge, save, respond
- POST /pay → bank charges ₹2,000
- Response lost on the way to the phone
- App sees an error, retries
- Server charges ₹2,000 again
₹4,000 taken for one order. "Failed dikha, paise do baar kate."
Idempotency key, claimed first
- POST /pay with key k1 → key claimed
- Bank charges ₹2,000, response lost
- App retries with the same k1
- Key already claimed → stored PAID returned
Charged once. The retry is free and safe.
The fixes people try
No key
Failscharge(); insert(); respond()A retry after a lost response is a second charge.
Don't retry in the app
Failsretries: 0The customer taps Pay again. Same double charge, now from a human.
One payment per order
Half a fixUNIQUE (order_id)Stops this case, but breaks part payments and a real retry after a declined card.
SELECT the key, then charge
Half a fixif (!exists(key)) charge()Works one request at a time. Two retries arriving together both find nothing. That's the next trap.
Claim the key first
WorksINSERT … ON CONFLICT DO NOTHINGThe unique index makes the claim atomic. A retry finds the key and gets the stored answer.
The fix and its cost
The app generates a key once per payment, a UUID, saves it before sending, and reuses it on every retry. A new key means a new payment.
The server claims the key with one INSERT … ON CONFLICT DO NOTHING before calling the bank. Zero rows back means it's a retry: read the stored row and return the same answer. Then pass the same key to the processor, so even a retry that slips past your database can't charge twice where the money moves.
This is the core of Airbnb's Orpheus: record the request and its key before the network call, record the result after.
What it costs
Interviewers also listen for: who makes the key
Check yourself
1Why can't the server just notice the retry?Show answerHide
Without a key, a retry is byte-for-byte a new payment request. Same order, same amount, nothing to tell them apart.
2Why claim the key before calling the bank, not after?Show answerHide
If you save the key after the charge, a retry that arrives in between finds nothing and charges again.
3Why also send the key to the payment processor?Show answerHide
Defence in depth. If anything slips past your database, the processor still returns the first charge instead of making a new one.
The code
pay.bug.ts
Example code. The tested version, with a proof test against real Postgres, lands in the repo.
Star the repo for the runnable version
Each trap in this world ships with the bug, the fix and a proof test that fires concurrent requests at a real database.
Remember
A lost response looks like a failed payment. Give every payment a key, claim it before the charge, answer retries from what you stored.