Clear your ACID concepts with me -  Isolation

 When we work with databases, there can be multiple events that can go wrong. A few of them are:

  1. Several clients can update the same data, leading to overwriting each other’ changes
  2. Several client can read partially updated data that doesn’t make sense
  3. The database can go down and lead to partial updates of write.
  4. The application may crash and lead to partial updates of write.
  5. Network issues can cut off both databases and applications and lead to partial write updates.
Photo by Rodion Kutsaiev on Unsplash

Databases have to deal with all these faults and avoid catastrophic failures. The safety guarantees provided by the transactions are defined by ACID which stands for

A: Atomicity

C: Consistency

I: Isolation

D: Durability

Atomicity

If the writes are grouped together into an atomic transaction, and the transaction cannot be completed (committed) due to a fault, then the transaction is aborted and the database must discard or undo any writes it has made so far in that transaction.

For more understanding of atomicity refer to:

Clear your ACID concepts with me — Part 1

Consistency

I would like to discuss the I and D of ACID, before jumping into this. I will surely talk about consistency in my later posts.

Let’s start

Isolation

Let’s start it with a simple situation

You work in a social networking company and are given a feature to manage i.e., “like counts” of videos. You developed this feature by incrementing the counter and tested this feature individually and deployed it. As it is just a counter incrementation, it should work fine. What a big deal!?

But a bug is raised that users are not able to see correct like counts.

You checked everything it is just

counter = counter + 1;

How can this fail?

I will tell you, we missed the fact that multiple users are liking the video concurrently.

Consider two users simultaneously who liked the video. Each user read the liked count as 10 incremented it by 1 and updated DB with 11. The like count should be 12 but it is 11 because of race conditions.

How this can be solved at the DB layer?

Isolation in the database saves us from these situations

Isolation means that concurrently executing transactions are isolated from each other. 

The database ensures that when the transactions have been committed, the result is the same as if they had run serially (one after another), even though in reality they may have run concurrently.

An isolation level protects against one or more types of race conditions and provides an abstraction that we can use to reason about concurrency. The stronger the isolation level is, the more protection it offers against race conditions, but the less performant it is.

Transactions can have different isolation levels that are defined based on the type of race conditions they forbid:

  • Serializable
  • Repeatable reads
  • Read Committed
  • Read uncommitted

I can talk about all of these levels and discuss them in as understandable a way it is possible. Let me know in the comments if you want me to :)

Durability

Durability in the database promises that once a transaction has been successfully committed, the data won’t be lost even if the database crashes or any other scenario happens.

For more understanding of durability refer to:

https://fintechcoddler.blogspot.com/2023/03/clear-your-acid-concepts-with-me_18.html

Soon, I will be sharing the C and D of ACID.


Copyright © 2023 fintechcoddler.blogspot.com


Let's discuss and grow.
Can also let me know what article you want to discuss in the next blog.

Post a Comment (0)
Previous Post Next Post