8.2 ACID Transactions, Locks, MVCC & Isolation Levels
Master how databases protect financial money transfers: ACID properties explained simply, how MVCC lets readers never block writers, the 4 Isolation Levels, and row-level locking with FOR UPDATE.
1. Why Transactions Exist (The ₹1,000 Bank Transfer)
Transferring ₹1,000 from Aarav to Meera takes 2 SQL statements:
- Deduct ₹1,000 from Aarav's account.
- Add ₹1,000 to Meera's account.
What if the server crashes right between Step 1 and Step 2? Without a transaction, ₹1,000 vanishes into thin air!
Wrapping both statements inside BEGIN ... COMMIT makes them atomic (all-or-nothing):
BEGIN;
-- Lock both rows in consistent order (by account_id) to prevent deadlocks!
SELECT balance FROM accounts WHERE account_id IN (101, 102) ORDER BY account_id FOR UPDATE;
UPDATE accounts SET balance = balance - 1000 WHERE account_id = 101 AND balance >= 1000;
UPDATE accounts SET balance = balance + 1000 WHERE account_id = 102;
COMMIT; -- If anything fails before COMMIT, run ROLLBACK to undo Step 1!
2. ACID Properties Explained in Plain English
| Letter | Property | Plain-English Meaning |
|---|---|---|
| A | Atomicity | All-or-nothing: Either every step inside BEGIN...COMMIT succeeds, or ALL steps roll back cleanly. |
| C | Consistency | Transactions never violate constraints (CHECK balance >= 0, PRIMARY KEY, FOREIGN KEY). |
| I | Isolation | Concurrent transactions don't see each other's half-finished dirty writes. |
| D | Durability | Once COMMIT returns success, data is safely written to disk (Write-Ahead Log / WAL) — even if the power cord is yanked 1ms later! |
3. How MVCC Works (Why Readers Don't Block Writers!)
In old databases, if Transaction A was updating a row, Transaction B couldn't even read it!
Modern databases (PostgreSQL, MySQL InnoDB) use MVCC (Multi-Version Concurrency Control):
- Instead of overwriting a row in place, an
UPDATEcreates a new version of the row tagged with its transaction ID. - Readers simply read the consistent snapshot from before the uncommitted write began — readers never block writers, and writers never block readers!
4. The 4 SQL Isolation Levels (System Design & Backend Classic!)
| Isolation Level | Dirty Read? | Non-Repeatable Read? | Phantom Read? | Speed / Default In |
|---|---|---|---|---|
| Read Uncommitted | ⚠️ Possible | ⚠️ Possible | ⚠️ Possible | Fastest (rarely used) |
| Read Committed | ✅ Prevented | ⚠️ Possible | ⚠️ Possible | PostgreSQL & Oracle default |
| Repeatable Read | ✅ Prevented | ✅ Prevented | ⚠️ Possible (prevented in Postgres MVCC!) | MySQL InnoDB default |
| Serializable | ✅ Prevented | ✅ Prevented | ✅ Prevented | Slowest (may throw serialization retry errors) |
What Those 3 Anomalies Actually Mean
- Dirty Read: Reading uncommitted data from another transaction that later rolls back!
- Non-Repeatable Read: Running the exact same
SELECTtwice inside one transaction and getting different values because another transaction committed anUPDATEin between. - Phantom Read: Running the same range query (
WHERE dept = 'Eng') twice and seeing a brand-new row appear because another transaction committed anINSERT.
Deadlock Prevention Pro Tip: If Tx 1 locks Row A then Row B, while Tx 2 locks Row B then Row A, they deadlock! Always lock rows in a consistent sorted order (
ORDER BY id FOR UPDATE) so deadlocks can never form a cycle.
5. Quick Check Your Understanding
Module 8.2 Quick Quiz
Keep Exploring: Related Lessons & Tools
All Courses →Convert Unix epoch seconds/milliseconds to ISO-8601 and local timezones, and bulk-generate UUID v4, time-ordered UUID v7, and sortable ULIDs.
Paste any Python, JS, Java, or C++ snippet to estimate Time & Space complexity, or enter input size N to see which algorithms pass in 1s.