Context
The money-handling core of the production platform I run for an online school, extracted into a standalone, framework-free TypeScript library on plain PostgreSQL. The production code is private; this repository is the part worth reviewing, with every lock and constraint visible as plain SQL.
Billing: exactly-once charges under concurrency
Staff mark, re-mark, clear and cancel lessons at the same time. The student must be charged exactly once, and the ledger must always agree with the attendance journal.
- Append-only ledger. A trigger rejects
UPDATEandDELETE; corrections are reversal rows. Each reversal names the charge it undoes, and aUNIQUEconstraint lets a charge be reversed only once. - One lock order everywhere. Writes for a student take
FOR NO KEY UPDATEon the student row, thenFOR SHAREon the lesson. Cancellations takeFOR UPDATEon the lesson, so "is this lesson cancelled?" cannot change between the check and the write. - Why
NO KEY UPDATE. A cancellation writing a reversal needs aKEY SHARElock on the student via foreign-key checks.FOR UPDATEwould block it and deadlock;FOR NO KEY UPDATEallows it.
Step through both versions — a mark and a cancellation racing on the same lesson, exactly as in the repository's concurrency test:
Interactive · lock order
- 1.SELECT … FROM student WHERE id = $1 FOR UPDATE· granted
Step 1/5. Mark takes the student row first.
Payments: a webhook that trusts nothing
Rate limit → constant-time secret check → provider IP allowlist → re-fetch the payment from the provider's API and validate it at runtime → match owner, amount and currency against our own payment intent → credit idempotently with ON CONFLICT DO NOTHING on the provider's payment id. Ten identical deliveries credit once.
Send a few requests through it — each scenario below is a case from the test suite:
Interactive · send a webhook
Pick a request above to send it through the handler.
- Rate limitPer source IP, counted in Postgres
- Shared secretConstant-time compare; mandatory in live mode
- Provider IP allowlistLive mode only
- Read payment id from bodyNothing else in the body is trusted
- Re-fetch from provider APIStatus, amount, currency, owner
- Match our payment intentSame owner, amount and currency
- Idempotent creditON CONFLICT DO NOTHING on the payment id
Response will appear here.
Auth: enumeration-safe login
Unknown emails still cost one bcrypt compare, so response time does not reveal who has an account. Login limits run per (IP, email) and per email across IPs, checked and counted atomically under a per-email advisory lock.
How it's tested
- Deterministic interleavings. A test pauses one operation right after it takes its locks, starts a competing one and asserts it waits — one test per lock, plus the deadlock case.
- Bursts. 20 simultaneous marks of one lesson, 10 identical webhook deliveries, 50 parallel login guesses: the ledger and the limits come out exact.
- Randomized ledger invariants. Seeded runs of 200 random operations, five at a time in parallel, checking nine invariants after every batch.
- Mutation check. A script removes each defence in turn — either lock, the cancelled-lesson guard, the one-reversal-per-charge constraint, the idempotency key, the dummy bcrypt compare and others — and the suite has to fail for all 15.
A bug the randomized test caught
The first run failed on "a cancelled lesson nets to zero for every student". A student was charged, the lesson was cancelled (charge reversed), then the attendance mark was cleared — reversing the charge a second time. The student was credited for a lesson they were never charged for.
The fix: clearing and cancelling both compute the reversal from the lesson's current net, under the lesson's lock, so a clear after a cancellation finds nothing to reverse. Clearing also refuses a cancelled lesson outright, as a business rule. Both the plain sequence and the race are now tests.
After publishing
A reader suggested moving the guarantee from the code into the schema. Every reversal now carries the id of the charge it reverses, with a UNIQUE constraint on that column, plus a trigger that the reversal matches its charge: same student, same lesson, opposite amount. A test pauses a clear after its locks, reverses the same charge from another path, and the clear fails at insert instead of refunding twice.