Fintech Lab
Lesson 39Scale and concurrencyAdvanced
Double-spend prevention
Two concurrent withdrawals can each pass the same balance check. Lock or constrain.

User has ₦100 in their wallet. They open two tabs and click 'Withdraw ₦60' in each tab at the same time. Both requests read balance = ₦100. Both pass the sufficient-funds check (100 >= 60). Both post withdrawal entries. The user now has ₦100 - ₦60 - ₦60 = -₦20 in their wallet, and ₦120 of withdrawals succeeded against ₦100 of actual balance. They double-spent ₦20 of money that doesn't exist. This bug is the most common production fintech outage. Three fixes exist: pessimistic locks (SELECT FOR UPDATE), optimistic concurrency (version column), and database CHECK constraints. All three solve it; engineers building serious ledgers use at least two.

Fintech Lab is a free, interactive lab for fintech engineers. Real ledger, your own sandbox, engineering patterns from production. See all 85 lessons.

Search lessons

Type to find any of the 85 lessons. Press Enter to open.