Overview
RocksDB supports multiple compression algorithms to reduce storage space and I/O. Compression is applied at the block level and can be configured per-level for optimal trade-offs between space, CPU, and performance.Compression Types
Fromcompression_type.h:18-166, RocksDB supports these algorithms:
Algorithm Comparison
ZSTD (recommended): Best balance of compression ratio and speed for most workloads.
Basic Configuration
Single Compression Type
Per-Level Compression
Fromadvanced_options.h:505-534:
Compression Options
Fromcompression_type.h:169-317, configure algorithm-specific parameters:
Compression Level
Fromcompression_type.h:175-195:
Parallel Compression
Fromcompression_type.h:229-244:
- Only works with BlockBasedTable
- Disabled if using partition filters without decoupling
Dictionary Compression
Fromcompression_type.h:200-220, use dictionaries for better compression on small values:
Dictionary Training
Two approaches: 1. ZSTD Trainer (better compression, more CPU):Dictionary Buffering
Fromcompression_type.h:255-272:
Dictionary building requires buffering SST data in memory. Limit with
max_dict_buffer_bytes to prevent excessive memory usage.Compression Ratio Threshold
Fromcompression_type.h:288-299:
ZSTD-Specific Options
Checksum
Fromcompression_type.h:301-307:
ZSTD Level Guidelines
Bottommost Level Compression
Apply stronger compression to the bottommost level:Compression Statistics
Fromstatistics.h:251-283, monitor compression effectiveness:
Advanced Patterns
Tiered Compression Strategy
Sample-Based Compression Testing
Fromadvanced_options.h:943-947:
Performance Tuning
Write-Heavy Workloads
Read-Heavy Workloads
Balanced Workload
Troubleshooting
Poor Compression Ratio
-
Check BYTES_COMPRESSION_REJECTED: Too many blocks failing threshold
-
Enable dictionaries: Helps with small, similar values
-
Increase compression level: More CPU for better ratio
High Write CPU
-
Use faster algorithms: Switch from ZSTD to LZ4
-
Disable parallel compression: May add overhead
-
Lower compression level:
Slow Reads
-
Profile decompression time: Check
DECOMPRESSION_TIMES_NANOShistogram -
Consider lighter compression: Trade space for speed
- Increase block cache: Cache uncompressed blocks
Best Practices
Start with ZSTD level 3: Good default for most workloads. Tune based on profiling.
- Use per-level compression: Match compression to data temperature
- Monitor statistics: Track compression ratio and CPU time
- Test with your data: Compression effectiveness varies by data characteristics
- Enable dictionaries for small values: Especially effective for similar records
- Profile before optimizing: Measure CPU overhead vs. space savings