Skip to content
LESSON 17 / 50Intermediatewiredtiger

Block Compression: Snappy, zlib, zstd & Prefix Compression

WiredTiger achieves 60-80% storage savings by compressing collection data blocks with Snappy/zstd and index pages with Prefix Compression.

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

WiredTiger compresses collection data pages before writing them to disk blocks using block compression algorithms (Snappy by default, or zlib/zstd). In addition, index B-Tree pages utilize Prefix Compression, which eliminates redundant common key prefixes across consecutive index entries.

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

Block compression is like putting your winter clothes into a vacuum-seal bag before putting them in the attic: they take up 70% less space. Prefix compression in an index is like writing "smith" once and writing ", john", ", jane", ", mary" instead of re-writing "smith" every time.

3. Real-World Architecture Example

Compressing a 100GB access log collection using zstandard (zstd) instead of default Snappy.

Sample BSON Documents:
Collection: db.system.profile
{
  "uncompressedSize": "100 GB",
  "snappyCompressedSize": "32 GB",
  "zstdCompressedSize": "18 GB"
}

Switching to zstd reduces disk footprint from 32GB to 18GB, saving disk costs at the expense of slightly higher CPU usage.

4. MQL Implementation Steps

Step 1: Create a collection with high-ratio zstd compression
db.createCollection("heavy_logs", {
  storageEngine: {
    wiredTiger: {
      configString: "block_compressor=zstd"
    }
  }
});
zstd provides superior compression ratios compared to snappy.
Step 2: Inspect compression ratio on existing collection
const s = db.heavy_logs.stats();
print("Compression Ratio: " + (s.size / s.storageSize).toFixed(2) + "x");
Shows uncompressed memory size divided by physical disk space.

5. Critical Engineering Rules

Rule / ConstraintUnderlying Reason & Impact
Snappy (Default)Balanced performance: low CPU overhead with moderate compression (~60% space savings).
zstd (Recommended for Analytics)High compression ratio (up to 80% savings) with competitive decompression speeds.
Prefix CompressionEnabled on all B-Tree indexes by default; strips duplicate key prefixes across adjacent entries.

6. Deep Dive: Index Prefix Compression Mechanics

Consider an index on { lastName: 1, firstName: 1 } where 10,000 entries share the last name "Williams". Instead of repeating "Williams" 10,000 times in B-Tree leaf blocks, WiredTiger stores the prefix length and only records the differentiating characters. This cuts index size by 50% or more, allowing vastly more index keys to fit into RAM.

// Verify prefix compression is active on indexes:
db.users.stats().indexSizes;
Production Warning: Decompression happens dynamically when reading pages into RAM; CPU usage increases on read-heavy workloads with zlib.

7. Architectural Trade-offs

Advantages
  • •Reduces disk storage costs by 60% to 80% across production clusters
  • •Reduces physical disk I/O bottlenecks: reading smaller compressed blocks is faster than raw uncompressed blocks
  • •Index prefix compression drastically expands the number of index keys fitting into RAM cache
Disadvantages & Gotchas
  • •Higher CPU utilization for compression during writes and decompression during reads
  • •Data pages must be decompressed in memory, meaning RAM cache capacity is unaffected by disk compression

8. Technical Interview Questions

Q1: Does WiredTiger compression reduce the RAM required to hold the active working set in memory?

No. WiredTiger compresses data blocks only when writing to disk. When pages are read into the WiredTiger in-memory cache, they are decompressed into raw BSON tuples. Therefore, working set RAM requirements are determined by uncompressed document and key sizes.

9. Staff-Level Ready Answer

"WiredTiger applies block compression (Snappy, zlib, or zstd) to collection data blocks on disk and prefix compression to index pages. Prefix compression eliminates duplicate key prefixes in index B-Trees, while zstd provides optimal disk density for cold or analytical collections with modest CPU trade-offs."

10. Quick Knowledge Check

Which compression algorithm is the default block compressor for collections in MongoDB WiredTiger?