A banking app shows the same account balance to two people. Both press “transfer” at nearly the same moment. Each request looks valid on its own, yet together they may spend money that was only available once.
Or consider a web service that works perfectly in local testing, then occasionally creates duplicate orders after deployment. The failure is rare, hard to repeat, and leaves almost no useful clue in a log.
These are not necessarily arithmetic mistakes or broken business rules. They are often race conditions: bugs caused by operations happening in an unlucky order when multiple tasks share data or resources.
Concurrent programming makes software responsive and efficient, but it also means correctness depends on timing. Understanding that dependence is the first step toward removing it.
🏁 What a Race Condition Is
A race condition occurs when a program’s result depends on the relative timing or order of concurrent operations, and at least one possible order produces the wrong result. The tasks are effectively racing to use or change something shared.
“Concurrent” does not always mean two instructions execute at the exact same instant. It means their work can overlap or be interleaved: one task pauses, another runs, then the first resumes.
A race is only a bug when order should not matter but does. If a program deliberately records which runner crosses a finish line first, timing is the intended behavior. If timing changes a customer’s account balance, it is usually a defect.
🧵 Why Concurrency Exists
Programs use concurrency to avoid making users wait unnecessarily. A browser can render a page while it downloads images. A server can handle many client requests. An operating system can run background work while keeping a window interactive.
Common forms include threads, processes, asynchronous tasks, event handlers, distributed workers, and database transactions. Their programming models differ, but they all introduce a question: who may access a shared resource, and when?
Concurrency is not inherently dangerous. It becomes difficult when independent-looking code actually depends on the same state.
🔄 The Read-Modify-Write Trap
The classic race hides inside a three-step operation: read a value, calculate a new value, and write it back. Source code can make this look like one simple action.
balance = balance - 50
Suppose the balance is 100. Task A reads 100 and plans to write 50. Before it writes, Task B also reads 100 and plans to write 50. Both writes succeed, leaving 50, although two withdrawals should have left 0.
The problem is called a lost update. One valid-looking update silently overwrote another because the whole operation was not protected as one unit.
🧩 Interleaving Creates the Failure
Interleaving is the alternation of small pieces of work from different tasks. The scheduler, runtime, operating system, network, or hardware may choose an interleaving that developers did not anticipate.
- Task A reads
counter = 0. - Task B reads
counter = 0. - Task A writes
1. - Task B writes
1.
Two increments occurred, but the final counter says one. Nothing illegal happened at the individual instruction level; the larger operation was not indivisible.
⚛️ Atomicity Is the Key Property
An atomic operation appears to happen all at once from the perspective of other tasks. No other task can observe or interfere with a halfway-completed result.
Reading a single suitably aligned machine value may be atomic on a particular platform, but an increment, a check followed by an update, or a multi-field object change often is not. Programmers should not infer atomicity from concise syntax.
Atomicity must be supplied by the language, a synchronization primitive, a database, or an explicitly designed protocol. Its exact guarantees depend on the environment.
🧮 A Counter Is Simple but Not Trivial
Counters are useful teaching examples because the intended rule is obvious: every event should increase the count once. In production, counters appear in rate limits, inventory, login attempts, metrics, job IDs, and quotas.
A plain shared integer is safe only when one execution context updates it, or when access is already serialized. Otherwise use an atomic increment supplied by the platform, protect it with a lock, or redesign ownership so one worker maintains it.
The right choice depends on the required operation. An atomic increment can solve a counter race, but it cannot automatically protect a complex multi-step business rule.
🏦 Check-Then-Act Races
Many serious bugs follow the pattern “check whether a condition is true, then act on it.” The check and action may each be correct, yet another task can change the condition in between.
Imagine two requests checking whether an item is in stock. Both see one item available. Both approve an order. The system has now promised the same item twice.
The safe design makes the decision and state change one coordinated operation: reserve inventory only if inventory remains positive. Databases can often express this as a conditional update, rather than separate read and write requests.
📁 Shared Resources Are More Than Variables
Race conditions involve any shared resource, not only memory. Files, sockets, queues, caches, temporary names, devices, session records, and database rows can all be contested.
A common file race occurs when two tasks both decide a pathname is unused and then create it. A cache race occurs when several requests simultaneously miss the same key and all perform expensive work.
Ask a practical question: if two copies of this code run at once, can they observe, create, consume, delete, or replace the same thing? If yes, there is shared coordination to design.
🧠 Threads Share Memory by Default
Threads in the same process commonly share heap memory. That makes communication fast, but it also means a mutable object may be reachable from several threads without any visible handoff.
Local variables are usually easier to reason about because each call has its own stack frame. Trouble begins when local work reaches a shared object: a global map, service singleton, connection pool, in-memory cache, or mutable field.
Thread safety is not a property of a class name or a single method. It is a property of how all access to its state is coordinated.
🌐 Asynchronous Code Can Race Too
Code using async, promises, callbacks, or an event loop may use one thread for much of its lifetime. It can still race whenever it yields control and later resumes after another task changes shared state.
For example, a handler reads a session object, awaits a network call, then writes a revised object. During the wait, another handler may have updated the same session.
“Single-threaded” reduces some kinds of simultaneous memory access; it does not make multi-step logic automatically safe across await points, timers, or external requests.
🗄️ Databases Have Their Own Races
A database is a shared concurrent system. Reading a row in application code and later writing a replacement can lose changes made by another request, even when each request uses separate database connections.
Transactions provide a boundary for grouping operations, but their behavior depends on the database and chosen isolation level. Some levels permit anomalies that stronger isolation would prevent, while stronger isolation can increase waiting, retries, or transaction aborts.
Use database constraints for rules that must never be violated, such as uniqueness. Application checks improve user feedback, but they are not a substitute for enforcement at the shared data store.
🔒 Locks Establish Exclusive Access
A lock, also called a mutex in many environments, allows one task at a time to enter a protected region called a critical section. A task acquires the lock, accesses the shared state, then releases it.
Locks are effective when the protected work is small and all code paths follow the same rule. They can make read-modify-write sequences behave as one coordinated action.
They are not magic. A lock protects only code that actually uses that same lock. One unprotected access can reintroduce the race.
⏱️ Keep Critical Sections Small
Holding a lock while performing slow work creates contention: other tasks wait even when they need only a quick update. Network calls, disk operations, user callbacks, and long calculations are particularly risky inside a lock.
Prefer this sequence: gather independent data first, acquire the lock, validate and update shared state, release the lock, then do slow follow-up work. The exact order must still preserve the business rule.
Do not shorten a critical section by moving a necessary check outside it. That often recreates the check-then-act race.
🧱 Deadlocks Are a Different Failure
A race condition produces an incorrect outcome because access was insufficiently coordinated. A deadlock occurs when tasks wait forever for one another’s resources.
For example, Task A holds lock 1 and waits for lock 2, while Task B holds lock 2 and waits for lock 1. Neither can proceed.
Fixing races with locks can therefore introduce deadlock risk. A common discipline is to acquire multiple locks in one consistent global order, and to avoid calling unknown code while holding a lock.
📉 Contention Is the Cost of Coordination
Synchronization trades some parallelism for correctness. If every request must obtain one heavily used lock, throughput can suffer and latency can rise during bursts of work.
That does not mean locks are bad. It means the protected resource may be too broad. Separate unrelated state, partition data by key, or assign ownership to independent workers where that matches the problem.
Measure real bottlenecks before pursuing complicated “lock-free” alternatives. A simple lock around a short critical section is often the clearest correct solution.
🔢 Atomic Variables Help Narrow Cases
Atomic variables and compare-and-swap operations let code update a value only if it still has the expected old value. They are useful for counters, flags, references, and small state transitions.
Conceptually, compare-and-swap says: “replace this value with the new value only if nobody has changed it since I looked.” If it fails, the operation can retry using current state.
These tools avoid a traditional lock in some cases, but their logic can be harder to review. They are best used when the state and invariant are genuinely small and well understood.
🧾 Invariants Define What Must Stay True
An invariant is a condition that must always hold for valid program state. For an inventory system, available quantity should not become negative. For a reservation system, a seat should not belong to two active reservations.
Race-condition fixes should protect invariants, not merely individual variables. Updating available and reserved separately may create a moment where their relationship is invalid.
Write down the rule before choosing a mechanism. Then ensure every state transition that can affect the rule is coordinated in one place.
🧭 Ownership Avoids Many Races
The easiest shared mutable state to synchronize is state that is not shared. Ownership-based design assigns one component, actor, worker, or event loop responsibility for changing a particular piece of state.
Other components send messages or requests instead of directly editing the data. The owner processes those messages in sequence, making its internal mutation easier to reason about.
This approach can simplify correctness, though queues introduce their own concerns: ordering, failure handling, backpressure, and message duplication must be defined.
📬 Message Passing Changes the Problem
Message passing replaces direct shared-memory updates with communication. A worker can own an account partition, for example, and process one command at a time for accounts in that partition.
The advantage is clearer control over who changes state. The trade-off is that a message may be delayed, delivered more than once, or fail after partial processing in distributed systems.
Therefore handlers often need to be idempotent: processing the same command again should not create a second unintended effect. A unique operation identifier can help detect repeats.
🧊 Immutability Reduces Coordination
Immutable data cannot be changed after creation. Tasks can safely read the same immutable configuration object, value, or snapshot without racing to modify it.
When a new version is needed, create a new value rather than editing the old one. Some systems then atomically publish a reference to the new snapshot.
Immutability does not eliminate every race. Choosing which version to publish, or applying an update based on stale state, may still require synchronization. It simply narrows the mutable surface area.
🪟 Visibility Matters Alongside Atomicity
Even when an update is atomic, another thread must reliably be able to observe it. Modern processors and compilers may reorder or cache operations in ways that are valid unless the language’s synchronization rules establish visibility.
Locks, atomic types, channels, and other language-provided primitives usually carry defined memory-ordering guarantees. Plain shared flags often do not provide the communication behavior developers expect.
Use your language’s documented concurrency mechanisms rather than relying on what appears to work on one machine. Low-level memory ordering is powerful, but it is rarely the first tool application code needs.
🎲 Why Timing Bugs Seem Random
A race may require a tiny timing window. A debugger, a log statement, a faster computer, a different processor count, or production network delay can change the schedule enough to hide or reveal it.
This is why “I cannot reproduce it now” does not disprove the bug. Intermittence is a characteristic symptom when outcomes depend on scheduling.
Production load often increases the chance of overlap, but it is not the root cause. The root cause is an unprotected dependency that allowed an invalid ordering.
🧪 Testing Concurrent Code Deliberately
Ordinary tests often run one scenario once, making them poor at finding schedule-sensitive errors. Better tests create controlled overlap and verify invariants after many operations.
- Start multiple workers behind a barrier so they act together.
- Repeat contention-heavy operations many times.
- Inject pauses at selected points in test-only code.
- Assert final state and also assert that invalid intermediate effects were never committed.
- Use race detectors, thread sanitizers, or runtime analysis tools where the language ecosystem provides them.
Testing can expose races, but passing tests cannot prove every schedule is safe. Code review and simpler designs remain essential.
🔍 Logging Without Creating False Confidence
Logs should record useful context: request IDs, operation IDs, resource keys, state versions, and important transitions. This can reveal two operations acting on the same resource at nearly the same time.
However, adding logging changes timing and may cause a race to disappear. Treat logs as evidence for reconstructing events, not as a guarantee that an observed order is the only possible order.
For sensitive systems, avoid logging private data merely to debug concurrency. Correlation identifiers often provide enough tracing value.
🪤 Common Incorrect Fixes
Several responses sound plausible but leave the underlying ordering problem intact.
- Adding a delay: sleeping changes probability, not correctness.
- Checking twice: another task can still act between checks.
- Using a global lock everywhere: it may work initially but can cause severe contention and deadlock complexity.
- Assuming a request is unique: retries, double clicks, and message redelivery happen.
- Synchronizing only writers: readers may observe inconsistent multi-field state unless the design provides safe publication or snapshots.
A reliable fix identifies the shared invariant and makes every relevant access follow one coherent protocol.
🧷 Idempotency Handles Repeated Requests
Some apparent race conditions involve duplicate delivery rather than shared memory. A payment request might be retried because a client timed out before learning whether the server completed it.
An idempotency key lets the system associate repeated attempts with one logical operation. The server stores or derives the prior outcome rather than performing the effect again.
Idempotency does not replace locking or transactions when concurrent updates conflict. It solves a different but frequently adjacent problem: doing the same intended action more than once.
🛡️ Constraints Are Lasting Guardrails
Where data rules live in a database, database constraints are valuable final guardrails. A unique constraint can prevent duplicate identifiers even if two application servers race to insert them.
Conditional updates and transactions can enforce state transitions close to the data. For example, an update that decreases stock only when stock is positive can reject one competing request cleanly.
The application must handle conflicts as normal outcomes: retry when appropriate, show an unavailable message, or request fresh state. Correct concurrent systems expect some operations to lose a race safely.
🏗️ Design for Failure Boundaries
Concurrency gets more complex when work crosses process, machine, or service boundaries. A local mutex cannot coordinate two application instances, and a distributed lock has failure modes such as expiration, partitions, and uncertain ownership.
Prefer mechanisms aligned with the resource’s authority. If a database owns the authoritative record, a transaction or conditional write there is often stronger than a lock in one web server.
Distributed coordination can be necessary, but it deserves careful design, operational monitoring, and clear behavior when communication fails. There is no universal primitive that makes network uncertainty disappear.
🧰 A Practical Review Checklist
When reviewing concurrent code, follow the data rather than searching only for thread keywords.
- What state or resource is shared?
- Which invariant must remain true?
- Can a read and later write be separated by another operation?
- What mechanism serializes, atomically updates, or rejects conflicts?
- Do all relevant code paths use that mechanism?
- What happens on retries, timeouts, cancellation, and duplicate messages?
- Could the coordination mechanism deadlock or become a bottleneck?
These questions uncover issues in threaded, asynchronous, and distributed code alike.
🎯 The Core Principle: Make Ordering Explicit
Race conditions flourish when code assumes an order that the runtime, scheduler, network, or another user never promised. The cure is not to guess timing more accurately.
Instead, make the required ordering explicit with atomic operations, locks, transactions, ownership, message sequencing, immutable snapshots, constraints, or carefully designed retries. Choose the smallest mechanism that protects the real invariant.
Correctness comes first; performance work can then reduce contention through partitioning and better architecture. A program is concurrent only when its shared state has a clear, enforceable story.
The central lesson is simple: whenever multiple operations can affect the same rule, design the rule so an unlucky schedule cannot break it. That mindset turns elusive timing failures into concrete engineering problems you can inspect, test, and solve. 🧵🔒💻

