What Is a Race Condition?
A race condition occurs when the correctness of a program depends on the relative timing of multiple operations happening concurrently, and that timing isn't...
Published October 7, 2026
A race condition happens when the result of a program depends on the unpredictable timing of two or more threads or requests accessing shared data. The fix is to make the critical section atomic with locks, transactions or atomic operations.
The concept
Two actors read the same value, both modify it, and the last write wins, losing an update. The classic example is two withdrawals reading the same balance.
Races are hard to reproduce because they depend on timing. They appear in threads, async code, databases and distributed systems.
Lost update
Practical examples
Atomic SQL update
UPDATE accounts SET balance = balance - 30 WHERE id = 1 AND balance >= 30;Lock a row in a transaction
START TRANSACTION;
SELECT balance FROM accounts WHERE id = 1 FOR UPDATE;
UPDATE accounts SET balance = balance - 30 WHERE id = 1;
COMMIT;Mutex in Python
lock = threading.Lock()
with lock:
counter += 1Common mistakes
- Read-modify-write in application code instead of in the database.
- Checking a condition and acting later (time-of-check to time-of-use).
- Sleeping to 'fix' a race.
Key takeaways
- Shared state + concurrency = risk.
- Make read-modify-write atomic.
- Use transactions, locks or unique constraints.
FAQ
Does JavaScript have race conditions?
Yes, between async operations even though it is single-threaded.
How do I find one?
Stress tests, thread sanitizers and logging with timestamps.