You deploy on a Friday afternoon. Everything looks fine in staging. The unit tests pass. Your code review was clean.
Then Monday morning hits. Your support inbox fills up with reports of missing inventory, double-charged orders, or profile updates that silently overwrite each other. You check the database logs and find nothing obviously wrong. No constraint violations. No deadlocks. Just data that doesn’t add up.
You are looking at a race condition.
Race conditions are one of the sneakiest categories of bugs in backend systems. They don’t crash your app. They corrupt your data slowly, often invisibly, until the damage is done. The worst part? Traditional testing rarely catches them. You need specific strategies to spot them before they wreck your database.
Race conditions happen when two or more operations read and write the same data in an overlapping window, causing the final state to depend on timing instead of logic. To prevent them, you need to combine proper database isolation levels, pessimistic or optimistic locking, atomic operations, and careful application-level coordination. This guide walks through five concrete ways to detect and fix these bugs before they reach production.
What a race condition looks like in practice
Imagine a ticket booking system. Two users check seat 12A at the same time. Both see it as available. Both click “Buy.” Your application code runs two UPDATE statements. Both succeed. Now you have two tickets sold for the same seat, or you have one sale silently erased by the other.
This is not a database corruption bug. It is a logic bug caused by a missing coordination mechanism. The database did exactly what you told it to do. The problem is that you told it to check availability and then update the seat in two separate steps without protecting the window between them.
Race conditions are especially common in these scenarios:
- Inventory management systems
- Financial transactions (balance transfers, point deductions)
- User profile updates from multiple devices
- Queue or job processing systems
- Any system using “read, modify, write” patterns
Let’s look at how to spot them and how to prevent race conditions in database systems effectively.
1. Look for read-check-then-write patterns in your code
The most common race condition pattern is a read-check-write cycle. Your code reads a value, checks a condition, and then writes a new value. Between the read and the write, another request can change that same value.
Here is a numbered process to audit your codebase for this pattern:
- Search your code for SELECT statements that are immediately followed by application-level conditionals (if statements, ternary checks) and then UPDATE or INSERT statements.
- For each match, ask yourself: “Could another request modify this row between the SELECT and the UPDATE?”
- If the answer is yes, flag that code path as a potential race condition.
- Add logging with timestamps around the read and write to measure the window size.
- Run concurrent load tests against that endpoint with tools like k6 or artillery to reproduce the overlap.
A concrete example looks like this in pseudocode:
-- Step 1: Read
SELECT quantity FROM products WHERE id = 42;
-- Result: quantity = 5
-- Step 2: Check in application code
if (quantity > 0) {
-- Step 3: Write
UPDATE products SET quantity = quantity - 1 WHERE id = 42;
}
If two requests run this pattern at the same time, both see quantity = 5. Both decrement. You end up with quantity = 4 instead of 3. One item just vanished from your inventory.
The fix is to use an atomic UPDATE that combines the check and the write:
UPDATE products SET quantity = quantity - 1
WHERE id = 42 AND quantity > 0;
This single statement either succeeds or fails atomically. No window for a second request to slip in.
2. Audit your transaction isolation levels
Transactions alone do not prevent race conditions. Many developers assume that wrapping code in a BEGIN/COMMIT block solves everything. It does not. The default isolation level in most databases (READ COMMITTED) allows phantom reads and non-repeatable reads.
Here is a table that clarifies which isolation levels help with which race condition types:
| Isolation Level | Prevents Dirty Read | Prevents Non-Repeatable Read | Prevents Phantom Read | Performance Cost |
|---|---|---|---|---|
| READ UNCOMMITTED | No | No | No | Lowest |
| READ COMMITTED | Yes | No | No | Low |
| REPEATABLE READ | Yes | Yes | No | Medium |
| SERIALIZABLE | Yes | Yes | Yes | Highest |
If you are running a financial transaction that reads a balance, subtracts an amount, and writes the new balance, READ COMMITTED will not protect you. A second transaction can read the same original balance and commit a conflicting update.
To prevent race conditions in database systems that involve multi-statement operations, use SERIALIZABLE isolation. In PostgreSQL, for example:
SET TRANSACTION ISOLATION LEVEL SERIALIZABLE;
BEGIN;
SELECT balance FROM accounts WHERE id = 1;
-- application logic here
UPDATE accounts SET balance = balance - 100 WHERE id = 1;
COMMIT;
SERIALIZABLE isolation ensures that your transaction sees a snapshot of the database that is consistent as if transactions ran one after another. If a conflict occurs, one of the transactions will fail with a serialization error. You catch that error and retry.
Expert advice: “SERIALIZABLE isolation is not a silver bullet. It increases the chance of transaction conflicts under high concurrency. Always pair it with retry logic in your application layer. A 3-retry pattern with exponential backoff handles most serialization failures gracefully.”
3. Use optimistic locking with version columns
Optimistic locking is one of the most practical ways to prevent race conditions in database systems without locking rows for long periods. The idea is simple: add a version column to your table, increment it on every update, and include the version in your WHERE clause.
Here is how it works in practice:
-- Table structure
CREATE TABLE products (
id INT PRIMARY KEY,
name VARCHAR(100),
quantity INT,
version INT DEFAULT 1
);
-- Update with optimistic lock
UPDATE products
SET quantity = quantity - 1, version = version + 1
WHERE id = 42 AND version = 3;
If another request already updated the row and bumped the version to 4, your UPDATE affects zero rows. Your application checks the affected row count. If it is zero, you know a race happened. You retry the entire operation.
This approach works well for:
- Web applications with moderate concurrency
- Systems where conflicts are rare but costly
- APIs that use ETags or similar versioning mechanisms
The tradeoff is that you need to handle retries in your application code. For high-conflict scenarios, this can lead to more failed requests and user-facing errors. But for most systems, it is a clean and scalable solution.
4. Implement pessimistic locking for high-conflict paths
When conflicts are frequent and the cost of a retry is high, pessimistic locking is a better fit. You explicitly lock the row or table before performing your read and write operations.
In PostgreSQL, you can use SELECT … FOR UPDATE:
BEGIN;
SELECT * FROM products WHERE id = 42 FOR UPDATE;
-- Now this row is locked until the transaction ends
-- Other transactions trying to read or write this row will wait
UPDATE products SET quantity = quantity - 1 WHERE id = 42;
COMMIT;
This guarantees that only one transaction processes that row at a time. Other transactions queue up and wait their turn. It is simple and effective.
But there are downsides:
- Lock contention can slow down your system under high load
- Long-running transactions with locks can cause deadlocks
- You need to keep your transaction windows as short as possible
- Distributed systems cannot rely on database-level locks alone
Use pessimistic locking when you have a clear understanding of your concurrency patterns and you can keep transaction duration under 50 milliseconds. Anything longer and you risk blocking other critical operations.
For a deeper look at how to reproduce these issues in your development environment, check out how to reproduce and fix race conditions in JavaScript. Many of the same debugging principles apply even though the implementation language differs.
5. Watch for time-of-check to time-of-use (TOCTOU) bugs in your application layer
TOCTOU bugs are a specific flavor of race condition where your application checks a condition and then acts on it later, assuming the condition still holds. These are extremely common in web applications that use API gateways, message queues, or background job processors.
Here are common TOCTOU patterns to watch for:
- Checking if a username is available before inserting a new user
- Verifying inventory in a cart before processing payment
- Validating a discount code before applying it to an order
- Checking if a file exists before writing to it
Each of these patterns has a window between the check and the action. In that window, another request can change the state.
The fix is to move the check into the write operation itself. Use database constraints, unique indexes, and atomic operations to enforce invariants directly.
For example, instead of:
# TOCTOU bug
if not user_exists(username):
create_user(username)
Use a unique constraint on the username column and handle the duplicate key error:
try:
create_user(username)
except IntegrityError:
# Username already taken, handle gracefully
return "Username is taken"
This eliminates the race window entirely. The database enforces the invariant atomically.
If you are dealing with more complex application logic that spans multiple services, you might need distributed locking or saga patterns. For a broader view on catching these kinds of bugs early, see mastering code review best practices to catch bugs early. Code reviews are one of the best places to spot TOCTOU patterns before they ship.
Common mistakes that make race conditions worse
Even when developers know about race conditions, certain habits make the problem harder to detect and fix:
- Using SELECT MAX() or SELECT COUNT() without locking. These aggregate queries read current state but do not lock it. Two concurrent requests can both see the same max value.
- Relying on application-level timestamps for ordering. Timestamps generated on different servers can drift. Use database-generated timestamps or sequence numbers instead.
- Caching stale data and making decisions based on it. If your application reads from a cache and then writes to the database, you have a race condition window between the cache read and the database write.
- Ignoring the order of operations in bulk updates. When you update multiple rows in a loop, other transactions can modify those rows between iterations.
How to test for race conditions before production
You cannot rely on unit tests alone to catch race conditions. You need concurrency testing. Here are practical approaches:
- Write integration tests that fire multiple requests simultaneously using threading or async libraries.
- Use database-level logging to track concurrent transactions and look for overlapping read/write windows.
- Run your application under load testing tools that simulate real user concurrency patterns.
- Add database assertions that check for invariant violations after concurrent operations.
Many teams skip this step because it is hard to set up. But a single race condition in production can cost hours of debugging and data recovery. The upfront investment in concurrency testing pays for itself the first time it catches a bug.
For more on why your testing might miss these issues, read why your unit tests pass but your app still crashes in production. Race conditions are a classic example of a bug that passes unit tests but fails under real concurrency.
Putting it all together
Race conditions are not going away. As long as we build systems that handle concurrent requests, we will need to guard against them. The good news is that the tools to prevent them are well understood and battle-tested.
Start with an audit of your codebase for read-check-write patterns. Add optimistic locking to your most critical tables. Use SERIALIZABLE isolation for financial transactions. And always, always move your business logic invariants into the database layer where they can be enforced atomically.
The next time you deploy on a Friday, you will sleep better knowing that your inventory counts stay accurate and your users get charged exactly once.
