Learn how to configure RocksDB for optimal performance
RocksDB provides extensive configuration options to tune performance for different workloads. This guide covers the most important configuration settings.
Increase background threads for better performance on multi-core systems:
// Easiest way: let RocksDB decide based on CPU coresoptions.IncreaseParallelism();// Or manually configureoptions.max_background_jobs = 8; // Total background threads
IncreaseParallelism() sets max_background_jobs to the number of CPU cores and is recommended for most workloads.
// Level-style compaction (default, recommended for most workloads)options.OptimizeLevelStyleCompaction();// Universal-style compaction (optimized for read-heavy workloads)options.OptimizeUniversalStyleCompaction();
The write buffer (memtable) holds recent writes in memory:
// Size of each memtable (default: 64MB)options.write_buffer_size = 64 << 20; // 64MB// Maximum number of memtables (default: 2)options.max_write_buffer_number = 3;// Total memory across all column familiesoptions.db_write_buffer_size = 512 << 20; // 512MB
Memory usage calculation:
Total = write_buffer_size × max_write_buffer_number × num_column_families
Larger write buffers improve write performance but increase memory usage and recovery time after crashes.
using ROCKSDB_NAMESPACE::WriteOptions;WriteOptions write_options;// Synchronous write (durable but slower)write_options.sync = true;db->Put(write_options, "key", "value");// Asynchronous write (faster but less durable)write_options.sync = false; // defaultdb->Put(write_options, "key", "value");
With sync = false, recent writes may be lost in case of a system crash. Use sync = true for critical data or implement periodic manual syncing with DB::SyncWAL().
using ROCKSDB_NAMESPACE::ReadOptions;ReadOptions read_options;// Skip block cache for this read (useful for large scans)read_options.fill_cache = false;// Verify checksumsread_options.verify_checksums = true;
// Keep all files open for faster access (-1 = unlimited)options.max_open_files = -1;// Number of threads for opening filesoptions.max_file_opening_threads = 16;
// Trigger level-0 compaction when this many files existoptions.level0_file_num_compaction_trigger = 4;// Maximum bytes for level-1options.max_bytes_for_level_base = 256 << 20; // 256MB// Multiplier for each leveloptions.max_bytes_for_level_multiplier = 10;