Bugpit

Lessons › Big architectures

Payouts: every cent accounted for

A marketplace that pays its sellers: 2,000 req/s, and 4% are payments, 80 a second, charged by the Payment API while the buyer waits. Each balance is one number, overwritten on every payment. Finance says the books are $41,000 off this month, and nobody can say where it went.

Play this level

Part of Bugpit Pro. It runs in your browser: build the design, press Run, and watch what breaks.

The lesson

Never keep money as one number you overwrite. A double-entry ledger saves each payment as entries that add up to zero: $40 out of the buyer's account and $40 into the seller's. A balance is the sum of an account's entries, so every cent leaves a trail.

Some calls to the provider time out, and you can't tell if the charge went through. If you retry, the buyer may pay twice. Send an idempotency key with each charge: the provider sees the same key and returns the first answer instead of charging again.

A timeout can also mean the provider took the money and your database never heard. Only checking finds it. Every night a Scheduler starts a Worker that reads the provider's report and compares it with your ledger, line by line. That's reconciliation.

Balances must be current, or a seller can withdraw the same money twice. The replica that answers balances has to wait for every write.

What you have to achieve

2,000 requests a second at the busiest hour, 4% of them writes.

What you learn

Every payment now leaves a trail that adds up, a retry can't charge twice, and each morning you know which charges didn't match. None of it made anything faster: the ledger writes twice the rows and the replica waits. Payment teams add one more piece: an outbox, so the event that sends a receipt is saved in the same transaction and can't be lost halfway.

Check yourself

Ana pays Ben $40, saved in a double-entry ledger. What do the entries add up to?

Show the answer

$0. $40 out of Ana's account and $40 into Ben's: minus 40 plus 40. If the entries ever don't add up to zero, you know something is wrong.

A charge times out and you don't know if it went through. What's the safe next step?

Show the answer

Retry with the same idempotency key. With the same key, the provider returns the first result if the charge already happened, and charges once if it didn't.

The nightly check finds a charge at the provider that isn't in your ledger. What happened?

Show the answer

A call timed out after the money was taken, and your app never saved it. That's what reconciliation is for. The fix is to record the charge, or refund it, so both sides match again.

Your app saves a payment, then crashes before sending the event that emails a receipt. What stops that receipt being lost?

Show the answer

An outbox: save the event in the same transaction, and a worker sends it after. The payment and the event are saved together or not at all. A worker reads the outbox table and sends what's waiting, so a crash can't lose it.

Try it yourself

More in Big architectures

← Watch next: a row for every person How a request travels →