ACID Properties
About
ACID stands for Atomicity, Consistency, Isolation, and Durability, which are the key properties ensuring data integrity, reliability, and correctness in a database system. These properties are fundamental to transaction management in Relational Database Management Systems (RDBMS) such as MySQL, PostgreSQL, Oracle, and SQL Server.
What is a Transaction?
A transaction is a set of database operations that must be executed as a single unit of work. It follows the all-or-nothing rule—either all operations in the transaction succeed, or none of them take effect.
Example of a Transaction (Money Transfer)
Imagine transferring ₹1000 from Alice’s bank account to Bob’s bank account:
Debit ₹1000 from Alice’s account
Credit ₹1000 to Bob’s account
1. Atomicity
Atomicity ensures that a transaction is treated as a single, indivisible unit.
If all operations in the transaction succeed, the transaction is committed.
If any operation fails, the entire transaction is rolled back.
Example
Imagine transferring ₹500 from Alice’s account to Bob’s account:
Debit ₹500 from Alice’s account
Credit ₹500 to Bob’s account
Scenario 1: Successful Transaction
✅ Both steps complete → Transaction is committed.
Scenario 2: Failure in Step 2 (e.g., system crash)
❌ If only Alice's account is debited but Bob's account is not credited, the system must roll back the entire transaction to prevent inconsistencies.
Implementation in SQL (Atomicity using Transactions)
If an error occurs:
2. Consistency
Consistency ensures that a database moves from one valid state to another. A transaction should not violate any database constraints, rules, or integrity conditions.
Example
A transaction should not leave the database in an invalid state.
If the bank rule says "Account balance cannot be negative", then a transaction must enforce this rule and not allow negative balances.
Scenario
❌ If Alice has ₹400 and tries to transfer ₹500, the transaction should fail, ensuring data consistency.
SQL Example (Ensuring Consistency with Constraints)
This prevents transactions that would cause negative balances.
Real-World Example of Consistency
In an e-commerce app, if we add an item to our cart but it is out of stock, the system prevents the purchase.
In a bank, if a transfer makes an account negative, the transaction is not allowed.
3. Isolation
Isolation ensures that multiple transactions executing at the same time do not affect each other.
Isolation -> Concurrent Transactions Don’t Interfere
Problems in Concurrent Transactions
The problems that occur when multiple transactions run at the same time.
1. Dirty Read
A transaction reads uncommitted data from another transaction.
If the first transaction rolls back, the second transaction has read incorrect data.
Example of Dirty Read
Transaction A updates Alice’s balance from ₹5000 to ₹4000, but has not committed.
Transaction B reads ₹4000 (uncommitted value).
Transaction A rolls back (Alice’s balance goes back to ₹5000).
But Transaction B already used ₹4000, leading to incorrect calculations.
2. Non-Repeatable Read
A transaction reads the same row twice but gets different values.
Another transaction modifies and commits changes between the two reads.
Example of Non-Repeatable Read
Transaction A reads Alice’s balance (₹5000).
Transaction B updates Alice’s balance to ₹4000 and commits.
Transaction A reads Alice’s balance again → Now it is ₹4000 (inconsistent data!).
3. Phantom Read
A transaction reads a set of rows that changes when it reads again.
Another transaction inserts or deletes rows, causing different results.
Example of Phantom Read
Transaction A selects all employees with salary > ₹10,000 (5 rows).
Transaction B inserts a new employee with ₹12,000 salary and commits.
Transaction A runs the same query again but now gets 6 rows instead of 5.
SQL Isolation Levels
To control these issues, SQL provides four isolation levels.
Read Uncommitted
❌ Allowed
❌ Allowed
❌ Allowed
Read Committed
✅ Prevented
❌ Allowed
❌ Allowed
Repeatable Read
✅ Prevented
✅ Prevented
❌ Allowed
Serializable
✅ Prevented
✅ Prevented
✅ Prevented
1. Read Uncommitted (Lowest Isolation Level)
Allows: Dirty Reads, Non-Repeatable Reads, Phantom Reads.
Transactions can read uncommitted data from other transactions.
Fastest but least safe – used when speed is more important than accuracy.
Example: Read Uncommitted in SQL
A transaction can read changes from other uncommitted transactions.
Risky for financial transactions (e.g., banking, e-commerce).
2. Read Committed (Default in Most Databases)
Prevents: Dirty Reads Allows: Non-Repeatable Reads, Phantom Reads
Transactions can only read committed data.
A row being modified is locked until the transaction commits.
Example: Read Committed in SQL
A transaction waits until other transactions commit before reading.
Used in PostgreSQL, Oracle (default level).
3. Repeatable Read (Prevents Non-Repeatable Reads)
Prevents: Dirty Reads, Non-Repeatable Reads Allows: Phantom Reads
Ensures consistent reads for a single transaction.
Locks rows being read so other transactions cannot modify them.
Used in MySQL (default level).
Example: Repeatable Read in SQL
Other transactions cannot modify the row until commit.
Does NOT prevent Phantom Reads (new rows can still be inserted).
4. Serializable (Highest Isolation Level)
Prevents: Dirty Reads, Non-Repeatable Reads, Phantom Reads
Full transaction isolation – transactions are executed one after another.
Uses locks or MVCC (Multi-Version Concurrency Control).
Slowest but safest – used in critical systems (e.g., financial applications).
Example: Serializable in SQL
Prevents all concurrent modifications.
Other transactions must wait until the first transaction finishes.
Comparison of Isolation Levels
Read Uncommitted
Fastest (Least Safe)
Log analysis, testing, caching
Read Committed
Good Balance
Most OLTP databases, financial transactions
Repeatable Read
Slower, safer
E-commerce, inventory management
Serializable
Slowest, safest
Banking, flight reservations
Example
Imagine two users trying to book the last movie ticket at the same time:
User A sees 1 available seat
User B sees 1 available seat
Both users book at the same time → Double Booking Issue! ❌ Isolation prevents this.
SQL Example Using Isolation Levels
We can prevent double booking by locking the row:
This ensures only one user can book the seat.
4. Durability
Durability ensures that once a transaction is committed, it remains in the database even in case of a system crash.
Data is permanently stored on disk.
Even if the system fails, data can be recovered.
Example
Online Purchase Confirmation
We buy a mobile phone online.
Payment is processed.
System crashes.
When the system restarts, our order should still be confirmed.
How Databases Ensure Durability?
Write-Ahead Logging (WAL): Changes are written to a log before applying to the database.
Checkpointing: The database periodically saves its current state.
Redo Logs: Transactions are stored so they can be replayed after a crash.
SQL Example of Durability (WAL in PostgreSQL)
This ensures that even if the system crashes, committed transactions are not lost.
Last updated
Was this helpful?