Aller au contenu principal

Readers Don't Wait for Writers: MVCC and the Snapshot Read

· 10 minutes de lecture
Clustron Team
Ingénierie des systèmes distribués

A store that allows one client to read a key while another writes it has to decide what the reader sees, and the oldest answer is a lock. The reader takes a shared lock and the writer takes an exclusive one; they are mutually exclusive, so the two operations take turns. It is correct, and it is slow in a specific, corrosive way: a long-running read holds its lock and blocks every write to that key, and a long-running write blocks every read. On any key that is even mildly contended, readers and writers spend their time queued behind one another, and the system's throughput on that key collapses to one operation at a time.

Multi-version concurrency control — MVCC — removes the conflict by removing the thing they were fighting over. The insight is that a read and a write only conflict if they are forced to share a single copy of the value. So don't share it: a write never overwrites in place. It appends a new version of the key, stamped with its position in the global commit order, and leaves every older version sitting exactly where it was. The key is no longer a single cell that must be locked; it is a chain of immutable versions, newest at the head.

A reader, then, never needs a lock. It picks a point in the commit order — a snapshot — and reads, for each key, the newest version that was committed at or before that point, ignoring everything committed after. The writer appending version five is invisible to a reader whose snapshot is pinned at version three, and the reader poring over version three is no obstacle to that writer, because version three is immutable and the writer is not touching it. Readers don't wait for writers and writers don't wait for readers; they are simply looking at different versions, and nobody is in anybody's way.

Every write makes a new version​

The mechanism is append-only at the key level. Each write produces a Version record carrying the value, the commit sequence number that orders it against all other writes, and a link to the version it superseded. The key's head pointer swings to the new version; the old one is still fully intact and reachable through the chain. A delete is not an erasure either — it is a version like any other, marked as a tombstone, so that a snapshot taken before the delete still sees the value and a snapshot taken after sees that the key is gone. This is the same tombstone idea that distributed deletion already needs, reused here to make deletion just another point on the timeline.

// A write never overwrites; it appends a new version stamped with a commit number.
void Put(Key k, Value v, Txn txn) {
var version = new Version(v, commitSeq: txn.CommitSeq, prev: Head(k));
SetHead(k, version); // readers already running never see it
}

// A read sees the newest version at or before its own snapshot — and no newer.
Value ReadAt(Key k, long snapshot) {
for (var ver = Head(k); ver != null; ver = ver.Prev)
if (ver.CommitSeq <= snapshot) // skip versions committed after the snapshot
return ver.IsTombstone ? null : ver.Value;
return null; // key did not exist as of the snapshot
}
v1 · commit 1reclaimedv2commit 2v3commit 3v4 (head)commit 4writer appendsnever overwritesReader A · snapshot @ 2sees v2 — ignores v3, v4Reader B · snapshot @ 3sees v3GC watermarkno live snapshotneeds v1readers and writers never block — they touch different versions of the same key

The snapshot: a consistent point in time​

A snapshot is just a number: the commit sequence as of the instant the read (or read transaction) began. Reading at that snapshot gives two guarantees that a lock gives only clumsily. First, consistency across keys — a read transaction that touches a hundred keys sees all of them as they were at one single point, never a torn mix of some-before and some-after a concurrent write, because every key is filtered through the same snapshot number. Second, stability over time — a long analytical scan that runs for a minute sees the database frozen at its start, no matter how many writes land while it runs, so it never has to restart because the ground shifted under it. This is how a store serves a consistent multi-key transaction read without locking the keys, and how a consistent backup or point-in-time snapshot is taken from a live system without stopping writes.

It is worth being clear about what MVCC does and does not settle. Snapshot reads make readers lock-free, but two concurrent writers to the same key still have to be ordered — the commit sequence decides who is v4 and who is v5 — and detecting that they conflicted is the job of optimistic compare-and-swap, which checks that the version a writer read is still the head before it commits. MVCC and CAS are complementary: versioning keeps reads out of the way, and the version check keeps two writers from silently clobbering each other.

Garbage: when an old version can go​

The bill for never overwriting is that old versions accumulate, and left unchecked the version chain for a hot key would grow forever. So MVCC needs a reclaimer, and the rule for when a version is safe to drop is precise: a version can be deleted once no live snapshot could ever read it again — that is, once there exists a newer version that is still at or below the oldest snapshot any in-progress reader is holding. That oldest live snapshot is the garbage-collection watermark. Everything superseded and entirely below the watermark is unreachable and can be reclaimed; everything at or above it must stay. The practical hazard this creates is a single very long-running reader: it pins the watermark back at its own ancient snapshot and prevents any version newer than that from being collected, so the chains bloat behind it. Real systems bound this by capping read-transaction age and forcing a too-old snapshot to abort — the same way a scan over a moving keyspace cannot hold a view open indefinitely. Reclaiming superseded versions is, mechanically, the same work LSM compaction already does when it keeps only the newest value per key and drops the rest.

How it composes​

Because every version is stamped with its commit order, the version chain and the operation log are two views of the same truth: the log is the ordered list of writes, and the versions are what you get when you index that list by key. A snapshot read is then just "replay the log up to sequence N and stop," which is also exactly what makes reading your own writes trivial — a client that just wrote at commit N reads at a snapshot of at least N and is guaranteed to see it.

Wrap-up​

Locking readers against writers makes a contended key move one operation at a time, because they are forced to share a single mutable copy. MVCC stops sharing it: a write appends a new immutable version stamped with its commit order, a read picks a snapshot and walks the chain to the newest version at or before it, and neither ever blocks the other. The snapshot buys consistency across keys and stability over a long read for free, the version check in CAS handles writer-writer conflicts, and a garbage-collection watermark — the oldest snapshot any live reader still holds — reclaims the versions nobody can reach. Readers don't wait for writers, and the only price is remembering to take out the garbage.