How Atomicity Powers ACID: Understanding the 'A' in ACID


What is atomicity?
Think of a transaction in a database like placing an order for a custom-made cake. You tell the baker to make a cake with specific ingredients and decorations, and you give them a deadline. Now, imagine if, halfway through, something went wrong—maybe the ingredients were mixed up, or the decorations were not applied correctly.
Atomicity ensures that either the entire order is completed (the cake is made just as you requested), or the order is canceled entirely (no half-baked cake). You never end up in a situation where you've got a partially completed cake or a wrong order. In case of any issues—like a power failure or a mistake—the baker will either redo the entire order or refund you, but the transaction will never leave you with something halfway done.
Similarly, an atomic system in databases guarantees that all parts of a transaction either succeed or fail together, ensuring that after any disruptions, the database never ends up in an inconsistent state.
A great example of seeing why it is critical to have atomicity is Money Transfers.
Imagine transferring money from bank account A to B. The transaction involves subtracting balance from A and adding balance to B. If any of these changes are partially applied to the database, it will lead to money either not debited or credited, depending on when it failed.
How is atomicity implemented?
Atomicity in Databases
Most databases implement Atomicty using logging; the engine logs all the changes and notes when the transaction started and finished. Depending on the final state of the transactions, the changes are either applied or dropped.
Atomicity can also be implemented by keeping a copy of the data before starting the transaction and using it during rollbacks.
Atomicity in Business Logic
The construct of atomicity can be implemented at a high-level language or business logic by burrowing the concept of atomic instructions; for example, you can use compare and swap to update the value of a variable shared across threads concurrently.
Atomicity is not just restricted to Databases; it is a notion that can be applied to any system out there.
Subscribe to my newsletter
Read articles from Nitin Yadav directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by

Nitin Yadav
Nitin Yadav
I am a highly driven backend engineer with expertise in designing robust architectures, optimizing microservices, and streamlining CI/CD pipelines. With extensive experience in cybersecurity, API development, and cloud platforms, I ensure secure, scalable, and efficient software solutions that drive business success.