Case study demo

Payment flow

Move a payment through its states and see why each step must be idempotent.

An interactive demo of how this system behaves. It runs in your browser.

Case study · Live model

How a payment flow behaves

A shop asks the payment service to take money. The service asks the bank to charge the card, then writes the payment in its ledger, which is its own list of money moved. Lose the bank's answer, or crash the service, and watch whether the two still agree.

Try it

Lab time 3.0 s
paychargerecordcharged?Shop checkout, 8 payments a secondShop checkout8 payments a second0 turned awayPayment service, sends a key each timePayment servicesends a key each time2 with no answer yetBank, took $627Banktook $62715 chargesLedger, shows $627Ledgershows $62715 paymentsReconciliation, every 5 sReconciliationevery 5 s0 put right
  • Shop checkout — 8 payments a second — healthy — 0 turned away
  • Payment service — sends a key each time — healthy — 2 with no answer yet
  • Bank — took $627 — healthy — 15 charges
  • Ledger — shows $627 — healthy — 15 payments
  • Reconciliation — every 5 s — healthy — 0 put right
  • A payment, or a charge sent to the bank
  • An answer
  • A row written in the ledger
  • Reconciliation asks the bank
  • An answer that was lost, or a payment turned away
Paid
15/ 17
payments charged and in the ledger
Charged twice
0
$0 taken that nobody owed
Taken with no record
0
$0 charged, no ledger row
Tried again
0
no answer in 1 s, so asked again
Put right
0
by the reconciliation job
Turned away
0
arrived while the service was down

Numbers add up:15 bank charges = 15 ledger rows + 0 charged twice + 0 with no row yet · $627 at the bank = $627 in the ledger + $0 charged twice + $0 with no row yetThe lab checks this sum as it runs, so a counting mistake would show here.

Controls

Payments and the bank
8 a second
10%
300 ms

The service waits 1 s for an answer, then asks again, up to 3 times in all.

Safety

One id for each payment, sent with every try. The bank remembers it and charges only the first time.

Every 5 s a job asks the bank about each payment with no answer for 3 s, and corrects the ledger.

Payments by state

StatePaymentsWhat it means
Charging2The bank was asked. The service has no answer yet.
Paid15The bank took the money, and the ledger shows it.
Declined0The bank said no. No money moved.
Unknown0No answer after 3 tries. Nobody knows whether the bank took the money.
Failed0Reconciliation asked the bank, and the bank had taken nothing.

The bank has taken $627 and the ledger shows $627. The two agree.

Money the bank has taken that the ledger does not show ($)
012.52537.5501s2s3s
Show data table
TimeBank ahead of ledger
1s0
1s0
2s0
2s11
3s44
3s0

What just happened

Nothing yet. Start traffic or run an experiment.

What this model simplifies
  • A payment here is one step: charge the card. Real card payments first hold the money, then take it later, and can be refunded.
  • The ledger has one row for each payment. A real ledger writes two rows for each move of money, one for each side, so that they sum to zero.
  • The service asks at once when 1 s have passed. Real services wait longer before each new try.
  • Reconciliation runs every 5 s and asks about one payment at a time. Real jobs compare a whole day's file from the bank, often hours later.
  • Reconciliation here repairs payments left without an answer. A real job also finds a second charge for one payment and gives the money back.
  • A crashed service is back after 2 s and its database keeps every payment row. Only what it held in memory is gone.