Let’s talk

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

  1. POST /pay → bank charges ₹2,000
  2. Response lost on the way to the phone
  3. App sees an error, retries
  4. Server charges ₹2,000 again

₹4,000 taken for one order. "Failed dikha, paise do baar kate."

Idempotency key, claimed first

  1. POST /pay with key k1 → key claimed
  2. Bank charges ₹2,000, response lost
  3. App retries with the same k1
  4. Key already claimed → stored PAID returned

Charged once. The retry is free and safe.

The fixes people try

  • No key

    Fails
    charge(); insert(); respond()

    A retry after a lost response is a second charge.

  • Don't retry in the app

    Fails
    retries: 0

    The customer taps Pay again. Same double charge, now from a human.

  • One payment per order

    Half a fix
    UNIQUE (order_id)

    Stops this case, but breaks part payments and a real retry after a declined card.

  • SELECT the key, then charge

    Half a fix
    if (!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

    Works
    INSERT … ON CONFLICT DO NOTHING

    The 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

One unique column, one extra write per payment, and a cleanup job for old keys. The app has to keep the key until it gets an answer.

Interviewers also listen for: who makes the key

The client, once per payment, before the first attempt. If the server made it, a retry would arrive without it and look new.

Check yourself

  1. 1Why can't the server just notice the retry?Show answer

    Without a key, a retry is byte-for-byte a new payment request. Same order, same amount, nothing to tell them apart.

  2. 2Why claim the key before calling the bank, not after?Show answer

    If you save the key after the charge, a retry that arrives in between finds nothing and charges again.

  3. 3Why also send the key to the payment processor?Show answer

    Defence in depth. If anything slips past your database, the processor still returns the first charge instead of making a new one.

The code

GitHub

pay.bug.ts

import type { Pool } from "pg";

// POST /pay. Charge, save, respond. Every step is right.
export async function pay(db: Pool, bank: Bank, req: PayRequest) {
  const charge = await bank.charge(req.orderId, req.amount);
  await db.query(
    "INSERT INTO payments (order_id, amount, status, charge_id) VALUES ($1, $2, 'succeeded', $3)",
    [req.orderId, req.amount, charge.id],
  );
  // If this response is lost on the way to the phone, the app retries,
  // and the retry runs every line above again: a second charge.
  return { status: "PAID", chargeId: charge.id };
}

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.