Transactions What,Why and How ?

Table of contents

No heading

No headings in the article.

In this article, we will talk about transactions and the acronym ACID. So let's first understand what and why transactions ?

Q)What is a transaction ?

It is a way for an application to group several reads and writes together into a logical unit.

Q)What is the purpose of transactions ?

Transactions aim to simplify the programming model for apps accessing a DB. It just means that certain potential errors and concurrency issues will be taken care by DB and not by app(also called safety guarantees).

Q) What are the safety guarantees that transactions provide ?

It's well known by the acronym ACID. So, let's dig deeper into each one of them one by one.

  1. Atomicity/Abortability -> It describes what happens if a client wants to make several writes, but a fault occurs midway. Some of the faults that can occur are as given below :-

    a)A process crashes.

    b)Integrity constraint violates.

    c)Network interrupted etc.

    Ability to abort a transaction on error and have all the writes from that transactions discarded is atomicity/abortability. One problem that can happen if an error occurs halfway during a transaction is when we retry after a failure. As we do not have the mechanism to detect what changes have already occured we can have duplicate data.

  2. Consistency -> Certain statements about the data that must always be true(also called invariants). It is a property of the application not DB. The apps define what data is valid or invalid, the database only stores it.

  3. Isolation -> Concurrently executing transactions cannot step on each other's toes. The basic example for this is 2 threads reading the same counter variable and incrementing it also popularly referred as the Counter problem.

  4. Durability -> Promise that once a transaction is committed successfully, even if there is a hardware fault or system crash the data will not be lost. 100% durability does not exist, what would happen if hardware and backup all are destroyed at the same time. So writing to disk, replicating to remote machines and backups should be used together.