What is ACID (atomicity, consistency, Isolation, and durability)?

Anshuman Pattnaik
4 min readDec 15, 2022

ACID is a set of properties in database transactions that guarantees data integrity with fault tolerance and ensures it keeps the database in a proper state in the event of unexpected errors. Each database operation that satisfies ACID properties is called a transaction. For example — a high-level function transfers funds from one bank account to another, deducts money from one account, and credits it to another is called a single transaction, and ACID properties guarantee that both accounts should have the same initial amount in case of failures.

In this article, we will explore what a database transaction is, the architecture of a database transaction, and how ACID properties resolve database transaction issues.

What is a database transaction?

Databases are responsible for processing millions of concurrent requests per second and allowing multiple clients to access the same system simultaneously. In other words, a sequence of multiple database transactions is executed on a database as a single logical unit that serves all the operations as a whole. Once the transaction is committed, the changes are applied to the database, and the results are saved.

Let’s look at a typical money transfer example of a database transaction.

There are two accounts, Accounts (A & B), and we need to transfer $100 from Account A to Account B. Let’s assume account A and B has $50 each.

Database Transaction Operations:

  1. Check account A’s balance to see whether it has a minimum balance to transfer money.
  2. If it has the balance, then subtract $100 from account A.
  3. Check account B’s balance before it receives a $100 amount from account A.
  4. Add a $100 credit to account B if it still needs to receive the amount.

In the above example, if there is a system failure during the transaction, then the database operation will be rollback to its original state, and both accounts will have the initial amount. Because when the database runs this transaction, it performs as a single logical unit and commits only one transaction.

The architecture of database transactions

The database transaction has several states to complete an operation and typically one of the following:

Database Transaction Architecture
  1. Active State: In every execution of a transaction, it is the first state, and it remains active until the operation fails or is terminated.
  2. Partially committed State: Transaction is initiated, and a change has been executed, but the database still needs to commit the whole change on disk instead, it stores the data in the memory buffer.
  3. Committed State: In this state, the database executes the final transaction and saves the results in the database. So, we cannot rollback the transaction at this state.
  4. Failed State: When a particular transaction fails or is aborted from an active state, it enters a failed state.
  5. Terminated State: The final transaction state, either committed or aborted. It is the end of the database transaction.

What are ACID properties?

To maintain the consistency of a database transaction, ACID properties are followed before the execution of a transaction. Let’s learn about all these properties and their advantages.

Atomicity

Atomicity guarantees that all the operations performed to complete a transaction should be treated as a single logical unit, either committed or failed. In that way, we can identify the database state in case of failure and rollback to the original state.

Consistency

Consistency guarantees that all transactions should comply with database constraints. The entire transaction should fail if the data falls into any illegal state.

For example, In a money transfer operation, there are many constraints: the amount should be a positive integer or the overdraw of the amount, the transaction should fail, etc. Because ACID transaction violates consistency properties and transaction fails.

Isolation

To avoid interference between transactions, Isolation guarantees that all transaction operations should perform in an isolated environment so that when multiple transactions run concurrently, they won’t interrupt each other.

For example, there are only two tickets on a booking website. If two people buy the tickets simultaneously, the transaction should run in Isolation so that when both transactions are complete, each person should have one ticket instead of two.

Durability

Durability guarantees that once the transaction operations are completed, and results are stored in the database, they should be persisted. In case of failures or system crashes, data within the system will remain permanent on the disk. However, If the database is lost in case of a crash, the recovery manager guarantees the database's long-term possibilities.

Conclusion

Databases are the key elements to many services and significantly impact our ecosystem. To maintain the database state consistency, it's essential to ensure the integrity and accuracy of the data that ACID transactions provide with different sets of properties. And it’s hard to imagine, without ACID properties, how we can manage or use services and platforms daily.

I hope you enjoyed reading this article, which gave you an insight into ACID properties. If you think this article helped you in anyways then feel free to share it with your friends. If you find any difficulties while following this tutorial, please let me know in the comment section.

--

--

Python | Application Security | Web Security | Cybersecurity | Software Development