Day 5: Simulate Concurrent Orders with Transactions — And Watch for Race Conditions with Isolation Levels.
Day 5: Simulate Concurrent Orders with Transactions — And Watch for Race Conditions with Isolation Levels
1. The Continuity Contract
In Day 4, you constructed a multi-table relational schema linking customers, orders, and order_items via foreign keys, and you wrote complex JOIN queries to generate customer purchase histories. However, those queries assumed a static database where only one user makes changes at a time.
Today, we introduce the chaotic reality of production: hundreds of customers attempting to buy the exact same inventory simultaneously. We will transition from simple queries to concurrent transactional operations, exposing how default database isolation levels can silently corrupt your catalog inventory. This work directly prepares you for Day 1, where we will loop back to build a resilient, production-grade product catalog schema from scratch, applying these concurrent safety patterns from the very first line of DDL.
2. The Production Stakes: The Flexcoin Bankruptcy
In March 2014, Flexcoin, a Bitcoin bank, was forced to shut down after a robber stole 896 BTC (worth approximately $600,000 at the time). The attack did not exploit a leaked private key or a compromised operating system. Instead, it exploited a classic database race condition in the bank's transaction handling code.
The attacker sent multiple concurrent withdrawal requests from a single account to different endpoints simultaneously. The application code for each request executed the following sequence under the default database isolation level:
Check if the user's balance is greater than or equal to the withdrawal amount.
If yes, proceed to withdraw the funds (send Bitcoin to the external address).
Update the user's database balance by subtracting the withdrawn amount.
Because these requests executed in parallel, both threads read the initial balance (e.g., 10 BTC) before either thread wrote the updated balance back to disk. Both checks succeeded. The system approved both withdrawals, resulting in a negative balance that the database did not prevent because the application-level check was bypassed by concurrent execution.
This is a Lost Update anomaly. In a retail catalog, this exact bug manifests as selling 100 units of a high-demand gaming console when you only have 5 units in stock, leading to broken customer trust, manual cancellations, and severe financial chargebacks.
3. The Intuition: The Library Book Reservation
To understand how databases manage this, imagine a physical library with a single copy of a rare book.
If two patrons want to borrow it, they must check the card catalog. Under a Read Committed isolation level (the default in PostgreSQL), a patron can see if the book is on the shelf. However, just because they see it on the shelf doesn't stop someone else from grabbing it while they are walking toward it.
To solve this, we need a Locking Mechanism.
Pessimistic Locking (
SELECT ... FOR UPDATE) is the equivalent of calling the librarian and saying: "Hold this book at the desk for me while I walk over." No one else can touch or reserve that book until you either check it out or leave the library.Serializable Isolation is the equivalent of the librarian forcing every patron to stand in a single-file line. Only one person can interact with the shelves at a time. If anyone tries to cut the line or change a book status out of order, the librarian cancels their transaction and tells them to start over.
4. Component Architecture & Concurrency Control
To handle concurrent checkouts safely, our system architecture must coordinate multiple application threads communicating with a single database engine.
[Insert Diagram 1: Component Architecture here]
When a checkout request arrives, the application must perform three steps inside a database transaction:
Keep going with the full lesson
The rest of this lesson — plus its implementation guide, runnable scripts and source code — is included with the course or an All Access plan. Create a free account to start the free lessons in every course.
Already a member? Sign in