Back to KB
Difficulty
Intermediate
Read Time
4 min

LSM Trees: Why Your Database Writes Are Fast and Your Reads Are Lying to You

By Codcompass TeamΒ·Β·4 min read

Current Situation Analysis

The fundamental failure mode of traditional B-tree storage engines emerges under sustained high-throughput ingestion. When profiling write-heavy workloads (e.g., 40–50k inserts/sec from event pipelines), the bottleneck is rarely CPU saturation or memory pressureβ€”it is await time on disk I/O. B-trees enforce strict on-disk sorted order, meaning every insert triggers a random write seek to a leaf node. At scale, this translates to tens of thousands of random seeks per second, which spinning disks physically cannot sustain and NVMe SSDs struggle with due to NAND flash architecture.

NVMe drives erase data in blocks of 128KB–512KB. Writing a single 4KB B-tree page forces the SSD controller to read the surrounding block, erase it, and rewrite the entire block. This hardware-level write amplification is compounded by B-tree maintenance: leaf node fills trigger page splits that propagate upward, causing cascading random writes. Under load, background writers fall behind (pg_stat_bgwriter shows buffers_backend_fsync spiking), forcing foreground queries to block on dirty page flushes. Traditional B-trees structurally cannot match high-write throughput because their architecture prioritizes read-optimized random access over sequential ingestion.

WOW Moment: Key Findings

Benchmarking B-tree vs. LSM-tree architectures under identical hardware constraints reveals a stark architectural trade-off. LSM trees defer sorting and enforce sequential I/O, shifting the cost from the write path to the read path and background compaction.

ApproachSustained Writes/secRandom I/O UtilizationPoint Read Latency (Cold Key)Write Amplification FactorHardware Saturation Point
B-Tree Index~50k100%0.5–1.0ms10–64x~50k–100k IOPS
LS

πŸŽ‰ Mid-Year Sale β€” Unlock Full Article

Base plan from just $4.99/mo or $49/yr

Sign in to read the full article and unlock all 635+ tutorials.

Sign In / Register β€” Start Free Trial

7-day free trial Β· Cancel anytime Β· 30-day money-back