← Back

Multi-Version Concurrency Control

11 min read

The Problem: Readers and Writers Interfering with Each Other

In a single-version database — where only one physical copy of each row exists — reading and writing fundamentally clash, regardless of whether you use pessimistic locking (2PL) or optimistic validation (OCC):

Example: The Single-Version Conflict

A bank database with two accounts: A = $100 and B = $200.

The timeline: T1 reads A ($100). T2 then updates B to $180 and commits, overwriting $200 in place. T1 now tries to read B.

In a single-version system: writers block readers (or cause them to abort), and readers block writers.

What If We Stored Multiple Copies of the Same Row?

The escape hatch is a question: what if, instead of overwriting a single record in place, the database retained older physical copies of a row as new versions were created?

Re-run the exact same scenario with multiple versions:

The Result

By keeping historical copies: writers do not block readers, and readers do not block writers.

Formalizing the Concept: MVCC

This paradigm is Multi-Version Concurrency Control (MVCC). When a transaction updates a logical row, the database does not modify the existing data in place — it creates a new physical version of that row, tagged with timestamps (begin_timestamp and end_timestamp). Readers simply walk the version chain and read the snapshot that was active when their transaction began.

The New Problem: Writer-Writer Conflicts

MVCC isolates readers from writers beautifully — but a new question arises: what happens when two concurrent transactions try to write the same row at the same time?

Continue the example: two transactions try to update B Version 1 ($200) simultaneously — T2 wants to deduct $20 (B = $180), and T3 wants to add $50 (B = $250). If both read Version 1 and blindly append their new versions without coordination, T2 creates Version 2 ($180) and T3 creates Version 3 ($250) — and whichever version ends up at the head of the chain silently erases the other's update. The classic lost update anomaly, back again.

Why MVCC Needs a Concurrency Control Protocol

MVCC alone only manages visibility for readers — it does not coordinate concurrent writers. So every real engine pairs MVCC with a concurrency control protocol:

  1. MVCC + 2PL (pessimistic — e.g., PostgreSQL, MySQL/InnoDB): writers must acquire an exclusive lock on the logical row before appending a new version. If T2 holds the write lock on B, T3 is blocked until T2 commits. Readers still require zero locks.
  2. MVCC + OCC (optimistic — e.g., Microsoft Hekaton, Amazon DSQL): writers construct new row versions in private workspaces without locks. At commit, the engine enforces a first-writer-wins rule: if T2 commits first, T3 detects that a newer version of B was created, fails validation, and aborts.

How to Store Multiple Copies: Version Storage Strategies

So how does the database physically store these multiple versions? Updating B from $200 (Version 1) to $180 (Version 2) looks different under each of the three primary strategies:

Append-Only Storage (PostgreSQL)

All physical versions — old and new — live directly in the main table space (the heap). Updating B appends a full new tuple ($180) right next to the old tuple ($200) in the table page, linked by a pointer from the old version to the new one.

