FullStack AI Hub
Studio
System Design · 03. Data & Storage
13 min
10 steps
Intermediate

Storage Engines

Trace write-ahead logs, fsync durability, B-Tree page updates, LSM-Tree SSTables, Bloom filters, and compaction.

System Design03. Data & StorageLesson 3 of 11
13 minIntermediate10 steps

Storage Engines

Trace write-ahead logs, fsync durability, B-Tree page updates, LSM-Tree SSTables, Bloom filters, and compaction.

I/O PATTERN
Random 8 KB
COMMIT P95
86 ms
CRASH SAFETY
Torn-page risk
SUSTAINED RPS
1,400/s
random 8KB overwriteWrite ClientsDirect PageWriter84%random fsyncScattered Data Pages90%torn-page risk
WAL / memtable write
read lookup
Bloom / cache hit
compaction / random I/O
write stall / crash
In This LessonStep 1 of 10
What You Will Master
  • ✓Explain how Write-Ahead Logs (WAL) and fsync guarantee crash recovery without random disk writes on commit
  • ✓Contrast in-place B-Tree page mutations against append-only LSM-Tree Memtable + SSTable flushes
  • ✓Eliminate wasted SSTable disk reads across LSM levels using in-memory Bloom Filters
  • ✓Navigate compaction strategies and the Read, Write, and Space Amplification triangle

Architectural Trade-Off Comparison Matrix

PatternBest ForKey Trade-Off
Write-Ahead Log (WAL + fsync)Guaranteeing crash durability on every committed write via fast sequential appendsGroup-commit batching is needed so per-commit `fsync` doesn’t bottleneck throughput
B-Tree Engine (PostgreSQL / InnoDB)Read-heavy OLTP workloads needing predictable p99 latency and single-location key lookupsRandom in-place 8 KB page rewrites cause high write amplification under heavy ingestion
LSM-Tree Engine (RocksDB / Cassandra)Write-heavy streams converting random mutations into sequential Memtable → SSTable flushesBackground compaction steals disk bandwidth and can cause tail-latency write stalls
Bloom FiltersAnswering "definitely not in this SSTable" in RAM before issuing a wasted SSD readSmall tunable false-positive rate (typically 1%) and ~10 bits of RAM per stored key

Step-by-Step Architectural Breakdown (10 Steps)

Full architectural walkthrough transcript and state transitions for Storage Engines.

STEP 01 · SETUPJump to step →

Why raw in-place disk writes crawl at scale

Suppose a naive storage engine writes every 200-byte update directly to its final random location on disk and calls `fsync` immediately.

Even on NVMe SSDs, **random 8 KB page rewrites** force write amplification inside flash blocks and cap throughput early—while a sudden power loss mid-write leaves half-written corrupt pages.

watch:Sequential disk appends are an order of magnitude faster than scattered random writes—and far safer to recover after a power crash.

Random vs Sequential I/OTorn Pagesfsync Durability
STEP 02 · SOLUTIONJump to step →

Step 1 of every durable engine: the Write-Ahead Log (WAL)

Every serious database (PostgreSQL, MySQL, RocksDB, Raft) writes mutations to a **sequential Write-Ahead Log (WAL)** *before* modifying in-memory pages.

With **Group Commit**, the engine batches concurrent commits into one sequential `fsync` every ~1 ms—returning durable ACKs in **4 ms** while dirty memory pages flush lazily to disk in the background.

idea:If power dies two milliseconds after ACK, the database simply replays the sequential WAL on boot—recovering 100% of committed data with zero corruption.

Write-Ahead Log (WAL)Group CommitCrash Replay
STEP 03 · TECHNIQUEJump to step →

B-Tree Engine: fast reads, expensive in-place page rewrites

In a **B-Tree storage engine** (PostgreSQL, InnoDB), every key exists in **exactly one fixed 8 KB page** on disk. Reads are ultra-predictable: traverse 3 levels and stop.

The cost hits on heavy random writes: modifying 100 bytes forces rewriting an entire **8 KB page** (plus WAL full-page images)—yielding **10–40× write amplification** when ingesting telemetry or chat firehoses.

idea:B-Trees optimize for read predictability (one authoritative copy of every key) at the expense of random page rewrites during checkpoints.

In-Place Page UpdateCheckpoint FlushingB-Tree Write Amp
STEP 04 · SOLUTIONJump to step →

LSM-Trees: turn random writes into sequential SSTable flushes

**Log-Structured Merge-Trees (LSM-Trees)**—powering RocksDB, Cassandra, LevelDB, and CockroachDB—never overwrite disk pages in place.

Writes append to the WAL and insert into an in-memory sorted **Memtable** (Red-Black Tree / SkipList). When the Memtable fills (~64 MB), it flushes sequentially as an **immutable sorted file (SSTable)** at full SSD bandwidth!

