Database Isolation Levels, ACID & Concurrency Anomalies
The On-Call Doctor Anomaly (Write Skew under Snapshot Isolation)
Test your architecture intuition: Pitch a 7-axis solution, survive two aggressive reviewer objections, and inspect the staff-level Teacher Gold Answer.
1. What It Is & Why It Exists
The Core Problem: Concurrency vs. Consistency Trade-off
In high-throughput transactional systems (e.g., banking transfers, inventory reservations, ride dispatch), thousands of concurrent database transactions execute simultaneously. Without isolation controls, interleaving read and write operations creates data corruption anomalies:
- Dirty Reads: Reading uncommitted data that is subsequently rolled back.
- Non-Repeatable Reads: Reading different values for the same row within a single transaction because another transaction modified and committed it.
- Phantom Reads: A query scanning a range of rows receives different row sets because another transaction inserted or deleted matching rows.
- Write Skew: Concurrent transactions read overlapping data sets, satisfy business constraints independently, and write disjoint data sets that collectively violate a global invariant.
- Lost Updates: Two concurrent transactions read a balance (10 and 110 and $120, losing one transaction's increment.
Synthesizing vector architecture diagram...
2. Concurrency Anomalies & ANSI SQL Lattice
Synthesizing vector architecture diagram...
Read each box in the "Distributed Concurrency Anomalies" panel as a short story between two transactions, then follow its dotted arrow to the weakest level that prevents it in the "Isolation Level Protections" panel. A dirty read (T2 sees T1's uncommitted change, then T1 aborts) is fixed by Read Committed. A non-repeatable read (T1 reads the same row twice and gets different data) and a lost update (two read-modify-writes where the second overwrites the first) are fixed by Repeatable Read. Phantom reads (a re-run range query finds new rows) and write skew (two transactions each keep a rule true alone, but break it together) need Serializable. The arrows only go downward: each level also prevents everything the levels above it prevent, and each step costs more locking or more aborted transactions. These are the ANSI minimums: real engines often do more at the same level (PostgreSQL's Repeatable Read also prevents phantoms), as the matrix below shows.
Comprehensive Isolation Levels Matrix
| Isolation Level | Dirty Read | Non-Repeatable Read | Lost Update | Phantom Read | Write Skew | Implementation Engine |
|---|---|---|---|---|---|---|
| Read Uncommitted | ⚠️ Occurs | ⚠️ Occurs | ⚠️ Occurs | ⚠️ Occurs | ⚠️ Occurs | Dirty page memory reads |
| Read Committed | ❌ Prevented | ⚠️ Occurs | ⚠️ Occurs | ⚠️ Occurs | ⚠️ Occurs | MVCC (New snapshot per SQL query statement) |
| Repeatable Read | ❌ Prevented | ❌ Prevented | ⚠️ Engine-dependent: PostgreSQL aborts the second writer (40001); MySQL InnoDB does not detect it for plain read-then-write, you need FOR UPDATE or an atomic SET x = x + ? | ⚠️ ANSI allows it. PostgreSQL prevents it (snapshot). InnoDB: plain reads use the snapshot, locking reads take next-key (gap) locks | ⚠️ Occurs | MVCC (snapshot taken at the first statement in PostgreSQL, the first consistent read in InnoDB) |
| Snapshot Isolation (SI) | ❌ Prevented | ❌ Prevented | ❌ Prevented | ❌ Prevented | ⚠️ Occurs | MVCC snapshot + first-committer-wins on write-write conflicts |
| Serializable | ❌ Prevented | ❌ Prevented | ❌ Prevented | ❌ Prevented | ❌ Prevented | Strict 2PL (Pessimistic) or SSI (Optimistic) |
Unlock Complete Architecture & Production Runbooks
You have explored the free architectural preview (~37%). Spend 1 Coin to unlock the remaining 5 production deep-dive sections for a full 24 hours.