# Race Condition

> A bug where the result depends on which task gets there first.

**Field:** Software Development  
**Category:** Compilers & Runtimes  
**Also known as:** Data Race, Race Bug

## Think of it like

Think of it like two people writing on the same line of a board at once. The words get mixed up.

## Definition

A race condition is a bug where the result depends on which of two tasks gets there first. It happens when threads or requests read and change the same data at the same time. Adding 1 to a number is really three steps: read it, add, write it back. If two threads do this at once, one write can wipe out the other. The bug appears only now and then, which makes it hard to find.

## Example

Jack's shop has 1 item left. Two buyers click at the same moment, both checks see 1 in stock, and the item is sold twice.

## Why it matters

Race bugs pass tests. Then they fail in real use, when many users act at the same moment.

## Common mistake

Adding a short wait to make the bug go away. It only hides it. Use a lock or a database transaction instead.

## Three principles

- **Timing decides.** Who gets there first.
- **Shared data.** Read, change, write.
- **Rare, then costly.** Hard to reproduce.

## Related terms

- [Mutex (Lock)](https://learn.benibix.com/term/development/mutex)
- [Thread](https://learn.benibix.com/term/development/thread)
- [Deadlock](https://learn.benibix.com/term/development/deadlock)
- [Database Transaction](https://learn.benibix.com/term/development/transaction)

## Told apart from

- [Race Condition vs Deadlock](https://learn.benibix.com/compare/race-condition-vs-deadlock): A race condition and a deadlock are both bugs that come from threads sharing data, but they fail in opposite ways. In a race, two threads touch the same data at once with no lock, and the result comes out wrong. In a deadlock, threads hold locks and wait for each other, so nothing runs at all. A race gives wrong answers; a deadlock gives none.

---

Source: https://learn.benibix.com/term/development/race-condition
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
