The gateway was down overnight. Payments that happened on their side never reached us by webhook, and in the morning the reconciliation job read that day's settlement report and closed whatever was missing. Up to here everything is correct — the job does exactly what it was built to do.
Then at eleven, the gateway redelivers a webhook that was never answered. And one payment gets recorded twice: once by the job, once by the webhook.
Not because either side is broken. The webhook path is already idempotent — it is the answer from the previous lab, complete with a per-event claim and a stored response. The job is also right for the question it answers. There is exactly one problem: they use different keys, and neither of them knows the other path exists.
| initial state | |
|---|---|
| 3 webhooks that night, then the morning reconciliation job | 6 payments become 9 rows — 3 of them have two rows, one per path |
| the late webhook finally arrives | 6 payments become 12 rows; balance 665,000 against the 220,000 it should be |
| the reconciliation job run twice | 6 new rows each time, and the job reports inserted=6 — not 0 |
| two job runs at the same time | 12 rows for 6 payments |
| a settlement row whose amount differs from what is recorded | 2 rows for 1 payment, and mismatched stays empty |
Note the third row. The job does not just write duplicates — it reports that it wrote six new rows, confidently, for a day that is actually closed.
In my first job the second path was not a reconciliation job but a callback to a different storefront backend — and the shape of the problem was identical: one fact, two paths, two keys that never meet. That story is here.
This is the second of the three angles I cleaned up there:
- It arrives more than once → the previous lab.
- It arrives from two paths → this lab.
- It arrives out of order → the one that broke my warehouse stock; different shape from this lab.
The root is one thing: the decision "has this been applied" is made from message arrival, in the application — not in the database, with a key and a time reference owned by the fact itself.
Key takeaways
Two paths that are each correct can still record one payment twice.
- The webhook path uses a key that belongs to its message; the reconciliation job mints its own — and the two never meet.
- A job run twice reports
inserted=6twice, confidently, for a day that is already closed. - The right key is not the unique key, it is the key owned by the fact rather than by the message.
- Two paths may need different policies when they find something odd — the callers waiting on them are not the same.
The lab's mission
Three conditions:
- One payment, one row. From any path, and no matter how many times a path is called.
- The job can run again, and its report is honest. A second run for the same day adds no rows
and reports
inserted=0. A run that only got partway can be continued whenever. Two overlapping runs must not double either. - An amount that is already recorded does not change quietly. If the settlement report says a different amount, the row stays one, the amount stays the first one, and the difference is reported — not swallowed.
Why this is worth trying
In the previous lab, "the payment" and "the message announcing the payment" always arrived as a pair, so the difference never mattered. As soon as a second path exists, that difference becomes the most important thing in the system: the right key is not the unique key, it is the key owned by the fact — not by the message.
And the subtler second half: two paths that are each correct can need different policies when they find something odd. A webhook may answer 409 for one weird event; a reconciliation job must not die because one report line does not match.
If you want to try it
git clone https://github.com/izzudd/inva-lab.git
cd inva-lab/0003.reconciliation
make up # database + API + fake gateway
make demo # the whole story: that night's webhooks, the morning job, the late webhook
make check # target: 24 checks, all green
The gateway is fake and its reports are generated from the date, so any date works:
make demo DAY=2024-03-17.
The questions
The reconciliation job is the second path that receives one fact through a different key. Before touching code, answer this: which key comes from the payment, and which key comes from the message announcing it? And second: why must the job mint its own key at all, when the webhook path already has a unique one?
The full answer, including why removing the run id from the key also removes the need for a cursor, is in the answer article.