DELAPARRA
Index
Color
NL
Category: Programming paradigms

Atomic Operation

Explained by Updated

What is Atomic Operation?

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.

Definition source Coders Lingo, CC BY 4.0

Frequently asked questions about Atomic Operation

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.

At a glance

CategoryProgramming paradigms
Explained by
Updated

More Programming paradigms terms

All terms starting with A

Skills

Download all skills (zip)

Manuals

Related services

Need any help?

Stuck on one of these in your own project? Tell me what you are working on and I will tell you how I would approach it.