ACID properties of Database Explained
Tags: webdev, programming, cloud, database
Published: 2023-01-05 18:58:09

ACID (Atomicity, Consistency, Isolation, Durability) is a set of properties that define the behavior of a SQL database. These properties ensure that database transactions are processed reliably and consistently, even in the face of system failures or concurrent access by multiple users.

### Atomicity
Atomicity ensures that a database transaction is treated as a single unit of work, and either all of its operations are completed, or none of them are. If a transaction fails, any changes made by the transaction are rolled back, leaving the database in a consistent state.

Example: Imagine that you are transferring money from one bank account to another. This process involves two steps: withdrawing money from the first account, and depositing it into the second account. Atomicity ensures that these two steps are treated as a single unit of work, and either both are completed, or neither are. For example, if the withdrawal is successful but the deposit fails, the entire transaction will be rolled back and the money will not be transferred.

### Consistency
Consistency ensures that a database is always in a valid state, and that any transaction that is committed leaves the database in a consistent state. This means that a transaction cannot violate any of the database's constraints or rules.

Example: Imagine that you are creating a new user account in a database. The database has a constraint that requires all user names to be unique. Consistency ensures that the database remains in a consistent state, and that any transaction that is committed leaves the database in a consistent state. This means that if you try to create a new user with a name that is already in use, the transaction will fail and the database will not be changed.

### Isolation:
Isolation ensures that concurrent transactions do not interfere with each other. Each transaction operates on a snapshot of the database, and the changes made by one transaction are not visible to other transactions until the transaction is committed. This prevents data from being corrupted by concurrent access.

Example: Imagine that two users are trying to update the same record in a database at the same time. Isolation ensures that the changes made by one user are not visible to the other user until the transaction is committed. For example, if one user changes the name of the record from "John" to "Jane", and the other user changes the name from "John" to "Jack", the final name of the record will be either "Jane" or "Jack", but not both.

### Durability
Durability ensures that once a transaction is committed, its changes are permanent and cannot be lost due to system failures. This is typically achieved through the use of a database log, which records the changes made by each transaction, and can be used to recover the database in the event of a failure.

Example: Imagine that you have just committed a transaction that updates a record in a database. Durability ensures that the changes made by the transaction are permanent and cannot be lost due to system failures. For example, if the database system crashes before the transaction is written to the log, the log can be used to restore the database to a consistent state and commit the transaction once the system is back up. This ensures that the changes made by the transaction are not lost.

Together, these properties ensure that a SQL database can be used to store and process data reliably and consistently, even in the face of system failures and concurrent access. They are an essential part of any database management system, and are what allow developers to build applications that depend on the integrity and consistency of the data stored in the database.