Race Condition: A bug where the outcome depends on the unpredictable timing/order of concurrent operations touching shared state. A race condition occurs when several concurrent operations touch shared state and the result depends on their unpredictable order. The code is correct when run alone and wrong when run in parallel. Classic examples are lost updates, where two processes overwrite each other's change, and check-then-act bugs, where a condition is true when checked but no longer true when acted on, such as a username that was free a moment ago.
Prevention means making the critical part atomic. Databases offer atomic updates, transactions with row locks, and unique constraints. Languages offer mutexes, atomic variables and channels. Designing to avoid shared mutable state, as the actor model and immutable data do, removes many race conditions at the root. Tests that run operations in parallel can help reproduce them before users do.
Use when code runs things at the same time - threads, async/await, queues and workers, parallel requests, cron jobs, webhooks that retry - or when a bug only happens "sometimes" or under load. Covers race conditions, deadlocks, mutexes/locks, atomic operations, idempotency, message queues and backpressure, with Laravel and database examples.
Master error handling patterns across languages including exceptions, Result types, error propagation, and graceful degradation to build resilient applications. Use when implementing error handling, designing APIs, or improving application reliability.