Skip to content
LESSON 12 / 50Advancedwiredtiger

WiredTiger In-Memory Cache (Clean vs Dirty Pages)

The WiredTiger cache buffers frequently accessed B-Tree pages in memory, managing transitions between Clean and Dirty page states.

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

The WiredTiger Cache is an in-memory buffer pool that holds working set pages. A page is "Clean" if its in-memory contents match its persistent representation on disk. A page becomes "Dirty" when an insert, update, or delete modifies it in memory. WiredTiger background eviction workers continuously write dirty pages to disk.

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

Think of the cache like a chef's kitchen prep table. Fresh ingredients from the pantry are "Clean". Once the chef chops and cooks them, they become "Dirty" (modified). Before closing the kitchen, the assistant must pack them into containers and store them back in the pantry (disk).

3. Real-World Architecture Example

Monitoring WiredTiger cache usage on an active production replica node.

Sample BSON Documents:
Collection: db.serverStatus.wiredTiger.cache
{
  "bytes currently in the cache": 17179869184,
  "maximum bytes configured": 34359738368,
  "tracked dirty bytes in the cache": 1073741824
}

WiredTiger actively tracks the ratio of dirty bytes to ensure dirty data does not exceed 20% of total cache capacity.

4. MQL Implementation Steps

Step 1: Query real-time cache metrics in mongosh
const c = db.serverStatus().wiredTiger.cache;
print("Cache total: " + (c["bytes currently in the cache"] / 1024 / 1024) + " MB");
print("Dirty bytes: " + (c["tracked dirty bytes in the cache"] / 1024 / 1024) + " MB");
Monitors memory pressure and dirty page accumulation.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Default AllocationDefaults to 50% of (RAM - 1GB), or 256MB, whichever is larger.
Eviction TriggerBackground eviction begins when cache reaches 80% full, or dirty bytes reach 5%.
Client Stall TriggerIf dirty bytes reach 20% or cache reaches 95%, client threads are forced to perform storage eviction.

6. Deep Dive: Client Thread Stalls During High Dirty Cache Pressure

If a massive write burst generates dirty pages faster than background eviction threads can flush them to disk, WiredTiger reaches its critical threshold (20% dirty). At this point, application client threads submitting writes are conscripted into performing eviction. Application write latency spikes from 1ms to hundreds of milliseconds.

// Mongod configuration parameter in mongod.conf:
storage:
  wiredTiger:
    engineConfig:
      cacheSizeGB: 16
Production Warning: Never allocate 100% of host RAM to WiredTiger; OS file system buffers and client connections need RAM.

7. Architectural Trade-offs

Advantages
  • •Sub-millisecond read and write latency for data resident in the working cache
  • •Separation of Clean and Dirty pages minimizes unnecessary disk I/O flushes
  • •Lock-free hazard pointers allow concurrent reads and updates on cache pages
Disadvantages & Gotchas
  • •Write bursts can overwhelm eviction workers, causing sudden client latency spikes
  • •High cache pressure forces premature eviction of hot read pages

8. Technical Interview Questions

Q1: What happens to client request latency when WiredTiger dirty cache exceeds 20%?

When dirty cache exceeds 20%, WiredTiger throttles incoming writes and forces the client worker threads themselves to synchronously write dirty pages to disk before accepting new operations, causing dramatic latency spikes.

9. Staff-Level Ready Answer

"WiredTiger allocates 50% of (RAM - 1GB) to its cache, categorizing pages as Clean or Dirty. Background eviction threads keep dirty pages below 5% and overall utilization below 80%. If dirty cache breaches 20%, client write threads are throttled and forced into synchronous disk reconciliation."

10. Quick Knowledge Check

By default, what formula does MongoDB use to determine the WiredTiger cache size on a standalone server?