ColumnFamilyOptions
Every column family — including the default one — has its own ColumnFamilyOptions. Compression, memtable size, compaction style, the merge operator, and the comparator are all CF-scoped.
DbOptions inherits from the same base as ColumnFamilyOptions, so a lot of the same setters are available on both. The CF-scoped ones override the DB-scoped defaults for that family.
Upstream reference: include/rocksdb/options.h, Tuning Guide.
Setting up a column family
var cfOpts = new ColumnFamilyOptions()
.SetCompression(Compression.Zstd)
.SetWriteBufferSize(64UL * 1024 * 1024)
.SetMaxWriteBufferNumber(4)
.SetTargetFileSizeBase(64UL * 1024 * 1024);
var cfs = new ColumnFamilies(cfOpts); // default CF
cfs.Add("audit", new ColumnFamilyOptions());
using var db = RocksDb.Open(
new DbOptions().SetCreateIfMissing(true).SetCreateMissingColumnFamilies(true),
path,
cfs);
Memtable & flush
| Setter | What it controls |
|---|---|
SetWriteBufferSize(ulong) |
Memtable size before flush. Larger = fewer flushes, more memory. |
SetMaxWriteBufferNumber(int) |
How many memtables can exist at once (filling + flushing). |
SetMinWriteBufferNumberToMerge(int) |
How many to merge into one SST on flush. |
SetMemtablePrefixBloomSizeRatio(double) |
Memtable Bloom filter ratio. Speeds up point lookups. |
new ColumnFamilyOptions()
.SetWriteBufferSize(128UL * 1024 * 1024)
.SetMaxWriteBufferNumber(4)
.SetMinWriteBufferNumberToMerge(2);
Upstream reference: Memtable.
Compaction
| Setter | What it controls |
|---|---|
SetCompactionStyle(Compaction) |
Level (default), Universal, Fifo. |
SetLevelCompactionDynamicLevelBytes(bool) |
Recommended on for level-style. |
SetTargetFileSizeBase(ulong) |
SST file size at L1. |
SetMaxBytesForLevelBase(ulong) |
Total size budget at L1. |
SetNumLevels(int) |
Number of levels. |
SetDisableAutoCompactions(int) |
Disable background compactions (manual only). |
Upstream reference: Leveled Compaction, Universal Compaction.
new ColumnFamilyOptions()
.SetCompactionStyle(Compaction.Level)
.SetLevelCompactionDynamicLevelBytes(true)
.SetTargetFileSizeBase(64UL * 1024 * 1024);
Compression
new ColumnFamilyOptions()
.SetCompression(Compression.Zstd);
// Per-level compression (e.g. LZ4 on hot levels, ZSTD on archival)
var perLevel = new[]
{
Compression.No, // L0
Compression.Lz4, // L1
Compression.Lz4, // L2
Compression.Zstd, // L3
Compression.Zstd, // L4
Compression.Zstd, // L5
Compression.Zstd, // L6 (bottom-most)
};
new ColumnFamilyOptions().SetCompressionPerLevel(perLevel, (ulong)perLevel.Length);
Available algorithms: No, Snappy, Zlib, Bzip2, Lz4, Lz4hc, Xpress, Zstd. ZSTD usually wins on ratio, LZ4 on speed.
Upstream reference: Compression.
Comparator and prefix extractor
By default RocksDB sorts keys bytewise (memcmp). Override only if you need a different ordering — and keep in mind that the comparator name is persisted and must be stable across runs.
new ColumnFamilyOptions()
.SetComparator(myComparator);
new ColumnFamilyOptions()
.SetPrefixExtractor(SliceTransform.CreateFixedPrefix(4));
A prefix extractor unlocks prefix-seek — a much faster scan when most queries hit one prefix bucket.
Merge operator
var op = MergeOperators.Create("StringAppend", partial, full);
new ColumnFamilyOptions().SetMergeOperator(op);
See the Merge Operator guide.
Table format
SetBlockBasedTableFactory plugs in a configured BlockBasedTableOptions:
var table = new BlockBasedTableOptions()
.SetBlockSize(16 * 1024)
.SetFilterPolicy(BloomFilterPolicy.Create(10))
.SetCacheIndexAndFilterBlocks(true);
new ColumnFamilyOptions().SetBlockBasedTableFactory(table);
BlockBasedTable options reference
Optimisation shortcuts
| Setter | Profile |
|---|---|
OptimizeForPointLookup(ulong blockCacheSizeMb) |
Optimises for key-by-key reads (hash index, larger Bloom). |
OptimizeLevelStyleCompaction(ulong memtableMemoryBudget) |
Sensible defaults for level-style. |
OptimizeUniversalStyleCompaction(ulong memtableMemoryBudget) |
Sensible defaults for universal-style. |
new ColumnFamilyOptions()
.OptimizeForPointLookup(blockCacheSizeMb: 128);
Inspecting effective options
Once the DB is open, use:
string s = db.GetProperty("rocksdb.cfstats", users);
…or the human-readable global one:
string s = db.GetProperty("rocksdb.stats");