Decode ACID Properties : Basics and Benefits

 


What is ACID and Why is it Important for Database Transactions?

Database transactions are operations that modify the data stored in a database i.e. CRUD. Transactions are required for the reliability, consistency and accuracy of the data in the database. But, not all transactions are created equal. Some transactions may have different requirements and expectations than others, depending on the nature and purpose of the data and the application.

One way to classify and evaluate transactions is by using the ACID model. ACID stands for Atomicity, Consistency, Isolation and Durability. These are four properties that guarantee that a transaction is executed in a safe and correct manner, regardless of any errors, failures or concurrency issues that may occur. We will try to explain what each of these properties means, why they are important, and how they can be achieved in a database system.

Atomicity

Atomicity means that a transaction is either executed completely or not at all. There is no state in between. You don't see a partial completion of a transaction. For example, if you want to transfer money from one bank account to another, expectation is, either both accounts are updated with the correct amounts, or none of them are changed at all. You don't want to end up with a situation where the money is deducted from one account but not added to the other, or vice versa.

Atomicity prevents partial updates or data loss in case of failures or errors. If something goes wrong during the execution of a transaction, such as a power outage, a network failure, or an application bug, the database system should be able to detect it and roll back the transaction to its original state before it started. This way, the database remains consistent and no data is corrupted or lost.

To achieve atomicity, database systems use various techniques, such as logging, locking, checkpoints and rollback segments. These techniques allow the database system to keep track of the changes made by a transaction, lock the resources involved in the transaction, save the state of the database before the transaction begins, and undo the changes if the transaction fails.

Consistency

Consistency means that a transaction preserves the validity and integrity of the database state. A database has certain rules and constraints that define what constitutes a valid state. For example, a database may have primary keys that uniquely identify each record, foreign keys that link records from different tables, check constraints that limit the range or format of values in a column, or business rules that enforce some logic or calculation on the data.

Consistency prevents violations of constraints, rules or business logic in the database. If a transaction tries to insert, update or delete data that would break any of these rules or constraints, the transaction should fail and abort. The database should not allow any invalid or inconsistent data to be stored or retrieved.

To achieve consistency, database systems use various techniques, such as validation checks, triggers and stored procedures. These techniques allow the database system to verify and enforce the rules and constraints on the data before and after a transaction is executed. 

Isolation

Isolation means that concurrent transactions do not interfere with each other. Transactions are concurrent when they are executed at or near the same time by different users or applications. For example, if two customers try to book the same flight seat or hotel room at the same time, they are executing concurrent transactions.

Isolation prevents anomalies such as dirty reads, non-repeatable reads or phantom reads in the database. A dirty read occurs when a transaction reads data that has been modified but not committed by another transaction. A non-repeatable read occurs when a transaction reads the same data twice but gets different results because another transaction has modified and committed the data in between. A phantom read occurs when a transaction reads a set of data that matches some criteria but gets different results because another transaction has inserted or deleted some records that match or do not match the criteria in between.

To achieve isolation, database systems use various techniques, such as locking, timestamps and multi-version concurrency control (MVCC). These techniques allow the database system to control and coordinate the access and modification of data by concurrent transactions. 

Durability

Durability means that the effects of a committed transaction are permanent and persistent in the database. A committed transaction is one that has been successfully executed and verified by the database system. Once a transaction is committed, it should not be reversed or undone by any subsequent event.

Durability prevents data loss or corruption in case of power failures, system crashes or restarts. If any of these events happen after a transaction is committed, the database system should be able to recover and restore the data to its latest committed state.

To achieve durability, database systems use various techniques, such as write-ahead logging (WAL), checkpoints and backups. These techniques allow the database system to record and save the changes made by a transaction to persistent storage devices (such as disks), periodically synchronize the data in memory and on disk, and create copies of the data for recovery purposes.

Conclusion

ACID properties are essential for ensuring the reliability, consistency and accuracy of database transactions. They provide a framework for designing and evaluating database systems and applications that deal with sensitive and critical data. By following the ACID model, database systems and applications can avoid many common problems and errors that may compromise the quality and integrity of the data.


Scalable Alternative to ACID