BitlystBitlyst
PostsSeriesTopicsAbout

Bitlyst

Short, diagram-first posts on JavaScript, React & Next.js. Follow via RSS or the newsletter.

© 2026 Mohsen Fallahnejad

PostsSeriesTopicsLinksAboutSync devicesRSSllms.txtGitHubX
Posts/system-design
AI-generated illustration. Press the button five times, and one lift still comes.
Intermediate5 min read

Part 1 of Engineering

Idempotency: Why a Retried Payment Can't Charge Twice

Idempotency means doing something twice has the same effect as doing it once. Learn how idempotency keys make retries safe, and why PUT is idempotent but POST isn't.

Mohsen Fallahnejad

Mohsen Fallahnejad

Nov 25, 2026Discuss

Picture a user tapping Pay on a train with bad signal. The request reaches your server, the card is charged, and then the response gets lost on the way back. The app sees a timeout and does the sensible thing: it retries.

Without idempotency, that retry charges the card a second time.

The mental model#

Pressing the lift button five times still calls one lift.

An operation is idempotent when doing it twice has the same effect as doing it once. The first press changes the world. Every press after that changes nothing, because the world is already in that state.

How it works#

Some operations are idempotent by nature. "Set the email to a@b.com" gives the same result no matter how many times it runs. "Delete order 42" leaves order 42 gone, whether it ran once or three times.

Others are not. "Add $20 to the balance" or "create a new order" changes the world every time it runs. Those are the ones that hurt when a request is retried.

The client can't tell the difference between "the request never arrived" and "it arrived, but the answer got lost". Both look like a timeout. So retrying is the only safe move for the client, and that means the server has to make retries harmless.

The usual fix is an idempotency key: a unique ID the client generates once per action and sends with every attempt.

js
const key = crypto.randomUUID(); // created once, when the user taps Pay

await fetch("/api/payments", {
  method: "POST",
  headers: { "Idempotency-Key": key }, // same key on every retry
  body: JSON.stringify({ amount: 2000 }),
});

On the server:

  1. Look up the key. Seen it before? Return the saved response. Don't charge again.
  2. New key? Do the work, then save the key together with the response.
  3. Same key, different body? Reject it. That's a bug in the client, not a retry.

A request with a tag gets one receipt; the copy with the same tag gets that same receipt backA request with a tag gets one receipt; the copy with the same tag gets that same receipt back The key is the tag. Same tag, same receipt: the second request gets the answer the first one already earned.

HTTP methods: PUT vs POST#

HTTP builds the idea into its methods:

  • GET, PUT, DELETE are idempotent by spec. PUT /users/7 with the same body sets the same state every time.
  • POST is not. POST /orders means "create one more", so two calls make two orders.
  • PATCH depends on the patch. "Set name to Sam" is idempotent; "increment count" is not.

That's why browsers warn before re-sending a form, and why payment APIs such as Stripe accept an Idempotency-Key header on their POST endpoints.

In practice#

You need this anywhere a duplicate costs money or trust: payments, orders, sending emails or SMS, and message-queue consumers. Most queues deliver at least once, so your handler will see the same message twice sooner or later.

You mostly don't need it for reads, or for writes that already set a value instead of adding to it.

Two details matter. Generate the key once per user action, not once per attempt, or every retry looks new. And on the frontend, disabling the button after the first tap helps, but it is not a fix: the network can still retry for you.

Say it in an interview: "An idempotent operation has the same effect whether it runs once or many times. Networks fail after the server has done the work, so clients must retry, and the server has to make those retries safe. For non-idempotent calls like payments, the client sends an idempotency key, and the server stores it with the result and returns the saved result on a repeat."

Cheat sheet#

QuestionAnswer
Idempotent meansOnce or ten times, same effect
Why it mattersTimeouts force retries; retries must be harmless
The fix for POSTClient sends an Idempotency-Key; server stores key + response
Idempotent methodsGET, PUT, DELETE (POST is not)
Key createdOnce per user action, reused on every retry

TL;DR: you can't stop retries, so make them harmless. Same key, same result, one charge.

Advanced · optional

Going deeper#

Everything above is enough to explain this in an interview. This part is for the curious: full numbers, edge cases and what's still unproven.

Two retries at the same moment#

"Look up, then do the work" has a race: two copies can both miss the lookup and both charge. Claim the key first with an atomic write, such as a unique constraint in the database or Redis SET key value NX, and only the winner does the work. The loser waits for the result or gets a 409 Conflict.

Store the key with the work#

If the charge commits but saving the key fails, the next retry charges again. Keep the key and the side effect in the same database transaction when you can. When the work calls an outside service, pass your key along so that service deduplicates too.

How long to keep keys#

Keys only need to outlive the retry window. Stripe, for example, keeps them for at least 24 hours. Expire them after that so the table doesn't grow forever.

Idempotent is not the same as safe#

In HTTP terms, a safe method doesn't change anything (GET, HEAD). DELETE is idempotent but not safe: the first call changes the world, the rest don't. Also, the response can differ: a second DELETE may return 404, and that's still idempotent, because the state is the same.

Filed under#system-design#api#http#payments#interview

Series

Engineering

Ideas behind reliable systems: idempotency, flags, indexes, and the vocabulary seniors expect in design interviews.

  1. 1Idempotency: Why a Retried Payment Can't Charge Twice · you are here
  2. 2Deploy is not release · coming soon
  3. 3One trip to the shop with a list · coming soon

New parts land here first — subscribe below to get them in your inbox.

Was this worth your time?

One tap tells me what to write more of.

Mohsen Fallahnejad

Written by

Mohsen Fallahnejad

Frontend engineer explaining JavaScript, React & Next.js internals in posts you can finish over coffee. More about me

X ↗GitHub ↗LinkedIn ↗
← OlderforEach and async/await: Why Your Loop Doesn't Wait

Start the discussion

Questions, corrections, a better approach? I read and reply to every comment.

Read next

CORS Explained Simply — Why Your Fetch Works in Postman but Fails in the Browser

Cross-Origin Resource Sharing in plain language: what blocks your request, what preflight means, and how to fix the errors you'll see in every frontend job.

Jul 8, 20266 min read

forEach and async/await: Why Your Loop Doesn't Wait

forEach with async/await doesn't wait. It starts every callback and returns undefined, so your code moves on early and errors skip your try/catch. Use for...of or Promise.all.

Nov 23, 20264 min read

What Runs First in a Next.js Page: Server Components, Render, Effects

Server first, browser second. Render goes down the tree, effects come back up. The order of every hook from request to state update, measured.

Nov 21, 20266 min read

Get new posts in your inbox

One email when a new post is out: short, diagram-first explanations of JavaScript, React and Next.js. No spam, unsubscribe any time.

5 min left
Discuss👍 React
👍