Multi-Version Concurrency Control
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):
- Under 2PL (pessimistic): writers acquire exclusive locks that block readers, and readers acquire shared locks that block writers.
- Under single-version OCC (optimistic): transactions run without locks, but if a writer modifies and commits a row while a long-running reader is reading it, the reader's validation check fails — the reader is forced to abort and throw away all its work.
Example: The Single-Version Conflict
A bank database with two accounts: A = $100 and B = $200.
- T1 (long analytical query): calculates the bank's total assets by reading A, then B.
- T2 (fast transfer query): deducts $20 from B (B = $180) and commits.
The timeline: T1 reads A ($100). T2 then updates B to $180 and commits, overwriting $200 in place. T1 now tries to read B.
- If using 2PL: T1 holds a read lock on A (blocking T2 from touching it), and when T2 locks B to write $180, T1 is forced to stop and wait in the lock queue until T2 finishes.
- If using single-version OCC: T1 reads A ($100) and B ($180) freely — but at commit, the engine detects that B was overwritten by T2 mid-execution. T1 fails validation and aborts, wasting every CPU cycle the analytical query spent.
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:
- T1 starts at timestamp 10.
- T1 reads Account A — Version 1 = $100.
- T2 updates Account B. Instead of overwriting $200, T2 creates a new version: B Version 1 = $200 (valid for timestamps 0–12), and B Version 2 = $180 (valid from timestamp 12 onward).
- T1 reads Account B. Since T1 started at timestamp 10, it ignores Version 2 and reads B Version 1 = $200.
The Result
- T1 (reader) reads Version 1 of B ($200) — the committed state when it started — and completes its calculation ($100 + $200 = $300) smoothly, without waiting for locks or failing validation.
- T2 (writer) wrote Version 2 of B ($180) instantly, never blocked by T1.
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:
- 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.
- 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:
- Disk and memory bloat: old, dead versions consume disk space and clutter the buffer pool.
- 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:
- OCC's workspace is private. The scratchpad is completely hidden from the rest of the database — no other transaction can see or read those drafts. It exists purely to do local work and run the validation check at commit time.
- MVCC's versions are public. Committed versions are placed into the shared global database — buffer pool or disk — specifically so other concurrent transactions can read those older committed snapshots without locks and without waiting.
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.
| Feature | OCC | MVCC |
|---|---|---|
| What it is | A conflict-validation protocol | A data storage architecture |
| Main goal | Avoid locking overhead during execution | Stop readers from blocking writers (and vice versa) |
| Where changes live during execution | Private, isolated scratchpad | Global version chain with visibility timestamps |
| Can it be in-memory? | Yes | Yes |
| Can it be on disk? | Yes | Yes |
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.
Related reads
- Optimistic Concurrency Control — the scratchpad-in-memory protocol MVCC is so often contrasted (and combined) with
- Two-Phase Locking — the pessimistic protocol most MVCC engines pair with for writer-writer conflicts
- Concurrency Anomalies — the lost-update and dirty-read races versioning eliminates for readers
- SSTables: The Sorted String Table — version chains and append-only storage are cousins of LSM compaction