DEV Community

Tushar Bharti
Tushar Bharti

Posted on

ACID Properties with real life examples

Okk folks, as we all know acid properties is an integral part of database management system and today we are going to learn about it in a very simple language.

A - Atomicity
C - Consistency
I - Isolation
D - Durability

ATOMICITY

Atomicity word comes from an "Atom" just like an atom is the smallest unit and cannot be broken into pieces, similarly transactions is something which should either happen 100% or not happen at all.

Let's assume I have a shop in which I sell spherical cube, now one day a customer came to me and said that I want 7.5 cubes from you, now tell me will I be able to give him 7.5 cubes or not ??

Definitely the answer is "NO", I can only give him either 7 cubes or 8 cubes but no in-between and the reason behind this is simply the inherit nature of the cube.

Cube is a discrete entity which exists as a whole not in pieces and DBMS transactions are also exists as a whole not in pieces. If I have to send my friend Rs.500, then it will not make any sense to send him only Rs.487.

So to make sure the transaction performed successfully, atomicity is there.


CONSISTENCY

We all have heard this line that says "Energy is neither created nor destroyed, it just change its form from one to another". The same rule applies to the transactions in our database.

Let me ask you two questions:

Q.1) If I have two accounts : A and B and both have Rs.500 and total is Rs.1000, so if B sends Rs.200 to A, will the total amount gets changed or not ??

Q.2) If you solve two medium questions everyday on "Leetcode", but one day you solved one-easy and one-medium question and on the same day I asked you how many questions you have solved, will the answer changed from 2 to any other number ??

The answer for both the above questions is "NO".
This means the system was consistent with its internal properties before any operations and it is still consistent with its internal properties after operations.


ISOLATION

Let's assume a situation where I have been assigned a task by my friend that in his room, there is a box which contains lots of candies and I have to bring that box to him. Now before me, someone came in his room and replace the box with another similar-looking box which contains something else.

Now because I trust my friend, I went to his room and picked up the box without checking if it is the right box or not.

The same thing can happen in transactions, where "t1" transaction is working on some value(10) and "t1" has to add (5) into the existing value, but in-between "t2" transaction came and add "25" into the already existing value(10) and makes it 35. So now "t1" will add (5) to (35) and not (10).

so the actual answer should be : 10 + 5 = 15
but because of "t2" interference, the answer is: 10 + 25 + 5 = 40

So "Consistency" make sure that every transaction can work on the data in isolation such like they are the only one in the whole system and if any other transaction made any changes to the data then those changes should be made permanent in the system and only after that the change value should be visible to other transaction.


DURABILITY

let's assume a situation where I was busy on Sunday because I have to:

  • visit the doctor

  • pay my electricity bill

  • go for a grocery shopping

and that's why I refuse my friend's offer of watching a movie together.

Now my friend confronted me on Wednesday and told me that I was free that day and I made a excuse for not going out. So to prove him that I was actually busy that day, I showed him the bills of grocery store and electricity and online receipt of doctor's appointment.

Similarly in database also, whenever we made any changes all those changes occur in main-memory and to it made those changes permanent in the database but before making those changes permanent some error occurred and we will not able to make it permanent.

So database maintains "Logs Files" which include the history of all the changes we have made to the database and using those logs files, database retrace the changes and made them permanent.

Top comments (0)