October 11, 2026•4 min read

One Payment, Four Ledger Rows: An Idempotency Lab

The code is not wrong and the gateway is not wrong, yet one payment shows up four times in the ledger. Guess why, then make make check green in the lab.

Read in Indonesian

Lab repo

The lab lives in folder 0002.idempotency

The deliberately broken code, the dataset, and the target live in a public repo. Everything runs in Docker. make check

A merchant complains: the balance went up, but not by the right amount. In the statement, one payment of twenty-five thousand appears four times. The balance rose to four times what it should be.

You open the code. POST /webhooks/payments — one request in, one ledger row out, balance incremented. No suspicious try/except, no loop, no bug. If you test by sending the same request twice, you get two rows — but you are the one who sent it twice, so that is expected.

I met a cousin of this problem in my first job: not a doubled balance, but warehouse stock that fell a few steps behind, and the item sold out before the number caught up. Different shape, same root — which is why this lab exists. That story is here.

What you cannot see: HTTP has no way to tell the sender that our answer arrived. The gateway sends the event, we commit the transaction, then the answer is lost on the way back. The connection dropped, the load balancer timed out, our container restarted a second too early. From our side everything is fine. From the gateway's side the event was never received, so it sends again.

That is not a broken gateway. It is the only option it has, and it chooses duplicate over lost — because losing a payment costs more than repeating one. The duplicate becomes your work.

This is what happens when you run the lab:

initial state
one event redelivered 4× 4 rows for 1 event, balance up 4× the amount
one event delivered 30× at once 30 rows, and the balance only partly rose (40,000–60,000 out of the 300,000 it should be)
3 different events, each delivered 2× 6 rows, balance up 6× the amount
the exact same request sent 2× 2 rows — nothing in the database can tell which one was already processed

That second row deserves a second read. Twenty rows landed in the ledger but the balance only rose twice — which means there are two different problems in one endpoint, and the second one is not about idempotency at all.

The same event can reach your system in three ways, and I have lived all three:

  1. It arrives more than once. The gateway redelivers because the answer never got back. This lab is that one.
  2. It arrives from two paths. A late webhook, plus a reconciliation job that already closed the day. That angle is the other lab.
  3. It arrives out of order. A later update carries older data, and the older data wins. That is what I hit with warehouse stock, and it looks different from the two above.

The root is one thing: the decision "has this event already been applied" is made from message arrival, in the application. Your app is not the one entitled to decide, and arrival order is not the reference.

Key takeaways

Four ledger rows for one payment, and no bug in the code.

  • HTTP cannot tell the sender that our answer arrived, so redelivery is normal, not anyone's mistake.
  • The gateway chooses duplicate over lost; the duplicate becomes your work.
  • Twenty rows landed in the ledger while the balance only rose twice: two different problems in one endpoint.
  • The question is not "where is the bug", it is who gets to decide that a payment was already received.

The lab's mission

Four conditions, and make check verifies all four:

  1. Exactly once. One event is applied exactly once. Not doubled, and nothing lost: three different events stay three ledger rows, not six, not two.
  2. A redelivery gets the same answer as the first. Not an error, not a "already processed" message — the same body, with the same entry_id. The reason is not politeness: an answer recomputed from the database as it is now can be wrong, because other events have moved the balance since.
  3. A reused event_id with different content is not processed. A different amount for the same event must not reach the ledger.
  4. The balance stays correct when many events arrive at once. Thirty different events arriving together must raise the balance thirty times the amount, not twice.

If you want to try it

The lab is a public repo and everything runs in Docker — no need to install Postgres, Python, or anything:

BASH
git clone https://github.com/izzudd/inva-lab.git
cd inva-lab/0002.idempotency
make up        # database + API
make replay    # replay one event, the way the gateway does
make check     # target: 21 checks, all green

There is a HINTS.md with three levels, and one rule: do not open the solusi branch before you are genuinely stuck.

The questions

Before you add any guard, answer this: why does the gateway redeliver when the first request was answered 200?

If that is not clear, every patch you write only covers the symptom. And once it is clear, a more interesting question appears: if redelivery is normal and nobody's fault, who should decide that this payment was already received — your app, or the database?

The long version, including why adding UNIQUE alone creates a new problem, is in the answer article.

Get better at shipping code.

Join the other developers getting tech updates, tips, and relaxed tutorials in their inbox every week.

Stay in the loop

Tech updates, coding tips, and relaxed insights straight to your inbox every week.

No spam. Unsubscribe anytime.

Read next

See all

For developers who learn by struggling

© 2026 Inva.dev.