race condition
A bug whose outcome depends on the timing of two things happening at once.
In more detail
It usually appears when two operations run concurrently or asynchronously and the code assumes an order that is not guaranteed. For example, two programs that each check a rule and then act can both pass the check before either acts, breaking the rule. Fixes make the check and the action one indivisible step, such as a lock, a database constraint or a conditional write, or make the order explicit and ignore results that arrive too late.
Examples
In user interfaces a slow response or timer can arrive after the state it was meant for has changed; state machines and effect cleanup prevent this.Two workers processing the same outbox event at once; a unique constraint lets the database settle it.Go: never rely on goroutine scheduling order; sort results when report order mattersIf a React component fetches user 1 and then user 2, the reply for user 1 may arrive last; cancel it with an AbortController or an ignore flag in the Effect cleanup.A lost update in a database is one kind of race condition.In this course: In tests, racing an assertion against a moving clock or a service that is still starting is a classic source of flakiness. Fixed inputs and explicit readiness checks remove the race instead of hiding it with sleeps.Related terms
- concurrency group — A workflow setting that lets only one run with the same group name proceed at a time, optionally cancelling the older run.
- state machine — A model in which something is always in exactly one named state, such as idle, loading, success or error, and only defined events move it to another.
- useEffect — The React hook for running side work, such as adding listeners or changing document.body, after a component renders.
- unique constraint — A constraint that no two rows may have the same value in a column or combination of columns.
- idempotency — The property of an operation that gives the same result whether you run it once or many times, because a repeat run finds nothing left to do.
- data race — A bug where two goroutines access the same memory at the same time and at least one of them writes, without any synchronization.
- Go scheduler — The part of the Go runtime that decides which goroutine runs on which operating system thread, and when.
- concurrency — Having several pieces of work in progress during overlapping periods of time, such as requests being handled at once.
- mutable state — Data that can change after it is created, such as a field that is reassigned or a list that is added to.
- atomic variable — A variable, such as AtomicInteger, whose updates like incrementAndGet happen as one indivisible step that no other thread can interrupt.
- synchronized — The Java keyword that lets only one thread at a time run a block of code guarded by the same lock object.
- critical section — The part of a program that reads and writes shared state and must therefore be run by only one thread at a time.
- Atomics — A JavaScript object of functions that read and change shared memory in steps that cannot be interrupted by another thread.
- mutex — A lock that lets only one piece of code at a time run a critical section or do a particular piece of work.
- transaction — A group of database changes that succeed or fail together, so the data is never left half-updated.
- AbortController — The standard JavaScript object for cancelling asynchronous work: its signal is handed to the work, and calling abort() cancels it.
- cleanup function — The function an Effect returns so React can undo what the setup did, such as removing a listener or cancelling a request.
- lost update — A concurrency bug where two sessions read the same value, both write a result based on it, and the later write silently erases the earlier one.
- constraint — A rule the database enforces on stored data, such as a column that must be unique or not null.
- check then act — Reading whether something exists and then acting on the answer as two separate steps, which races under concurrency.
- stale read — A read that returns an older value than the latest committed write.
- flaky test — A test that sometimes passes and sometimes fails on the same code, because it depends on something it does not control.
- deterministic — Always producing the same result from the same inputs, with nothing left to chance or timing.
- time.sleep — A function in the time module that pauses the program for a given number of seconds, which may be a fraction.
Taught in
- Triggers and the Event Model — Continuous Integration & Continuous Delivery Pipelines
- Headless Architecture: State vs Presentation Separation — Design Systems & Component Engineering
- Full-Stack System Verification: Capstone Integration — Production Full-Stack Engineering
- Goroutines and Concurrent Execution — Concurrent Systems Programming with Go
- Executing Concurrent Checks — Concurrent Systems Programming with Go
- Concurrency Basics: Threads And Runnable — Modern Java: Virtual Threads & High-Throughput Services
- Dependency Injection And The IoC Container — Modern Java: Virtual Threads & High-Throughput Services
- Thread Safety: Locks And Atomics — Oracle Certified Professional: Java SE 21 Developer (1Z0-830) Exam Prep
- Worker Threads: worker_threads & SharedArrayBuffer — Node.js Internals & Backend Services
- useEffect Lifecycle: Mount, Dependencies, And Cleanup Phases — React 19 & Modern Component Architecture
- Keys, Constraints And Referential Integrity — PostgreSQL, Indexing & Relational Data Modeling
- Isolation Levels And The Lost Update — PostgreSQL, Indexing & Relational Data Modeling
- The Storage Decision: Relational Versus Document — Scalable System Design & Distributed Architecture
- Eventual Consistency And What It Costs A Product — Scalable System Design & Distributed Architecture
- Cache Invalidation And The Three Hard Cases — Scalable System Design & Distributed Architecture
- Idempotency, And Exactly Once As A Fiction — Scalable System Design & Distributed Architecture
- Worked Design One: Ticket Booking, Correctness First — Scalable System Design & Distributed Architecture
- Testing Time And Randomness — Automated Testing, TDD & Quality Engineering
References
- CWE-362: Race condition
- The Go memory model
- Java Tutorials: Thread Interference
- MDN: Atomics (race conditions)
- React: Fetching data (race conditions)
- MDN: AbortController
- PostgreSQL documentation: Concurrency Control introduction
- Operating Systems: Three Easy Pieces, Concurrency: An Introduction
- Go blog: introducing the race detector
- Martin Fowler: eradicating non-determinism in tests