Atomic Operation: An operation that completes as a single, indivisible step — no other thread can observe it half-done. An atomic operation happens as one indivisible step. Other threads or processes see it either not done or fully done, never halfway. The classic problem it solves is a lost update. Two requests read a stock level of 5, both subtract one, and both write 4. One sale has disappeared. An atomic decrement, such as UPDATE products SET stock = stock - 1 WHERE id = 7, lets the database handle it in one step, so the result is 3.
What should you keep in mind with Atomic Operation?
In application code, prefer atomic operations the database or cache already offers: increments, INSERT ... ON DUPLICATE KEY, Redis INCR, or a row lock inside a transaction. Reading a value in PHP, changing it and writing it back is never atomic. It works in testing and fails under load, which makes the bug hard to reproduce.
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.