idea:LSM-Trees achieve 5–10× higher sustained write throughput than B-Trees because disk writes are 100% sequential immutable file flushes.

MemtableImmutable SSTablesSequential Flush
STEP 05 · PROBLEMJump to step →

The LSM read tax: checking multiple SSTable levels

Because SSTables are immutable, older versions of a key stay in deeper levels (**L0 → L1 → L2 → L3**) while newer updates sit at the top.

Reading a key that **doesn’t exist** forces the engine to probe the Memtable, then **every overlapping L0 SSTable**, then L1, L2, and L3 on SSD—turning a simple lookup into **8 separate disk reads**.

watch:Worst-case LSM read latency hits on missing keys: without Bloom Filters, the engine must check every single level before proving a key doesn’t exist.

Read AmplificationOverlapping L0 FilesMissing-Key Penalty
STEP 06 · SOLUTIONJump to step →

Bloom Filters: skip 99% of unnecessary SSTable reads in RAM

Attach a tiny in-memory **Bloom Filter** (~10 bits per key) to every SSTable. Before opening an SSTable on SSD, hash the key into its bit array.

If any bit is `0`, the key is **100% definitely NOT in that file**—skip the disk read immediately! Only ~1% false positives ever touch SSD, slashing read amplification from 8 probes down to **~1**.

idea:Bloom Filters never produce false negatives ("definitely not here" is 100% guaranteed), making them the secret weapon that makes LSM-Tree reads fast.

Bloom Filter Bit ArrayZero False Negatives1% False Positive Rate
STEP 07 · TECHNIQUEJump to step →

Background Compaction: merging SSTables & reaping tombstones

Without cleanup, flushed SSTables pile up endlessly with stale overwritten keys and **delete tombstones**. Background **Compaction** merge-sorts older SSTables into larger clean levels and discards dead versions.

**Size-Tiered Compaction** merges similarly sized files (great write throughput, up to 50% temp disk overhead). **Leveled Compaction** (RocksDB default) keeps non-overlapping key ranges per level (tighter space & faster reads, more background rewrites).

idea:Pick Size-Tiered Compaction for write-heavy append logs; pick Leveled Compaction for balanced read/write workloads and tighter SSD budgets.

Leveled CompactionSize-Tiered CompactionTombstone GC
STEP 08 · TRADE-OFFJump to step →

The LSM tail-latency hazard: Compaction Write Stalls

During a massive write spike, Memtables flush into L0 faster than the Compactor can merge them into L1. Meanwhile, compaction competes with live queries for the **exact same SSD bandwidth**.

When L0 crosses its file threshold, the engine intentionally **stalls incoming writes** (`write stall`) to prevent runaway read latency—causing sudden p99 latency spikes.

watch:LSM-Trees deliver higher average write throughput than B-Trees, but unthrottled compaction spikes cause worse p99 latency jitter unless rate-limited.

Compaction StormWrite StallsI/O Rate Limiting
STEP 09 · TRADE-OFFJump to step →

The RUM Conjecture: Read, Write, and Space Amplification

Storage engine physics obeys the **RUM Conjecture**: you can optimize at most **two** of **Read Amplification**, **Write (Update) Amplification**, and **Space (Memory) Amplification**—never all three simultaneously.

**B-Trees** minimize Read Amp (single exact page) at higher Write/Space cost; **Leveled LSMs** minimize Write & Space Amp with slightly higher Read checks; **Size-Tiered LSMs** maximize Write speed at higher Space overhead.

idea:Storage engines aren’t magic—every architecture simply chooses which leg of the Read / Write / Space amplification triangle to pay.

RUM ConjectureRead vs Write vs Space AmpEngine Selection
STEP 10 · RECAPJump to step →

Recap: inside every database engine on disk

Under the SQL or NoSQL API, almost every production database relies on the same building blocks: a **sequential WAL** for crash durability, an **in-memory buffer/memtable**, and either **B-Tree pages** or **LSM SSTables** on SSD.

Remember: **B-Trees** for crisp read-heavy OLTP, **LSM-Trees + Bloom Filters** for write-heavy telemetry/messaging, and always monitor **compaction lag** and **buffer pool hit ratio**.

idea:Understanding WAL, Memtables, SSTables, Bloom Filters, and Compaction unlocks how PostgreSQL, MySQL, RocksDB, Cassandra, and Kafka actually achieve speed and durability.

WALB-TreesLSM-TreesBloom FiltersCompaction

Related Production Deep-Dives & Simulators

Included In Free Course
Free · Verifiable Credential

FS-102LLM Systems Foundations

Build a connected mental model from tokens through retrieval evaluation security and operations.

24 concepts~18h self-paced
Start Free Course
Weekly Engineering Dispatch

Get the weekly systems teardown

One technical note per week covering inference bottlenecks, retrieval harnesses, and commit-pinned teardowns.