Why Compaction?
As writes accumulate in RocksDB, several problems emerge without compaction:- Read amplification: More files to check for each read
- Space amplification: Multiple versions of the same key
- Write amplification: L0 files with overlapping key ranges
Compaction solves these problems by merging files, removing obsolete data, and maintaining the LSM tree invariants.
How Compaction Works
Compaction merges SST files from one level to the next:Compaction Process
- L0 → L1
- Li → Li+1
- Data Cleanup
L0 to L1 compaction merges overlapping L0 files with L1:
L0→L1 compaction is the most expensive because L0 files can overlap with multiple L1 files.
Compaction Styles
RocksDB supports multiple compaction strategies:Level Compaction (Default)
Data is organized into levels of increasing size:Level Compaction Characteristics
Level Compaction Characteristics
Pros:
- Lower space amplification (1.1-1.3x)
- Predictable read performance
- Works well for most workloads
- Higher write amplification (10-30x)
- More background I/O
Universal Compaction
Optimizes for write amplification:Universal Compaction Characteristics
Universal Compaction Characteristics
Pros:
- Lower write amplification (2-5x)
- Higher write throughput
- Simpler model (no levels)
- Higher space amplification (1.5-2x)
- Variable read performance
- Large compactions can cause latency spikes
FIFO Compaction
Simple time-based deletion:FIFO Compaction Characteristics
FIFO Compaction Characteristics
Pros:
- Minimal write amplification (1x)
- No compaction overhead
- Simple space management
- No key updates or deletes (append-only)
- High space amplification
- Oldest data deleted when limit reached
Compaction Priority
Control which files are compacted first:Manual Compaction
Trigger compaction programmatically:Manual compaction blocks until complete and can take significant time for large databases. Use
CompactRangeOptions::exclusive_manual_compaction = false to allow parallel automatic compactions.Compaction Configuration
Background Threads
Write Stall Protection
Prevent writes from overwhelming compaction:When limits are exceeded, RocksDB slows or stops writes to allow compaction to catch up. Monitor these triggers to avoid write stalls.
Compaction Size Limits
Compaction Filters
Custom logic to drop keys during compaction:Compaction filters run on every key during compaction. Keep logic simple to avoid slowing compaction.
Monitoring Compaction
Compaction Statistics
Compaction Events
Optimization Strategies
- Write-Heavy
- Read-Heavy
- Space-Constrained
Optimize for write throughput:
Best Practices
- Use level compaction for general-purpose workloads
- Monitor L0 file count - too many indicates compaction falling behind
- Tune thread count based on CPU cores and I/O capacity
- Set appropriate triggers to prevent write stalls
- Use compaction filters for application-specific cleanup
- Profile compaction with statistics and event listeners
- Consider universal compaction for write-heavy workloads
- Manual compaction during off-peak hours for large databases
Next Steps
LSM Tree Design
Understand the LSM structure compaction maintains
Architecture
See how compaction fits in RocksDB architecture
Performance Tuning
Advanced compaction optimization techniques
Write-Ahead Log
Learn about WAL file management and compaction