SQL · Module 08: Indexes, ACID Transactions & OptimizationTutorial 23 of 24
All Tutorials·~1.5 hrs

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.

01 Intuition: Plain-English mental model
02 Visuals: Step-by-step diagrams
03 Practice: Clean examples & tradeoffs

1. Why Transactions Exist (The ₹1,000 Bank Transfer)

Transferring ₹1,000 from Aarav to Meera takes 2 SQL statements:

  1. Deduct ₹1,000 from Aarav's account.
  2. 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):

SQL
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

LetterPropertyPlain-English Meaning
AAtomicityAll-or-nothing: Either every step inside BEGIN...COMMIT succeeds, or ALL steps roll back cleanly.
CConsistencyTransactions never violate constraints (CHECK balance >= 0, PRIMARY KEY, FOREIGN KEY).
IIsolationConcurrent transactions don't see each other's half-finished dirty writes.
DDurabilityOnce 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 UPDATE creates 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 LevelDirty Read?Non-Repeatable Read?Phantom Read?Speed / Default In
Read Uncommitted⚠️ Possible⚠️ Possible⚠️ PossibleFastest (rarely used)
Read Committed✅ Prevented⚠️ Possible⚠️ PossiblePostgreSQL & Oracle default
Repeatable Read✅ Prevented✅ Prevented⚠️ Possible (prevented in Postgres MVCC!)MySQL InnoDB default
Serializable✅ Prevented✅ Prevented✅ PreventedSlowest (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 SELECT twice inside one transaction and getting different values because another transaction committed an UPDATE in between.
  • Phantom Read: Running the same range query (WHERE dept = 'Eng') twice and seeing a brand-new row appear because another transaction committed an INSERT.

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

Q1.Which ACID property guarantees that a committed bank transfer survives a sudden server power outage?
Q2.What is the default transaction isolation level in PostgreSQL vs MySQL (InnoDB)?

Keep Exploring: Related Lessons & Tools

All Courses →