race condition

/ˈreɪs kənˌdɪʃ.ən/noun

A race condition is a software bug in which the outcome depends on the order or timing of actions that can happen concurrently. The same input can produce different results.

A race condition caused the online shop to accept two orders for the last item in stock.

Software often runs several tasks concurrently. Those tasks can read and change the same data, such as an online shop's stock level. If their steps occur in an unexpected order, the outcome can depend on which task gets there first. This is called a race condition.

How does a race condition occur?

Suppose one item remains in stock and two customers order it at the same time. Both requests first read the stock value of 1 and decide the item is available. Each then writes a new value. Without proper coordination, the shop accepts two orders for one item.

Race conditions do not occur only between program threads. Separate processes, servers or connected systems can also change the same data concurrently. A bug may occur rarely because a small timing difference changes the sequence. This can make it difficult to reproduce in a test.

How do you prevent timing errors?

Developers ensure related actions run as one operation, or allow only one task at a time to change shared data. Options include locks, atomic operations or a Glossary · In briefdatabaseA database is a structured collection of data that software can store, retrieve and modify. A database management system controls access to that data.Read more transaction with appropriate isolation. A Glossary · In briefmessage queueA message queue temporarily stores messages until another software component processes them. The sender does not have to wait for the recipient to finish.Read more can also process tasks in a fixed order.

The right approach depends on the system. It must coordinate access to shared data without unnecessarily blocking other work. Tests with many concurrent requests and focused Glossary · In briefloggingLogging records events while software is running. These records help developers follow a system's behaviour and investigate problems.Read more help identify race conditions.