Back to System Design Lab

5,998 of 6,000 items paid for, none recorded twice

The same flash sale as the replay, this time all the way to payment. Some buyers pay, some decline, some just walk away. The payment provider is a fake, built to misbehave: notifications arrive twice, forged, or late.

Second137
Bills created7,113
Paid5,998
Cancelled1,115
4×

Every bill settled

Every bill is settled: 5,998 paid, 1,115 cancelled, none left hanging. 2 items came back after buyers stopped arriving.

Bills per second
0120240Second 1Second 35Second 69Second 103Second 137createdpaidcancelled
  • created
  • paid
  • cancelled

Duplicate and forged notifications change nothing

The payment provider tells the shop through notifications. The shop trusts only notifications with a valid signature, and a bill can change status only once. A payment that arrives after the bill was cancelled is logged as a conflict, so the money can be refunded.

Duplicates ignored671
Forged, rejected1,288
Conflicts0

What buyers saw and what the shop recorded match exactly

The fake buyers (k6) log what happened to them. The shop logs what happened in Redis. Each is counted on its own, and both end on the same numbers.

Buyers (k6)

Paid
5,998
Declined
366
Walked away
749

Shop (Redis)

Paid
5,998
Cancelled
1,115
Still open
0

Books (Postgres)

Bills stored
7,113

When every payment lands in one account

In a one-item flash sale, every payment is booked to the same revenue account. We measured 20,000 transfers from 32 concurrent writers in three ways. In every run, debits and credits balanced.

  • Postgres, one transfer per transaction99% of calls finished within 219 to 235 ms563 to 581
  • Postgres, 100 transfers per statement99% of calls finished within 1,130 to 2,761 ms5,423 to 10,176
  • TigerBeetle, 100 transfers per call99% of calls finished within 81 to 92 ms40,107 to 41,788

transfers a second

For this shop Postgres is enough: at the busiest second of the recording, only 214 payments cleared.

Show the technical side

One bill, step by step

  • Buying only holds the item. The order waits with a 30-second due time.
  • The API creates one Snap transaction per order at the provider (guarded by a Redis key), and the shop opens its payment page.
  • Notifications are checked by their SHA-512 signature_key with a constant-time compare, and their gross_amount must match.
  • Every status change is a single Lua script: only from waiting, only once. A cancel returns the stock. A payment that lands after a cancel is flagged as a conflict for a refund instead of selling the stock again.
  • A sweeper runs every second for bills past due plus a 5-second grace: it asks the provider, cancels there first, and only then returns the stock.
  • A worker writes every change to Postgres, including double-entry journal lines in the ledger.

Failures injected on purpose

Duplicate notifications
10%
Paid copies with a bad signature
5%
Pending arriving after the final statusrandom delay up to 1,500 ms
10%
Buyers who pay5% decline, the rest leave
85%

Other numbers

  • From buying to the shop knowing it was paid: median 4.3 s, p95 6.4 s, including the 1 to 5 seconds k6 buyers take to pay.
  • Peak load 3,318 requests a second, because buyers check their bill every second and notifications come in too.
  • 395 k6 buyers ended with an unknown ticket: they were still queuing when stock briefly ran out, then stock came back from cancelled bills. The shop shows “your number is no longer valid” and offers a new one.

Per second

Secondcreatedpaidcancelled
1000
11000
212221779
312241559
4121619312
51122722
61362227
71141126
81121
91112
101113
111200
121000
131000