(Note · · 5 min read)
The customer pressed the button twice
A request that creates something — a payment, an order, a transfer — will eventually arrive twice. The connection drops after the work was done but before the reply, and the client retries in good faith. Disabling the button helps with the impatient customer and does nothing about the network.
By Md. Habibur Rahman Shohel, full-stack & AI product engineer in Dhaka
The duplicate usually succeeds
The dangerous case is not a failed request repeated. It is a successful one whose response never arrived. The client has no way to distinguish that from a genuine failure, so the correct client behaviour is to retry, and the correct server behaviour must therefore be to recognise the repeat.
That is why this cannot be solved in the interface. The browser is the one component guaranteed not to know what happened.
Let the client name the attempt
The client generates a unique key for the operation and sends it with the request, reusing the same key on every retry of that same attempt. The server stores it with the result. A second request carrying a key it has seen returns the stored result instead of doing the work again.
on request with key K:
if K already recorded -> return the stored response
insert K as in-progress (unique constraint does the work)
-> conflict means a concurrent duplicate: wait or reject
perform the operation
store the response against K, within the same transactionThe unique constraint is what makes this safe under concurrency. Two simultaneous duplicates both try to claim the key and exactly one wins, which is a guarantee the database gives you and application logic cannot.
Store the response, not just the fact
Recording only that a key was used leaves the retrying client with nothing useful. Store the response body and status so the repeat receives the same answer the original would have — the retry then behaves exactly like the call that was lost.
Commit the key and the work together. If the operation commits and the key does not, the next retry does it again, which is the specific failure you set out to prevent.
Scope the key and expire it
Keys belong to an account, not to the world, or one customer's key can collide with another's. Where the amount matters, record the request's essentials alongside and reject a reused key carrying different values — that is someone's bug, and failing loudly beats silently returning the wrong stored result.
Keep them long enough to cover realistic retries, then expire them. This is a short-term deduplication table, not a permanent record of every request ever made.
Use the provider's version too
When your server calls someone else — a payment provider in particular — the same problem exists one level down, and most such APIs accept an idempotency key for exactly this reason. Send one, derived from your own operation, so your retry does not become their duplicate charge.
Combined with receiving webhooks idempotently, that closes the loop: the duplicate cannot be created by your client, by your retry, or by the provider's at-least-once delivery.
Common questions
Why do duplicate requests happen even without double clicks?
Because a request can succeed while its response is lost. The client cannot distinguish that from a failure, so retrying is correct behaviour, and the server must recognise the repeat. No amount of interface work prevents it.
How does an idempotency key work?
The client generates a unique key per operation and reuses it on every retry of that attempt. The server records the key with the result, and a request carrying a key it has already seen returns the stored response instead of performing the work again.
What makes idempotency safe under concurrency?
A unique constraint on the key. Two simultaneous duplicates both attempt to claim it and exactly one succeeds — a guarantee the database provides that application-level checking cannot.
Should the stored idempotency record include the response?
Yes, the body and status, committed in the same transaction as the work. Recording only that a key was used leaves the retrying client with nothing useful, and committing the work without the key means the next retry repeats it.