Skip to main content
← PROBLEMS / SYSTEM-DESIGN
An end-to-end domain learning journey

Design a Write-Optimized
LSM-Tree Storage Engine.

A B+Tree saturates 80K NVMe IOPS flushing random 16 KB dirty pages under 250,000 random writes/sec; an LSM-Tree converts writes into sequential appends.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When a single bottleneck threatens end-to-end availability, append writes to a CRC32C-checksummed WAL + 64 MB SkipList MemTable, flush to L0 SSTables with 10-bit/key Bloom filters + sparse indexes, and run leveled compaction (L0->Ln) with L0 write-stall backpressure.

TL;DR30-second simplified summary
▾

Core Mechanics: At 250,000 random 1 KB writes/sec (250 MB/s), a B+Tree must read 16 KB leaf pages from disk, dirty them in RAM, and flush random 16 KB pages back to NVMe—quickly saturating 80,000 random read/write IOPS and accelerating SSD garbage collection.

Visual & Structural Flow: Depicts Group-Commit WAL + 64MB SkipList MemTable -> Immutable L0 SSTables (Put/Delete(k, v) -> CRC32C WAL (128MB) -> SkipList MemTable -> L0..Ln SSTables); MemTable -> Overlapping L0 -> Disjoint L1..Ln with Bloom Filters (MemTable (64MB) -> L0 SSTables (<=12) -> L1..Ln (10x ratio) -> 4KB Block).

Trade-Offs & Production: Size-Tiered Compaction (STCS) incurs 2x temporary space amplification (20 TB on a 10 TB dataset) and poor point-lookup latency. Meet all write, read, RAM (16 GB), and recovery (< 10 s) targets: Write & Crash Path: Group-commit to a CRC32C-checksummed WAL (32 KB blocks; truncate at first.

Failure Modes & Application: LSM-Trees power the storage layer of modern distributed databases: RocksDB / Pebble: Embedded inside CockroachDB, TiKV, Kafka Streams, and Spark Structured Streaming state stores.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why B+Trees hit an I/O wall on write-heavy workloads.

Why this matters: The mechanism: understand how Why B+Trees hit an I/O wall on write-heavy workloads works from first principles.

Think of Why B+Trees hit an I/O wall on write-heavy workloads as the intuition layer for Design a Write-Optimized LSM-Tree Storage Engine: The mechanism: understand how Why B+Trees hit an I/O wall on write-heavy workloads works from first principles.

At 250,000 random 1 KB writes/sec (250 MB/s), a B+Tree must read 16 KB leaf pages from disk, dirty them in RAM, and flush random 16 KB pages back to NVMe—quickly saturating 80,000 random read/write IOPS and accelerating SSD garbage collection. An LSM-Tree turns every Put and Delete into sequential appends.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

90% negative lookup amplification, L0 write stalls, and torn WAL records.

Why this matters: The problem: see what breaks without 90% negative lookup amplification, L0 write stalls, and torn WAL records under real concurrency or scale.

Naive LSM-Tree implementations fail on three fronts:

  1. 90% Negative Lookup Read Amplification: Without Bloom filters, checking whether a missing key exists forces disk reads across every L0 SSTable and L1..Ln level, blowing past the 8 ms p99 SLA.
  2. L0 Write Stalls: If foreground writes (4 flushes/sec of 64 MB MemTables) outpace L0 -> L1 compaction, overlapping L0 files pile up and freeze writers.
  3. Torn WAL Tail on Crash: Unclean power loss leaves a partial WAL record that halts recovery.
LSM-Tree Write & Recovery

Group-Commit WAL + 64MB SkipList MemTable -> Immutable L0 SSTables

Put/Delete(k, v)250k writes/s (250 MB/s)CRC32C WAL (128MB)Seq append, <0.5s replaySkipList MemTable64MB active + immutableL0..Ln SSTables4KB blocks + 10b/key…

Sequential WAL + MemTable flushes convert 250k random writes/sec into fast sequential NVMe I/O with <0.5 s crash replay.

Engineering Decision Framework
Problem
How to implement Design a Write-Optimized LSM-Tree Storage Engine without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When a single bottleneck threatens end-to-end availability, append writes to a CRC32C-checksummed WAL + 64 MB SkipList MemTable, flush to L0 SSTables with 10-bit/key Bloom filters + sparse indexes, and run leveled compaction (L0->Ln) with L0 write-stall backpressure.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Design a Write-Optimized LSM-Tree Storage Engine experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Naive size-tiered compaction and unindexed SSTables.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Think of Naive size-tiered compaction and unindexed SSTables as the intuition layer for Design a Write-Optimized LSM-Tree Storage Engine: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Size-Tiered Compaction (STCS) incurs 2x temporary space amplification (20 TB on a 10 TB dataset) and poor point-lookup latency. Meanwhile, scanning SSTables without per-SSTable 10-bit/key Bloom filters wastes 90% of NVMe IOPS on negative lookups.

Component Topology

Naive size-tiered compaction and unindexed SSTables

Client TxingressQuery PlannercomputeWAL BufferstorageStorage PagescontrolSQL queryappend redocheckpoint flush
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: Leveled Compaction + Bloom Filters + Tombstone Scoring.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Meet all write, read, RAM (16 GB), and recovery (< 10 s) targets:

  • Write & Crash Path: Group-commit to a CRC32C-checksummed WAL (32 KB blocks; truncate at first checksum mismatch on crash -> 128 MB replays in < 0.5 s at 3.5 GB/s). Insert (userKey ASC, seqNum DESC) into a 64 MB SkipList MemTable (flushes every 0.256 s).
  • Read Path (<= 8 ms p99): Check Active MemTable -> Immutable MemTables -> L0 SSTables -> at most 1 SSTable per L1..Ln (10x level multiplier). 10-bit/key Bloom filters (~1% false positive) eliminate 99% of negative disk seeks within the 16 GB RAM budget.
  • Compaction & Guardrails: Soft throttle at 12 L0 files, hard stop at 24, parallel subcompactions, and priority compaction of SSTables with > 20% tombstones + range-delete tombstones.
Leveled Read Hierarchy

MemTable -> Overlapping L0 -> Disjoint L1..Ln with Bloom Filters

MemTable (64MB)SkipList (seqNum DESC)L0 SSTables (<=12)L1..Ln (10x ratio)Page 04KB BloPage 2Page 3

Disjoint L1..Ln key ranges and 10-bit/key Bloom filters bound point lookups to <= 8 ms even at 10 TB.

// InternalKey ordering: userKey ASC, seqNum DESC (newest version wins)
record InternalKey(byte[] userKey, long seqNum, byte opType) implements Comparable<InternalKey> {
    @Override
    public int compareTo(InternalKey o) {
        int cmp = Arrays.compareUnsigned(this.userKey, o.userKey);
        if (cmp != 0) return cmp;
        return Long.compare(o.seqNum, this.seqNum); // Descending seqNum
    }
}
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where LSM-Tree storage engines are used.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

LSM-Trees power the storage layer of modern distributed databases:

  • RocksDB / Pebble: Embedded inside CockroachDB, TiKV, Kafka Streams, and Spark Structured Streaming state stores.
  • Cassandra / ScyllaDB: Uses MemTable + SSTable + leveled/time-window compaction for high-ingest wide-column workloads.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

In an LSM-Tree handling 90% negative lookups across 10 TB, how do 10-bit/key Bloom filters and Leveled Compaction keep p99 `Get()` under 8 ms?

Recommended Learning Flow