Key takeaways
The claim has to be about the payment (charge_id), not about its message (event_id).
- One path with its own claims table equals two truths; what has to be unified is the key, not the tool.
- The webhook returns the first response; the job skips matches and reports mismatches instead of overwriting them.
- Dropping the run id from the key also drops the need for a cursor: replaying a whole day becomes safe.
Measurements
Every number here comes from a measurement run, not an estimate: a clean database per variant. The suite has 24 checks.
| variant | red | note |
|---|---|---|
| initial state | 14 of 24 | the webhook path's key (event_id) and the job's key (recon:<run>:<charge>) never meet |
the claim moved to charge_id, but the job keeps its own claims table |
13 of 24 | the failure shape changes: now the job hits the unique constraint in the ledger and answers 500 |
| an amount already recorded is overwritten by the report | 3 of 24 | still one row, but the correct amount is gone without a trace |
| per-payment claims on both paths, two policies, no cursor | 0 of 24 | — |
Concrete numbers from make demo at the initial state (six payments worth 220,000): 12 rows for 6
payments, balance 665,000. On the solusi branch: one row per payment, exact balance.
1. Every part is right on its own. That is the problem.
The webhook path is the previous lab's answer, and it stays right — for the question "have I received this message before?"
The reconciliation path is also right for the question it answers: "has this run processed this
report line?" Because the run id is always new, the answer is always "no" — and that is where the
bug lives. A job run twice reports inserted=6 twice, proudly.
What this system never had: one place that can answer "have I recorded this payment before?" Two paths, two tables, two truths.
2. The key moves from the message to the fact
before: processed_events(event_id PK) one message, one claim
after: payment_claims(charge_id PK) one payment, one claim
charge_id is the same key for both paths, because both are talking about the same payment.
event_id is still stored — in the ledger and on the claim row — but its role changed: it is a
trace, not the arbiter.
The follow-on in the database: the unique constraint in ledger_entries moves from event_id to
charge_id, because it is the payment that must not happen twice. In production that is a
migration to do carefully: clean up the old duplicates first — and that is a business decision, not
a SQL decision — then install the constraint.
3. Two paths, two policies
| claim exists, same content | claim exists, different amount | |
|---|---|---|
| webhook | return the first response (200) | 409 |
| reconciliation job | skip (skipped) |
report in mismatched, write nothing |
The reason is who is waiting. At the end of the webhook path there is an HTTP connection waiting for an answer: it deserves the same answer, and if the content contradicts, 409 is the right one. At the end of the reconciliation path there is a job sweeping one day of reports: if one bad line fails the whole day, the job never finishes — and tomorrow morning the same line fails it again.
4. The cursor was a symptom, not a requirement
Once the claim is per payment, there is nothing left to record about "how far we got". Replaying a whole day is safe, two concurrent runs are safe (whoever loses the claim skips the row), and a run that only managed two rows can be continued by the next one. That is why the three checks about repeated, overlapping and interrupted runs go green without an extra line of code.
The sentence "the key belongs to the fact, not the message" only landed for me after being bitten twice. The second time cost more, because by then I already believed the system was safe.
Traps left in on purpose
Giving the second path its own table. It looks tidiest: the webhook path is untouched, the job has its own table, nothing collides. What actually happens is measurable: 13 of 24 still red, and the failure shape changes — now the job hits the unique constraint in the ledger and answers 500.
Treating the settlement report as the truth. In the "recorded amount overwritten by the report" variant: still one row, but the amount that actually happened is gone without a trace, and the balance is off by a thousand. If you decide the report is more authoritative, that decision is allowed — but it has to be deliberate, and the difference still has to be recorded.
Failing the job so it "looks" right. A job that throws on the first bad report line stops there forever, and that day's report never completes.
Questions for you
- This job runs every morning for yesterday. If the gateway only sends that day's report three days later, what should change — the job's code, or the schedule?
- If a charge can have two different facts (a refund after the payment), what is the right key, and
why is
charge_idalone not enough? - The webhook answers 409 for a contradicting amount, the job reports it. Which is better for the user: an automatic adjustment row, or a daily report a human has to read?
- If you delete the run-history table entirely, what does this system lose? Answer before you delete it.