Trade-off: fast writes, but the table bloats with old tuples and needs heavy background garbage collection (PostgreSQL's VACUUM) to clean up expired versions.

Time-Travel Storage

The main table always holds the latest version of the row; when a row is updated, the older full tuple is moved to a separate time-travel storage area. Updating B puts Version 2 ($180) in the main table and relocates Version 1 ($200) to the time-travel space.

Trade-off: keeps the main table lean for primary queries, but pays an overhead physically copying full tuples across storage boundaries.

Delta Storage / Undo Logs (MySQL/InnoDB, Oracle)

The main table holds the latest version, and instead of storing full copies of old tuples, the engine writes compact column-level diffs (deltas) to a separate undo log segment. Updating B stores Version 2 ($180) in the main table and a tiny delta entry in the undo log: -$20. An older reader needing Version 1 reads $180 from the main table and applies the delta backwards ($180 + $20 = $200) to reconstruct the historical row in memory.

Trade-off: extremely fast updates and minimal bloat, but older readers pay a small CPU cost reconstructing historical rows from delta chains.

Garbage Collection

Why It's Needed

MVCC's version chains solve interference — but they create a storage problem of their own. Using our bank example: Account A starts at $100, Transaction 1 transfers $20 out, creating a new version (A = $80). Because MVCC never overwrites in place, the database now stores both versions — $100 and $80.

Multiply that across thousands of transactions updating A and B throughout the day, and the database accumulates thousands of historical row versions:

  1. Disk and memory bloat: old, dead versions consume disk space and clutter the buffer pool.
  2. Slower queries: every read has to traverse long version chains, skipping past dead rows just to find the version it needs.

Once all transactions that needed the old $100 version complete and commit, that copy becomes obsolete — no active or future transaction can ever see it again. Garbage Collection (GC) is the background process that identifies these dead versions and reclaims their space. To know when it's safe, the database tracks a watermark — the timestamp of the oldest currently active transaction. Any version that expired before the watermark is invisible to everyone and can be safely purged.

The Three Techniques

Tuple-Level GC: Background Vacuuming (PostgreSQL Style)

A dedicated background thread — the vacuum — periodically scans table pages, compares each row version's expiration time against the active watermark, and reclaims the dead ones. In the bank example: once all queries that started before Transaction 1 finish, the vacuum scans the page containing Account A, sees the $100 version is older than the watermark, unlinks it, and marks the space free.

Optimization: to avoid re-reading untouched pages from disk, the vacuum consults a visibility map — a simple bitmap marking which pages have had no updates since the last cleanup — and skips the clean ones.

Tuple-Level GC: Cooperative Cleaning (On-the-Fly)

Rather than relying solely on a background thread, active worker threads clean up dead versions as they traverse version chains during normal user queries. A query checking Account A's balance walks the chain to find the latest value ($80), stumbles across the dead $100 version, sees it's older than the watermark, and unlinks and reclaims it right there — cleanup performed as a free side effect of fulfilling the read.

The "dusty corner" limitation: if Account B is rarely queried, worker threads never visit its page, so its dead versions linger in "dusty corners" until a secondary background sweep eventually runs.

Transaction-Level GC: Undo Log Truncation (MySQL/InnoDB Style)

In delta storage, the main table always holds the latest version (A = $80) while older changes live as small undo diffs (e.g., +$20) in a central undo log segment. When Transaction 1 updated Account A, it wrote a +$20 undo delta into an undo log block. Once the watermark moves past Transaction 1's commit time, the database doesn't scan table pages or touch main-table rows at all — it simply truncates and drops the entire undo log block in one bulk operation.

Advantage: reclamation is instantaneous, requires zero table scanning, and never pollutes the buffer pool.

So... Is MVCC Just OCC with a Disk?

Step back and squint at what we built: a transaction's changes stay invisible to others until commit, and conflicts get resolved at the end instead of blocked upfront. That looks a lot like OCC — so is MVCC just OCC with the scratchpad moved from RAM to disk?

No — and the difference isn't memory vs. disk at all. MVCC can live entirely in memory (Microsoft Hekaton stores all its versions in RAM), and single-version OCC can write to disk. The real distinction is visibility and purpose:

The ultimate proof is PostgreSQL and MySQL/InnoDB: if MVCC were just OCC on disk, every MVCC database would validate optimistically at commit. They don't — both use pessimistic 2PL row locks for writer-writer conflicts, yet remain fully MVCC, because readers still traverse old versions without blocking anyone. MVCC is a storage architecture (keep every version, timestamp it, let each reader pick its snapshot); 2PL and OCC are protocols layered on top to coordinate the writers.

FeatureOCCMVCC
What it isA conflict-validation protocolA data storage architecture
Main goalAvoid locking overhead during executionStop readers from blocking writers (and vice versa)
Where changes live during executionPrivate, isolated scratchpadGlobal version chain with visibility timestamps
Can it be in-memory?YesYes
Can it be on disk?YesYes

One last thread to pull: every version chain we described lives in ordinary pages and segments — so what guarantees all those versions survive a sudden server crash? That's the job of the Write-Ahead Log.