This is a great question about LSM (Log-Structured Merge) trees, which are fundamental to many modern databases like LevelDB, RocksDB, Cassandra, etc. Let me provide a comprehensive explanation of compaction strategies.
LSM Tree Basics First
LSM trees maintain sorted key-value data in multiple levels to balance read and write performance:
Recent writes go into memory (memtable)
When memtable fills, it's flushed to disk as an SSTable (Sorted String Table)
Multiple SSTables are organized in levels (Level 0, 1, 2, etc.)
Compaction merges SSTables to maintain the LSM invariant
Compaction Problem
As the tree grows:
We get more levels with more SSTables
This increases read amplification (need to check more files)
But we also need to manage write amplification (rewriting data during compaction)
Leveled Compaction
Strategy:
Each level has a size limit (e.g., level k has limit = 10^k MB)
Each level has multiple SSTables
When a level exceeds its size limit, pick one file and compact it with overlapping files from level L+1
This is incremental - small, predictable writes
Read Amplification:
Because files within a level don't overlap (sorted ranges)
Only need to check ~1 file per level for a key
Max read amplification ≈ number of levels ≈ log(data_size)
Write Amplification:
Every piece of data gets rewritten multiple times as it moves through levels
If we have L levels, a write is amplified by ~L factor
High write amplification due to continuous, incremental compaction
Characteristics:
Better for read-heavy workloads
More SSD wear due to rewrites
More CPU usage for compaction
More predictable latency
Maintains stable write throughput
Tiered Compaction
Strategy:
Each level stores multiple overlapping SSTables
When a level reaches a threshold (e.g., T SSTables), all files in that level compact together with level L+1
Compaction happens in bulk rather than incrementally
Read Amplification:
Files within a level can overlap, so checking all SSTables in a level is necessary
Max read amplification ≈ T (SSTables per tier) × number of levels
If T=2, read amp = 2*log(data_size)
Write Amplification:
Lower than leveled compaction since data moves through fewer rewrites per level transition
Bigger, less frequent compactions reduce overall amplification
Characteristics:
Optimized for write-heavy workloads
Reduces SSD wear and CPU overhead during compaction
Trades space efficiency for write performance due to overlapping files
Write latency becomes bursty from batch compactions
Fundamental Trade-off
The core difference between these approaches comes down to ordering constraints. Leveled compaction enforces strict ordering within levels to minimize read costs, but this requires frequent rewrites. Tiered compaction relaxes this ordering to allow overlaps, making writes cheaper at the expense of more complex reads.
Read Performance Comparison
Leveled compaction keeps files within each level sorted by key range with no overlaps, so finding a key requires checking at most one file per level plus all files in L0. This typically means 11-20 disk seeks depending on the number of levels. Tiered compaction allows files to overlap within levels, forcing the system to check multiple files per level—if each level contains T SSTables, the cost scales with T times the number of levels, potentially reaching 12-14 accesses or higher when T is large.
Write Amplification
Leveled compaction incurs exponential write amplification as data cascades through levels. A 1GB SSTable at L0 compacts to L1 by reading 1GB plus ~10GB from L1 and writing 11GB total—an 11x amplification. As that data moves through subsequent levels, each compaction multiplies the cost, resulting in roughly 10^(levels-1) total amplification across 8 levels. Tiered compaction shows different behavior: compacting 2GB from L0 with 20GB from L1 produces 22GB written (11x), then compacting that 22GB with 200GB at L2 writes 222GB (10x on top), creating a more moderate but still significant cumulative cost.
Different systems handle this tradeoff differently. RocksDB's universal compaction offers tiered-like behavior with tunable parameters, while Cassandra lets operators choose their strategy. Some databases blend approaches—using tiered compaction at lower levels to optimize writes, then switching to leveled at higher levels for better read performance.
LSM Tree Compaction Strategies
Overview
Compaction merges overlapping SSTables (Sorted String Tables) across levels, but the strategy for when and how to compact creates fundamental trade-offs in LSM performance.
# Leveled compaction trigger
if level[i].total_size > level_size_limit(i):
# Pick ONE file from level i
# Compact with overlapping files in level i+1
file = pick_smallest_or_hot_file(level[i])
overlapping = get_overlapping_files(level[i+1], file.key_range)
new_sstable = merge_sorted(file + overlapping)
level[i+1].add(new_sstable)
# Incremental: one file at a time
# Tiered (size-tiered) compaction trigger
tier_size_ratio = 2 # Trigger when We have N SSTables
if level[i].num_sstables >= tier_size_ratio:
# Compact ALL of level i with level i+1
all_files = level[i].all_sstables + level[i+1].all_sstables
new_sstable = merge_sorted(all_files)
level[i+1].set([new_sstable])
# Batch compaction: all files move together
With tiered, each level contributes ~constant amplification factor (the tier ratio).
With leveled, you rewrite data many more times as it percolates down.
Direct Comparison: Read vs. Write
Read Amplification
code
LEVELED:
Lookup key "x":
┌──────────────────────────────┐
│ L0: scan all files (10 files) │ ← pay this cost once
│ L1: binary search (1 file) │
│ L2: binary search (1 file) │
│ L3: binary search (1 file) │
│ L4: binary search (1 file) │
└──────────────────────────────┘
Total: ~10 + 1 + 1 + 1 + 1 = 14 file checks
TIERED (with tier_ratio=2):
Lookup key "x":
┌──────────────────────────────┐
│ L0: scan all files (2 files) │ ← all may have "x"
│ L1: scan all files (2 files) │ ← all may have "x"
│ L2: scan all files (2 files) │ ← all may have "x"
│ L3: scan all files (2 files) │ ← all may have "x"
│ L4: scan 1 file │
└──────────────────────────────┘
Total: 2 + 2 + 2 + 2 + 1 = 9 files... BUT if tier_ratio=4:
TIERED (tier_ratio=4):
Lookup: 4 + 4 + 4 + 4 + 1 = 17 file checks ← WORSE than leveled
Write Amplification
code
LEVELED:
New 1MB write enters L0
├─ Compact L0→L1: read 1MB, read ~10MB, write 11MB = 11× amp
├─ Compact L1→L2: read 11MB, read ~100MB, write 111MB = 10× additional
├─ Compact L2→L3: read 111MB, read ~1000MB, write 1111MB = 10× additional
└─ ... (happens repeatedly to same data)
Total: 11 × 10 × 10 = 1100× amplification ✗ SEVERE
TIERED (tier_ratio=2):
New 1MB enters, waits at L0
├─ When 2 files at L0 (2MB total): compact with L1
│ read 2MB + read 2MB + write 4MB = 2× amp
├─ Now 4MB at L1, wait for 2 files at L1 (8MB): compact with L2
│ read 8MB + read 8MB + write 16MB = 2× amp (on top of previous)
├─ Each level: 2× more data
└─ Total: 2 × 2 × 2 × 2 = 16× amplification ✓ MUCH BETTER
Why the Trade-off Exists
The Fundamental Constraint
code
Within a level, you can either:
A) ENFORCE ORDERING (Leveled)
✓ Find keys quickly (binary search)
✓ No overlays to check
✗ Must maintain invariant → frequent rewrites
B) ALLOW OVERLAPS (Tiered)
✓ Defer compaction → fewer rewrites
✓ Better write throughput
✗ Must scan all files → slower reads
Information Theory View
code
To achieve low read amplification, you need:
→ Ordered, non-overlapping files (use indexing)
→ But maintaining this order requires continuous compaction
To achieve low write amplification, you need:
→ Defer work (batch writes)
→ But batching means overlapping data
You cannot optimize both simultaneously. It's an inherent trade-off.
Practical Implications
Choose Leveled for:
Read-heavy workloads (e.g., serving user requests)
RocksDB "Leveled":
L0 → L1: leveled (ordered, non-overlapping)
L1 → Ln: leveled
Result: standard low read amp
RocksDB "Universal" (Tiered):
All levels: tiered with overlap allowed
Result: fewer but larger compactions
RocksDB "FIFO":
Just drop oldest data when full
Result: lowest write amp, no reads (TTL cache)
Cassandra (SizeTiered):
Similar to tiered, but can switch to leveled
Hybrid approaches:
- Use tiered at L0-L1 (fast writes)
- Switch to leveled at L2+ (consistent reads)
Summary Table
Aspect
Leveled
Tiered
Key Ordering
Strict per level
Overlapping
File Checks per Read
log(N) × ~10
log(N) × T
Read Latency
Better
Worse (scales with T)
Write Amplification
10-100×
4-10×
Compaction Frequency
Continuous
Bursty
CPU/SSD Load
High steady
High spikes
Best For
Reads
Writes
Examples
LevelDB, default RocksDB
Cassandra, HBase
The choice depends on your workload: optimize for the critical path given your read/write ratio.