Atomicity in Database System, explained!

Database is beautiful, like really beautiful. But let's learn more about its system. How the databases can keep the data true, sound, and safe?

Atomicity in Database System, explained!
Photo by Tobias Fischer / Unsplash

Database is beautiful, like really beautiful.


What are we going to talk about?

Okay, it might be a little bit weird if we just jump straight into all of the messy world of the database's ACID systems. Let's start from the basic.

Most of the technologies nowadays, are storing their persistent data in a database. There are a lot of choices of database management system (DBMS) out there, but some of the popular ones are: MySQL, PostgreSQL, MongoDB, etc.

While anyone can build their on DBMS as their wish, to make sure the data is true, up-to-date, and secure, the ACID system was made as the base guideline for that.

A need

In order to use a database (which function is to store the data), the most important thing is querying the data. We might want to read the data, create a new data, update/change the data, and delete the data. That is the most general things that we need.

Often time, we need to create multiple queries in order to achieve what we want, such as:

Send $100 from person A to person B

The goal are sound simple for us as a human. But, the machine need to know the exact, step-by-step guide, to reach the goal. The (super oversimplified) steps might looks like:

READ person A balance > UPDATE -$100 of person A balance > UPDATE +$100 of person B balance

In the 'database world' we call the multiple queries that act as one single query as a TRANSACTION.

Transaction?

As we have mentioned before, a transaction is a set of queries that being treated as one unit of work. Transaction are usually being used in data changing or modification. But it is normal to have a read-only transaction.

Transaction has its own lifespan:

  • Transaction BEGIN
  • Transaction COMMIT
  • Transaction ROLLBACK
  • Transaction unexpected ending = ROLLBACK (e.g crash)

Okay, but what on earth is the relationship between the transaction and the ACID thing?

A is for Atomicity

Since we know that the transaction is just a set of queries, then everything can happens while the queries is on process. Let's say, we have made some errors, the server crash, etc.

In those situations, the ACID compliance database system must ROLLBACK all of the changes that they have made and leave the database just like where it was before all of those problem starts to happen.

It means, all of the queries in a transaction is just like single atom, solid, unbreakable, and unseparatable. If one query is down, every query is down. Simple and straightforward.


TLDR: Atomicity = all of the queries in a transaction is just like single atom, solid, unbreakable, and unseparatable. But, atomicity is just one part of the ACID systems. We still have a really long way to go.

Subscribe to Hisam's Spaces

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
jamie@example.com
Subscribe