29 · Database Transactions & Isolation Levels

Part 2 · Storing data · ⏱️ ~25 min read · Prerequisites: 06 · Relational vs NoSQL


🏢 Real-world case studies: how Google Spanner (two-phase locking plus TrueTime), PostgreSQL (Serializable Snapshot Isolation) and Stripe (idempotency keys) keep data correct, in REAL_WORLD_EXAMPLES.md.


🧒 The 30-second version

A transaction is a safety shield: it bundles multiple database reads and writes into a single logical execution. Either everything succeeds (Commit), or if anything goes wrong, everything is completely undone (Abort/Rollback).

Without transactions:

The Isolation Level determines how well concurrent transactions are shielded from each other:

  1. Read Committed: No dirty reads (you only read data that has been permanently committed).
  2. Snapshot Isolation / Repeatable Read: Readers see a consistent snapshot of the database as of the transaction start time; readers never block writers, and writers never block readers.
  3. Serializable: The gold standard. Transactions execute with the mathematical guarantee of running one-at-a-time, completely preventing race conditions like Write Skew.

🍕 Everyday analogy: The Shared Google Doc vs Meeting Rooms

Imagine multiple coworkers booking meeting rooms:

In everyday life In Database Architecture
Peeking at an unsaved draft Dirty Read
Overwriting someone's draft Dirty Write
Two people selling the same item Lost Update
Two doctors logging off simultaneously Write Skew (Phantom Read)
Locking the door while in the room Pessimistic Locking (2PL)
Checking if anyone changed the room before leaving Optimistic Concurrency Control (SSI)

🧩 The Problem It Solves

In high-concurrency production systems, executing transactions without strict isolation triggers catastrophic race conditions:

  1. Lost Updates: Concurrent increments or JSON modifications overwrite each other.
  2. Non-Repeatable Reads: Query 1 reads an account balance of <span class="math-inline">100. Query 2 (in the same transaction 10ms later) reads </span>50 because an external transaction committed in between.
  3. Write Skew: Two concurrent transactions read the same data, make independent decisions based on that data, and write to different rows, violating global business constraints.

🖼️ Picture It: The 4 SQL Standard Isolation Levels

SQL Isolation Spectrum & Concurrency Anomalies

flowchart TD
    subgraph Levels["The Isolation Spectrum (Weakest to Strongest)"]
        L1["1. Read Uncommitted<br/>(Suffers from Dirty Reads)"]
        L2["2. Read Committed<br/>(No Dirty Reads, No Dirty Writes)"]
        L3["3. Snapshot Isolation / Repeatable Read<br/>(MVCC, Consistent Snapshot, Prevents Lost Updates)"]
        L4["4. Serializable<br/>(Guarantees Total Sequential Execution; Prevents Write Skew)"]
    end
    
    L1 --> L2 --> L3 --> L4
    
    style L1 fill:#161b22,stroke:#f85149,stroke-width:2px;
    style L2 fill:#161b22,stroke:#d29922,stroke-width:2px;
    style L3 fill:#161b22,stroke:#58a6ff,stroke-width:2px;
    style L4 fill:#161b22,stroke:#3fb950,stroke-width:2px;

🔢 The Race Conditions & Anomalies Matrix

Isolation Level Dirty Read Lost Update Non-Repeatable Read Write Skew / Phantoms Default In
Read Uncommitted ❌ Allowed ❌ Allowed ❌ Allowed ❌ Allowed Rarely used
Read Committed ✅ Prevented ❌ Allowed ❌ Allowed ❌ Allowed PostgreSQL, Oracle, SQL Server
Repeatable Read (Snapshot) ✅ Prevented ✅ Prevented ✅ Prevented ❌ Allowed MySQL InnoDB
Serializable ✅ Prevented ✅ Prevented ✅ Prevented ✅ Prevented CockroachDB, Google Spanner

⚙️ How It Works Under the Hood

1. Multi-Version Concurrency Control (MVCC)

Modern databases (PostgreSQL, MySQL InnoDB, CockroachDB) implement Snapshot Isolation using MVCC:

2. Preventing Write Skew: Two-Phase Locking (2PL) vs Serializable Snapshot Isolation (SSI)


⚖️ Trade-Offs & Decision Matrix

Strategy Performance / Throughput Concurrency Anomalies Operational Cost Best Use Case
Read Committed Highest Throughput Vulnerable to non-repeatable reads & lost updates Lowest CPU/RAM Standard CRUD dashboards
Snapshot Isolation High Throughput (MVCC) Vulnerable to Write Skew Medium RAM (old row versions) Financial ledgers & carts
Serializable (2PL) Low Throughput (Lock waits) Zero Anomalies High lock contention & deadlocks Rare single-node high-stakes
Serializable (SSI) High on low-conflict Zero Anomalies Abort/retry handling in app code Distributed SQL (Spanner/Cockroach)

