What Are ACID Transactions?
We perform several database transactions every day, such as wire transferring money. While it sounds like a simple procedure, it has to be secure from any errors; otherwise, users can incur detrimental financial consequences. ACID (Atomicity, Consistency, Isolation, and Durability) is a set of principles that add security, reliability, persistence, consistency and ensure data integrity.
What is a database transaction?
A transaction occurs when a functionality is executed using a database. This way, a database is maintained in a consistent state which is either transaction completed or failed.
A transaction can involve a single or multiple CRUD (Create, Read, Update and Delete) operations. For instance, the process of transferring money across a bank account involves a transaction. Whenever a user initiates a transaction, the specified amount has to be removed from their account and needs to be added to the recipient’s account. This entire state chain comprises a transaction. If all steps involved are executed then a database commit takes place which is one of the end states. The other state is a rollback which makes sure the database snapshot is exactly similar to how it was before the transaction was communicated.
Figure 1: Transaction as a logical operation unit.
A transaction can be described as a set of operations which form a “logical operation unit” as shown in figure 1. It could be as simple as inserting a value into a database.
When all components or steps of a transaction execute successfully and intended changes are made, a COMMIT() command is executed which means that all changes made are to be permanent in the database. If at any point execution fails in any component then that means it failed as a whole unit and any changes made are to be undone via a ROLLBACK() command.
A.C.I.D. Transactions: Atomicity, Consistency, Isolation, and Durability
It is critically important that a transaction needs to be reliable and the database stays accurate and consistent. ACID properties are a set of principles that are used to ensure this.
- Atomicity - means “all or nothing”. So if the entire transaction is successful only then changes are committed otherwise no change takes place in the database state.
- Consistency - the execution of transactions maintains consistency constraints in a database, no data integrity, constraint, or formatting is invalid. For instance, entering words in a phone number field in an online form will result in a rejected request if the database is to be consistent. This way data integrity is kept safe from any compromise as it can lead to loss of valuable information and compromise analytical decision making.
- Isolation - A transaction should be isolated from other concurrent transactions being executed. No transaction currently executing considers a state with changes from a transaction that hasn’t been completed yet. Although this might result in latency, it is extremely vital for maintaining an accurate database in that no transactions interfere with each other at one given instance in a database environment.
- Durability - the changes which take place consequent to successful transactions are permanent and can endure any system crash, power outage, or any downtime scenario.
Why ACID is good to have?
Companies use data for running business logic, quality checks, and daily operations. Incorrect or corrupted data can threaten business models and damage growth prospects.
ACID principles are needed to :
- Make sure the transactions work successfully and no unintended changes persist in the database.
- Concurrency proof - removes the need for exhaustive conditioning based on possible scenarios caused by operations from a different transaction; transactions can be treated as isolated and sequential units even though they run concurrently.
- Ensure data integrity which is critical for extracting optimal value; corrupted data can potentially turn obsolete or require long pre-processing operations.
It is immensely important for any change in the state of a database to take place in a safe way that does not compromise the integrity and reliability of the data. ACID principles ensure the transactions do not result in unintended changes, loss of information, or invalid data.