Skip to content
LESSON 15 / 50Advancedwiredtiger

WiredTiger MVCC & In-Memory Snapshot Isolation

WiredTiger implements multi-version concurrency control (MVCC) in memory using linked update chains, allowing readers to query consistent snapshots without blocking writers.

Interactive Engine Simulator

WiredTiger Storage Engine: Cache, Journal & Checkpoints

Simulate RAM page states, dirty page eviction thresholds, WAL journaling, and 60-second checkpoint disk flushes.

Dirty Page Ratio
50%
Eviction triggers at 80%
Journal WAL LSN
#1048
Flushed every 100ms
Last Checkpoint
18s ago
Default period: 60s
Node State
HEALTHY
WiredTiger Engine
1. RAM Cache (Volatile)4 Pages
p-1users: [u1..u20]
CLEAN
p-2orders: [o101..o150]
DIRTY
p-3products: [p1..p50]
CLEAN
p-4users: [u21..u40]
DIRTY
2. Journal (WAL Ring Buffer)fsync 100ms
Active Journal File:WiredTigerLog.0000000001
Current LSN:#1048
Persistence guarantee:Write Concern j:true
Sequential append-only log enables instant crash recovery by replaying changes between last checkpoint and current LSN.
3. Disk Storage (.wt B-Trees)Checkpointed
Data Tables:collection-*.wt
Index Tables:index-*.wt
Root Pointer:WiredTiger.turtle
Disk files store B-Tree pages with Snappy / zlib compression. Only updated during checkpoint flush or heavy eviction.
ENGINE_LOG:Engine initialized. WiredTiger cache allocated (50% RAM minus 1GB).

1. Formal Definition & Architecture Intent

Multi-Version Concurrency Control (MVCC) in WiredTiger ensures that readers do not block writers, and writers do not block readers. When a document is modified, WiredTiger does not overwrite the existing record in memory. Instead, it prepends a new update structure containing the new data and transaction commit timestamp to an in-memory update chain linked to the B-Tree row.

2. Explain Like I'm 5 (Mental Model)

When an author edits a shared document, instead of erasing sentences and locking the page, they stick a transparent sticky note over the paragraph. Readers who arrived earlier read the original page; new readers read the sticky note.

3. Real-World Architecture Example

Concurrent read queries reading customer balance while an update transaction is in flight.

Sample BSON Documents:
Collection: db.accounts
{
  "_id": "acc_01",
  "balance": 500
}

A reader with snapshot timestamp 102 reads 500, while a reader with timestamp 106 reads 400 concurrently without any mutex locks.

4. MQL Implementation Steps

Step 1: Run query with snapshot read concern in a transaction
const session = db.getMongo().startSession();
session.startTransaction({ readConcern: { level: "snapshot" } });
const acc = session.getDatabase("bank").accounts.findOne({ _id: "acc_01" });
session.commitTransaction();
Guarantees the read views a point-in-time consistent snapshot across all collections.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Readers vs WritersReaders read historical versions from update chains without acquiring write locks.
Update ChainsEach memory row tuple maintains a linked list of update structures ordered by timestamp.

6. Deep Dive: Update Chain Traversal & Reconciliation

When a reader traverses a B-Tree page, it evaluates the update chain on each row against its active transaction read timestamp. If the top version is uncommitted or committed after the reader's snapshot timestamp, WiredTiger walks backward along the pointer chain until it finds the version committed before its snapshot.

// Structure of in-memory row:
// [Row Key: "acc_01"] -> [Update Tx: 108, Balance: 400] -> [Update Tx: 100, Balance: 500]
Production Warning: Deep update chains on hot spot rows consume CPU during backward traversal.

7. Architectural Trade-offs

Advantages
  • •Eliminates read-write lock contention across concurrent clients
  • •Provides true point-in-time Snapshot Isolation for multi-document ACID transactions
  • •Historical versions are garbage collected automatically during in-memory page reconciliation
Disadvantages & Gotchas
  • •Update chains consume working cache RAM until reconciled and evicted
  • •High write contention on a single document creates long update chains that slow reads

8. Technical Interview Questions

Q1: How does WiredTiger handle concurrent updates to the same document under MVCC?

WiredTiger uses optimistic concurrency control for row updates. If two concurrent transactions attempt to update the same document, the second transaction detects a write conflict (WriteConflictException) and automatically retries internally, or aborts if operating within an explicit client transaction.

9. Staff-Level Ready Answer

"WiredTiger implements MVCC using in-memory update chains attached to B-Tree leaf tuples. Readers consult their snapshot timestamp and traverse the chain to view consistent point-in-time data without acquiring locks. When pages are reconciled during checkpoints or eviction, committed updates are collapsed into base leaf blocks."

10. Quick Knowledge Check

Under WiredTiger MVCC, what structure is attached to a B-Tree row in memory to record concurrent modifications?