# Data Inconsistency and Race Conditions

> A race condition is a bug where the correctness of a program depends on the unpredictable relative timing of multiple processes or threads accessing shared data. It is the mechanism behind data inconsistency in unsynchronized concurrent code, and it is hard to catch because the program can run correctly many times before failing when the scheduling order changes.

Canonical: https://devsunite.com/resources/core-subjects/operating-systems/data-inconsistency-and-race-condition
Last updated: 2026-11-22

Subject: Operating Systems · Category: Process Synchronization · 4 min read

A race condition is one of the most notorious classes of bugs in concurrent programming, precisely because it doesn't always happen. The program can run correctly a thousand times and then fail on the thousand-and-first run, purely because the OS happened to schedule things in a slightly different order that time.

## What a race condition actually is

A race condition exists whenever the final, correct result of a piece of code depends on the exact relative order or timing in which multiple processes or threads execute, rather than being guaranteed by the program's logic alone. The name comes from the idea that multiple threads are effectively "racing" to read or write shared data, and whichever one gets there in a particular order determines the outcome, an outcome that should be deterministic but instead becomes a matter of luck.

## Tracing through exactly how it produces bad data

Take the shared counter example again: `counter = counter + 1`, executed by two threads, starting from `counter = 5`.

**Interleaving A (no race, threads run one fully after the other):** Thread 1 reads 5, computes 6, writes 6. Thread 2 reads 6, computes 7, writes 7. Final value: 7, correct.

**Interleaving B (a race occurs):** Thread 1 reads 5. Before Thread 1 writes back its result, Thread 2 also reads 5 (since Thread 1 hasn't written yet). Thread 1 computes 6 and writes 6. Thread 2, still working from its own earlier read of 5, computes 6 and writes 6. Final value: 6, wrong, one increment was silently lost.

Both interleavings are entirely possible outcomes of the exact same code, run twice; the OS's scheduler decides which one actually happens each time, purely based on timing that the program itself has no control over.

## A concrete example

An inventory system where two sales happen at nearly the same instant against a stock count of 1: both processes read "1 in stock," both independently decide there's enough stock to fulfill their sale, both decrement it, and the final stored value is 0, but two units were actually sold against only one that existed. This is data inconsistency, the stored data no longer accurately reflects reality, and it happened specifically because of an unsynchronized race between the two sales being processed concurrently.

## Where this matters in real systems

Race conditions are widely considered one of the hardest classes of bugs to find and fix in real production software, precisely because they're timing-dependent: a bug report saying "this only happens sometimes, we can't reproduce it reliably" is a strong, recurring signal of a race condition. Real, famous production incidents (including safety-critical ones, like the Therac-25 radiation therapy machine's fatal overdose incidents in the 1980s) have been traced directly back to race conditions in the underlying control software. This is exactly why proper synchronization (locks, semaphores, atomic operations) isn't an optional nicety in concurrent code, it's the only reliable defense against this entire class of bug.

## What interviewers actually ask about race conditions

- **"What is a race condition, precisely?"** A situation where a program's correctness depends on the unpredictable relative timing of multiple processes or threads, rather than being guaranteed deterministically by the program's logic.
- **"Why is `counter++` unsafe across threads without synchronization, exactly?"** Because it's really a read-modify-write sequence, and if two threads' reads happen before either has written its result back, one thread's update can be silently overwritten by the other's, losing an increment.
- **"Why are race conditions hard to debug?"** Because they depend on rare, specific timing between threads or processes, so a program can run correctly the vast majority of the time and only fail intermittently, making the bug hard to reliably reproduce.
- **"How do you actually prevent a race condition?"** By ensuring the shared data's read-modify-write sequence happens as one uninterruptible (atomic) unit, typically enforced with locks, semaphores, or hardware-supported atomic operations, so no other thread can interleave in the middle of it.

## Key takeaways

- A race condition means the program's correctness depends on unpredictable timing between concurrent processes or threads.
- It happens because operations that look atomic (like `counter++`) are really multiple steps, and interleaving those steps across threads can silently lose updates.
- Race conditions are notoriously hard to debug because they only manifest under specific, rare timing conditions.
- Proper synchronization is the only reliable defense, making the critical read-modify-write sequence effectively atomic.

## Related

- [Previous: The Need for Process Synchronization](https://devsunite.com/resources/core-subjects/operating-systems/need-of-process-synchronization)
- [Next: The Critical Section Problem](https://devsunite.com/resources/core-subjects/operating-systems/critical-section-problem)
- [More in Process Synchronization: Peterson's Solution](https://devsunite.com/resources/core-subjects/operating-systems/petersons-solution)
- [More in Process Synchronization: Semaphores](https://devsunite.com/resources/core-subjects/operating-systems/semaphores)
- [More in Process Synchronization: Busy Waiting and Spinlocks](https://devsunite.com/resources/core-subjects/operating-systems/busy-waiting-and-spinlocks)
- [All 205 OS interview questions](https://devsunite.com/resources/core-subjects/operating-systems)
