When designing an NVMe LSM engine for <2 ms p99 reads, pair O_DIRECT + sharded Block Cache + Ribbon filters with adaptive compaction rate-limiting.
TL;DR30-second simplified summary▾
O_DIRECT stops page-cache thrashing, Ribbon filters cut filter RAM ~30%, and a 600 MB/s token bucket (boosting to 900 MB/s if L0 >= 10) keeps NVMe read queues shallow.
T=10, ~1.11x space amp) and why L0 file pileups degrade reads.O_DIRECT + an isolated Block Cache are used.R & E · Requirements, NVMe Bandwidth & Endurance Math.
200K writes/sec x 1 KB = 200 MB/s of logical writes. Add 200 MB/s for the sequential WAL = 400 MB/s before compaction even starts.
Illustrative Sizing: Under Leveled Compaction with ~10x–20x effective write amplification (assuming MemTable/L0 deduplication and Zstd compression ~2x), compaction writes ~1.0–1.5 GB/s if unthrottled—pushing total SSD writes to 1.4–1.9 GB/s (near 100% of the NVMe's 2.0 GB/s ceiling!). At that saturation point, NVMe controller GC stalls foreground read queues past 10 ms.
Buffered Unthrottled Compaction vs. O_DIRECT + Rate Limiter
- Compaction saturates 2.0 GB/s
- OS page cache evicted by cold reads
- Read p99 spikes >15 ms
- User-space Block Cache stays warm
- NVMe queue depth stays <16
- Read p99 stays <2 ms
S · Storage: 4 KB Aligned SSTables, Ribbon Filters & Block Cache.
O_DIRECT requires all reads and writes to be 4 KB-sector aligned in memory and on disk.
Each SSTable is structured into 4 KB–16 KB Zstd-compressed data blocks, a two-level partitioned Index Block, and a Ribbon Filter (10 bits/key -> ~1% false positive rate, using ~30% less RAM than Bloom filters). Only Index, Filter, and hot Data blocks enter the sharded user-space Block Cache (64 shards to avoid mutex contention).
4 KB Aligned Data Blocks (Zstd) -> Partitioned Index -> Ribbon Filter
H & A · Write Path, Pipelined MemTables & Read Path.
Incoming writes enter a Group-Commit WAL leader (batching fsync across concurrent threads) and an active Lock-Free SkipList MemTable (128 MB).
When the active MemTable hits 128 MB, it rotates atomically to an Immutable MemTable list (max 4 buffers) while a new active MemTable opens immediately—zero write pause! Background flush threads write L0 SSTables via O_DIRECT.
Group-Commit WAL -> MemTable -> O_DIRECT Block Cache -> NVMe
D · Detailed Design: Token-Bucket Compaction Rate Limiter.
Every background compaction write buffer (e.g., 1 MB chunk) must request bytes from a shared CompactionRateLimiter before calling pwrite().
The rate limiter refills tokens at 600 MB/s (with a small 8 MB burst). If compaction threads exhaust the bucket, they sleep for a few milliseconds instead of flooding the NVMe controller. Meanwhile, WAL and L0 flushes bypass (or get priority over) deep L2->L3 compactions so MemTables never back up.
600 MB/s Steady -> L0 >= 10 Boost (900 MB/s) -> L0 >= 20 Write Delay
- Problem
- How to prevent background LSM compactions from spiking foreground NVMe read p99 past 2 ms.
- Naive Solution
- Use standard buffered Linux file I/O and run compaction threads unthrottled.
- Why It Fails
- Buffered I/O evicts hot read pages from the OS page cache, and unthrottled writes saturate NVMe controller queues.
- Alternatives
- OS
ionice+ buffered I/O vs.O_DIRECT+ User-Space Block Cache + Token-Bucket Rate Limiter. - Engineering Decision
- Use
O_DIRECTfor all SSTables, cache hot reads in a 64-shard user-space Block Cache, and throttle L1+ compactions with an adaptive token bucket. - Trade-Offs
- Requires 4 KB buffer alignment and explicit user-space cache sizing in exchange for deterministic <2 ms p99 reads.
Your NVMe LSM engine runs with O_DIRECT disabled (using Linux buffered I/O). Every time a 20 GB L2->L3 compaction runs, point-read p99 latency jumps from 1 ms to 18 ms even though total SSD bandwidth is only at 50%. Why?
E & D · Evaluation, Assumptions, Limitations & Glossary.
Latency targets (<2 ms p99) assume local enterprise NVMe SSDs with deterministic PLP (Power-Loss Protection) and 4 KB aligned O_DIRECT.
Assumptions & Limitations: Sizing assumes 1 KB average values, ~2x Zstd compression, and ~10% hot working set fitting in the 64 GB Block Cache. If 100% of reads are uniformly random cold keys across 4 TB, Block Cache hit rate drops and p99 tracks raw NVMe queue latency + Bloom/Ribbon overhead.
Group-Commit WAL -> SkipList -> O_DIRECT Block Cache -> Rate-Limited LCS
Practice: Make the trade-off yourself.
Why does a high-write NVMe LSM engine enable `O_DIRECT` for SSTables and manage its own user-space Block Cache?