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.
WiredTiger Storage Engine: Cache, Journal & Checkpoints
Simulate RAM page states, dirty page eviction thresholds, WAL journaling, and 60-second checkpoint disk flushes.
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.
{
"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
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");5. Critical Engineering Rules
| Rule / Constraint | Underlying Reason & Impact |
|---|---|
| Default Allocation | Defaults to 50% of (RAM - 1GB), or 256MB, whichever is larger. |
| Eviction Trigger | Background eviction begins when cache reaches 80% full, or dirty bytes reach 5%. |
| Client Stall Trigger | If 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: 167. Architectural Trade-offs
- •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
- •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
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?