CSE Roadmap

Core tier · about 2 hours · 5 parts

Part 4 of 5 · 2 min read

Why readers never block writers, and writers never block readers

A reader uses an old version while a writer creates a new one, so the two never compete for the same lock.

Why readers never block writers, and writers never block readers

Think of someone reading a library's archived, bound volume of last year's newspaper. Elsewhere in the same building, today's newspaper is still being printed. The reader never has to wait for the press to finish, since they are not touching the same physical pages at all.

In plain words: under MVCC, a reader is simply given an existing, already-written version of a row, one that a writer is not actively modifying, so no lock conflict between the two ever needs to arise. A writer creating a new version does not need to wait for, or disturb, any reader still consulting an older version, since the two are working with entirely separate copies of the data rather than contending over a single shared value. This is the central reason MVCC-based systems can offer much higher read concurrency than a purely lock-based approach, where a reader's shared lock and a writer's exclusive lock would directly conflict.

A worked example. A long-running report reading an old snapshot of orders does not block, or get blocked by, a concurrent transaction that is busy writing new versions of rows in that same table, since the report never touches those new, in-progress versions at all.

The key lines. No new SQL; this non-blocking behavior between readers and writers is a structural consequence of versioning, not a configurable setting.

Reader-writer non-blocking (MVCC)

Reader-writer non-blocking under MVCC means a reader consulting an older version and a writer creating a newer version never contend for the same lock, since each works with a separate version of the row.

Example: reading last year's archived newspaper while today's edition is still being printed, with neither party waiting on the other, mirrors MVCC's reader-writer non-blocking behavior.

Remember this

MVCC trades extra storage, for keeping multiple versions around, for dramatically better read concurrency, since readers and writers simply stop contending for the same locks. The next page covers exactly which anomalies snapshot isolation, despite all this, still does not prevent.

Quick notes

  • MVCC has a write create a new version of a row rather than overwriting it in place.
  • Snapshot isolation gives a transaction a consistent view of the database as of its own start time.
  • A transaction under snapshot isolation always reads whichever version was current at its own start, ignoring later versions.
  • Readers and writers never block each other under MVCC, since they work with separate row versions.
  • This read concurrency benefit comes at the cost of extra storage for keeping multiple versions around.