Series · @aymane.codedev
Everyday Bugs
I can explain what I build. Short videos on the bugs behind everyday tech.
EP.01
Pay once
idempotency
Why a payment should go through only once
You tap “Pay”, the network drops, the app retries. Did you just pay twice?
About 3,000 views on its first day on Instagram.
The fix: an idempotency key
The app makes one key per payment and sends it with every retry. The server remembers the key, so the second request returns the first result instead of charging again.
// Same key on every retry of this payment
const key = crypto.randomUUID();
await pay(order, { idempotencyKey: key });
// Server: answer a repeated key with the first result
const done = await payments.findByKey(key);
if (done) return done;
return payments.charge(order, key);Tap “Pay” and watch the retry
The network drops the first reply, so the app sends the payment again. Turn the idempotency key on and off to see what the server does with the second request.
Charged: 0 €
Episodes
- EP.01Pay onceIdempotency: why a retry should not charge you twice.
More episodes come here as they ship.