0inconsistencies through 26k crashes and 398 killed connections
01The problem
Why it exists
The core of a payment company: a double-entry ledger in Go on PostgreSQL for wallet top-ups, holds and captures, mada card payments, fees, refunds and merchant payouts. Every request is applied exactly once, even when the server crashes mid-transaction or the network loses and duplicates messages, and history is hash-chained so any edit is detected. The same engine runs in the browser through WebAssembly.
02The system
What I built
Payment requestidempotency key
Ledger enginebalanced legs, holds
PostgreSQLrow locks, append-only
Hash chainsper account, audited
mada reconciliationdiscrepancies to suspense
An engine with balanced multi-leg transfers, two-phase holds, partial captures, reversals and balance limits, with idempotency keys committed in the same transaction as the money
PostgreSQL storage with ordered row locks (zero deadlocks), CHECK constraints and an append-only trigger, plus per-account SHA-256 hash chains and a full auditor
Reconciliation of mada settlement files: matches posted, every discrepancy routed to suspense with a reason
A deterministic chaos simulator checked against an independent reference model, mutation testing, fuzzing and a PostgreSQL load test that kills connections mid-transaction
03The result
What it achieved
1,000 of 1,000 chaos runs correct through 26k mid-transaction crashes, 99k lost and 39k duplicated messages; all 6 planted bugs caught on the first seed; every injected settlement fault found with no false alarms. On PostgreSQL: 4,246 payments/s (p99 23 ms), 1.7x from sharding hot accounts, and zero inconsistencies with 398 connections killed mid-flight.