🚫 Common Mistakes & Production Pitfalls

  1. Blind Overwrite Instead of Atomic Updates:
    • Pitfall: balance = SELECT balance; balance += 10; UPDATE SET balance = balance;
    • Result: Lost Update under Read Committed.
    • Fix: Use atomic database expressions: UPDATE accounts SET balance = balance + 10 WHERE id = 1; or SELECT FOR UPDATE.
  2. Long-Running Transactions Holding Locks:
    • Pitfall: Opening a database transaction, making an external Stripe API call (taking 1.5s), and then committing.
    • Result: Database connection pool starvation and lock timeouts. Never place third-party HTTP network calls inside database transactions!
  3. Table Bloat in MVCC:
    • Pitfall: High-frequency updates without PostgreSQL VACUUM maintenance.
    • Result: Millions of dead tuple row versions consume disk space and degrade sequential scans.

💻 Try It Yourself

The companion script demo.py simulates concurrent multi-threaded transactions demonstrating:

  1. The Lost Update Anomaly under naive execution vs. atomic Compare-And-Swap (CAS).
  2. The Write Skew Anomaly (the classic Doctors on Call problem) under Snapshot Isolation vs. Serializable enforcement.

Run it directly in your terminal:

python demo.py

🌍 Where You'll See It in Production


🎯 Top 5 Interview Questions & Deep Answers

Q1: "What is Write Skew, and why does Snapshot Isolation fail to prevent it?"

💡 Click to reveal the ideal senior engineering answer

Write Skew occurs when two concurrent transactions read the same data, make a decision based on that premise, and update different rows in a way that violates a global invariant (e.g. the Doctors on Call scenario, or reserving the same conference room). Snapshot Isolation fails because it only detects Dirty Writes on the same row (first-committer-wins). Because Transaction A updates Row 1 and Transaction B updates Row 2, Snapshot Isolation sees zero row conflict and allows both to commit! Write Skew can only be prevented by:

  1. Explicit Locking: SELECT ... FOR UPDATE (Pessimistic).
  2. Serializable Isolation: SSI tracks read-write dependency cycles and aborts one transaction.

Q2: "How does Multi-Version Concurrency Control (MVCC) eliminate read locks?"

💡 Click to reveal the ideal senior engineering answer

Instead of overwriting rows in-place, MVCC creates a new timestamped version of the row for every mutation. When a transaction starts, it receives a logical snapshot watermark. Any read operation simply seeks row versions created before that watermark, ignoring later writes. Because readers only inspect immutable past versions, readers never acquire locks and never block concurrent writers.


Q3: "What is the Lost Update problem, and what are 3 ways to prevent it?"

💡 Click to reveal the ideal senior engineering answer

A Lost Update happens when Transaction 1 and Transaction 2 both read value V, compute new values independently, and write back, with the second write blindly overwriting the first. Three ways to prevent it:

  1. Atomic Database Operations: UPDATE counters SET val = val + 1 WHERE id = 10;
  2. Pessimistic Locking: SELECT val FROM counters WHERE id = 10 FOR UPDATE;
  3. Optimistic Locking (Compare-And-Swap): Add a version column: UPDATE counters SET val = 20, version = 2 WHERE id = 10 AND version = 1; If 0 rows are updated, the app retries.

Q4: "Why should you never execute external HTTP network calls inside a database transaction block?"

💡 Click to reveal the ideal senior engineering answer

Database transactions hold row-level locks and occupy a dedicated connection from the server's connection pool (typically limited to 50–200 connections). External HTTP calls (e.g., Stripe, Twilio) can take seconds or time out. Holding a database transaction open during network I/O exhausts the connection pool, locks other transactions, and triggers cascading system failure. Always execute database writes and external HTTP calls in separate decoupled steps using the Transactional Outbox Pattern.


Q5: "What is the difference between Two-Phase Locking (2PL) and Two-Phase Commit (2PC)?"

💡 Click to reveal the ideal senior engineering answer
  • Two-Phase Locking (2PL) is a concurrency control mechanism on a single database ensuring Serializable isolation: transactions acquire locks during the growing phase and release them during the shrinking phase.
  • Two-Phase Commit (2PC) is an atomic distributed consensus protocol across multiple independent databases/nodes (Prepare phase → Commit phase) ensuring all nodes commit or all abort. They are completely different concepts despite sharing the "Two-Phase" name!

📚 Go Deeper