Transactions
Introduction to Transactions with ACID

I am working as freelancer. I am a experienced full stack web developer. My lovely stack is MERN but I am open for new tech. I love to work with teammates for achieve goals. I love researches to find specific new things.
Databases are improving day by day by many organizations. Modern databases give a lot of storage functionality and scalability. The emergence of new technology databases like MongoDB and Cassandra offer different approaches to data storage and processing, but these do not support transactions effectively compared to RDBMS.
It has no question that the emergence of non-SQL databases like MongoDB and Cassandra has given more scalability, performance and flexibility but they may lack support for transactions that RDBMS systems provide.
In this blog, we will look that what transactions are and what determines a database as a transaction.

What are Transactions?
Transactions are a process of transfer that can be either empty or full means it can be in the initial state or final state.
For example, Rahul buys a computer from a shop. He uses his online account to pay money.

There can be three stages of transferring money from Rahul's account to the shopkeeper's account.
Money deduction from Rahul's account
Wire transfer of money
Money is credited to the shopkeeper's account.
Let's look steps of transferring money.
At the first stage, Rahul confirms the transfer of money and after authorization money deducts from his account, but the money will not credit at the same time in the shopkeeper's account.

In the second stage, deducted money goes through a wire transfer from Rahul's account to the shopkeeper's account. This process completes so many tasks like verifying account details, checking sufficient balance and changing the status of a transfer. When these tasks are completed and the wire transfer is completed then the transaction process reaches the next stage.

In the final stage, deducted money from Rahul's account was credited to the shopkeeper's account.

If the final stage fails due to some technical issues or other problems then the transaction process will be rolled back and deducted money will be returned back to Rahul's account. In this transfer process only two conditions can take place either money will be in Rahul's account or the shopkeeper's account.
Transactions are either all or none.
ACID Properties
If a database follows transactions, it must follow the ACID properties. ACID stands for Atomicity, Consistency, Isolation and Durability.
Let's see ACID properties one by one.

Atomicity
Atomicity means an atom can not be divided into parts. We know that an atom is the smallest part of the universe. Context to transactions in databases, a transaction also can not be divided into steps either all processes of a transaction will be fully completed or all must fail.
A transaction can be fully executed or fully rolled back.

Consistency
Consistency means a state of a system remains unchanged before and after transactions.
For example, Bandhan Bank has many accounts of individuals. In the bank, customer A has 5000 rupees and customer B has 1000 rupees in his accounts. Customer A sends 1000 rupees to Customer B's account. After the successful execution of the transfer, Customer A has 4000 rupees and Customer B has 2000 rupees. If we check the total amount of both accounts of A and B, we will find that before the transaction and after the transaction total amount of both accounts does not change. Besides this transaction, all other accounts of Bandhan Bank remain unchanged. This is called consistency the state of transactions before execution and after successful execution remains unchanged.

Isolation
In today's time, people use so many online platforms like Gpay, AmazonPay, and Paytm to make payments at shops and other places. A shopkeeper does not suggest to his customers to make payment one by one. Because at the same time, many transactions are happening and every transaction does not affect any other transaction. This ensures that a transaction is successfully executed without any interference by any other transaction which is called transaction isolation.

Durability
If a transaction is successfully executed, it cannot be rolled back means it can not be reversed.
For example, A customer pays money to a shopkeeper's account. After payment of money in the shopkeeper's account, it has no possibility that paid amount will return to the customer's account. It explains the reliability and integrity of a transaction system. This is called the durability of a transaction.




