When you write some data in a database, you are NOT taking a leap of faith. You know that the database won’t let you down when you need that data once again.
This is because the transaction provides some pretty useful guarantees known as ACID.
The acronym stands for Atomicity, Consistency, Isolation, and Durability.
But what is the meaning of each term in the context of transactions?
In this post, we’ll look at each term in detail.
Atomicity (A)
Atomic means something that cannot be broken down.
It’s the same thing that our Physics teachers tried to tell us many years ago. If only we would have listened…
In the context of transactions, Atomicity describes what happens to a Transaction that’s executing multiple write operations and something goes wrong halfway.
Think of a Transaction as a bridge between two states of data named A and B. No intermediate states are allowed. When the transaction completes successfully, you go from A to B. But you can’t stay anywhere between A and B.
It’s all or nothing as show in the diagram below:
So, if you can’t complete the entire transaction successfully, you don’t complete any part of it.
The transaction is completely aborted.
Why is this a good thing?
Without atomicity, a mid-way error during multiple updates, the entire database state can turn into a mess. You won’t know which changes have taken effect and which haven’t.
Retrying the transaction would be risky because it can result in duplicate or incorrect data.
Atomicity saves you from all this trouble.
Consistency (C)
Consistency in the ACID sense is all about the context of your application.
In any application or system, there are certain invariants or statements that must always be true.
For example, credits and debits in a bank must always be balanced.
Or, in an email management system, the number of read and unread emails must always be balanced.
A consistent transaction is one where statements are true at the beginning as well as the end of the transaction.
The below illustration tries to show the concept.
But most of it depends on your application.
A database cannot save you if you write bad data violating the invariants. Sure, you can have some checks like foreign keys or constraints but a lot depends on the application’s requirements.
So - when you think about it, Consistency is the property of your application.
Isolation (I)
Isolation is the trickiest guarantee to get right.
It’s a given that your database will be accessed by multiple clients simultaneously.
Databases can handle this quite well if they are reading and writing different parts of the database. However, the plot thickens when different clients try to access the same database records.
This ends up creating concurrency issues.
An example of this issue is two clients simultaneously trying to increment a counter that is stored in a database.
Despite two increment transactions, the counter was only incremented by 1 because of race conditions.
The goal of Isolation is that concurrently executing transactions are isolated from each other.
But it’s more of a degree than an absolute value.
There are multiple isolation levels ranging from weak to strong. Some common ones are:
Read Uncommitted
Read Committed
Repeatable Read
Serializable
Durability (D)
Lastly, the purpose of a database system is to provide a safe place where you can store data without any fear of losing it.
Durability is the guarantee that promises this safety.
Once a transaction has been committed successfully, any data that has been written won’t be lost.
The guarantee holds even in case of a hardware fault or database crash.
There are two ways databases achieve this:
Disk storage and write-ahead logs for single-node databases
Replication for multiple nodes.
Of course, despite all of this, there is no perfect durability.
It’s all about risk-reduction techniques and their use depending on the requirement.
👉 So - do you have any additional points about ACID properties?
Eraser Professional Plan Free Trial (Affiliate)
As you all know, I use the Eraser for drawing all the diagrams in this newsletter.
Eraser is a fantastic tool that you can use as an all-in-one markdown editor, collaborative canvas, and diagram-as-code builder.
And now you can get one month free on their Professional Plan or a $12 discount if you go for the annual plan. The Professional Plan contains some amazing features like unlimited AI diagrams, unlimited files, PDF exports, and many more.
Head over to Eraser and at the time of checkout, use the promo code “CODEX” to get this offer now.
Shoutout
Here are some interesting articles I read this week:
3 challenging questions to ask as a software engineer by
5 Mistakes that New Tech Leads Make: Learn to Lead Effectively by
Going from impostor one day to feeling like a superhuman the next day by
That’s it for today! ☀️
Enjoyed this issue of the newsletter?
Share with your friends and colleagues.
See you later with another value-packed edition — Saurabh.
Thanks for the shoutout.
I wish I had learned these concepts from visuals like yours instead of reading a formal definition that I didn't understand at the time
Great and detailed explanation of ACID, a fundamental concept of transactions. Also loved the recommendations!