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.
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
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.
{
"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
db.createCollection("heavy_logs", {
storageEngine: {
wiredTiger: {
configString: "block_compressor=zstd"
}
}
});const s = db.heavy_logs.stats();
print("Compression Ratio: " + (s.size / s.storageSize).toFixed(2) + "x");5. Critical Engineering Rules
| Rule / Constraint | Underlying 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 Compression | Enabled 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;
7. Architectural Trade-offs
- •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
- •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
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?