Skip to content

Chapter 12: MVCC & Isolation Levels


Metadata Card

DimensionValue
Difficulty(Intermediate)
PrerequisitesChapter 11 (ACID), understanding of concurrent transactions
KeywordsMVCC, isolation levels, dirty read, non-repeatable read, phantom read, snapshot isolation, serializable
Code LanguageSQL, Python

Your Progress

"Administrators started using 'copy notebooks' to avoid interfering with each other — the art of MVCC and isolation levels."

The Three Anomalies

AnomalyDescriptionExample
Dirty ReadRead uncommitted dataT1 writes, T2 reads before T1 commits (T1 may rollback)
Non-Repeatable ReadSame query returns different results within a transactionT1 reads row, T2 updates row, T1 reads again — different value
Phantom ReadSame query returns different rows within a transactionT1 reads rows matching predicate, T2 inserts, T1 reads again — new row appears

SQL Standard Isolation Levels

LevelDirty ReadNon-RepeatablePhantom
READ UNCOMMITTEDPossiblePossiblePossible
READ COMMITTITTEDPreventedPossiblePossible
REPEATABLE READPreventedPreventedPossible
SERIALIZABLEPreventedPreventedPrevented

MVCC — Multi-Version Concurrency Control

Instead of locking, MVCC maintains multiple versions of each data item:

Time ──────────────────────────────────────────→
Row "balance=100"

  │  T1 updates to 200
  │  └── Version 1: balance=100 (readable by older transactions)
  │      Version 2: balance=200 (visible to T1 only)

  │  T2 updates to 300
  │  └── Version 1: balance=100
  │      Version 2: balance=200
  │      Version 3: balance=300

  │  Garbage collection removes version 1 (no active transaction needs it)

Each row has:

  • Creation timestamp (XID or commit timestamp)
  • Expiration/delete timestamp
  • Version chain pointer

How MVCC Implements Isolation

READ COMMITTED: Transaction sees all changes committed before its current statement.

REPEATABLE READ / SNAPSHOT ISOLATION: Transaction sees all changes committed before its start time. A consistent snapshot.

SERIALIZABLE: Full isolation — as if transactions run one after another.

Version Storage

ApproachHowExample
Append-onlyNew version = new row/tuplePostgreSQL (heap + dead tuples)
DeltaStore differences from base versionMySQL InnoDB (undo log)
Copy-on-writeCopy page, modify copyLMDB, Btrfs

MVCC vs Locking

MVCCTwo-Phase Locking (2PL)
Reads block writes?NoYes (read locks)
Writes block reads?NoYes (write locks)
Storage overheadHigher (multiple versions)Lower
Best forRead-heavy workloadsWrite-heavy, contention-prone

Traveler's Notes

MVCC is one of the most elegant solutions in database systems. By keeping multiple versions of data, it allows readers to never block writers and writers to never block readers. This makes modern databases practical under high concurrency. Understanding MVCC and isolation levels helps you choose the right level for your application — balancing correctness against performance.

Built with VitePress | Software Systems Atlas