Question #17 of 60•Databases•Hard• ~3 min read

How does an LSM-Tree storage engine differ from a B-Tree?

Executive Takeaway

Compare the mechanics of write-optimized Log-Structured Merge-trees (MemTable, SSTables, compaction) with read-optimized B-Trees.

Architectural Breakdown & AnalysisFAANG Aligned

Databases store records differently on disk based on whether their workload is write-heavy or read-heavy.

B-Tree (Read-Optimized: PostgreSQL, MySQL)

  • Organizes data in self-balancing multi-way trees on disk.
  • Updates pages in-place via random disk I/O.
  • Pros: Lightning-fast point lookups ($O(\log N)$) and range queries.
  • Cons: High write amplification due to page rewrites.

LSM-Tree (Write-Optimized: Cassandra, RocksDB)

  • Appends all writes sequentially to an in-memory buffer (MemTable) and Write-Ahead Log (WAL).
  • Flushes MemTable periodically to immutable sorted disk files (SSTables). Background threads compact duplicate keys.
  • Pros: Ultra-fast write speeds via sequential disk appends.
  • Cons: Read path requires searching multiple SSTables using Bloom Filters.
TIP — Storage Selection

Choose B-Trees for transactional read-heavy OLTP workloads. Choose LSM-Trees for high-ingestion logging, telemetry, and time-series systems.

Key Architectural Comparison

Storage EngineWrite StrategyRead StrategyRandom I/O ImpactBest Fit Workload
B-TreeIn-place overwrite of disk pages.Direct $O(\log N)$ point lookups.High random write I/O.Read-heavy OLTP databases (PostgreSQL, MySQL).
LSM-TreeSequential append to MemTable + Commit Log.Multi-SSTable scan filtered by Bloom Filters.Zero random write I/O (pure sequential disk writes).High-ingestion write workloads (Cassandra, RocksDB).
Previous QuestionWhen do you choose Eventual Consistency vs. Strong Consistency (PACELC Theorem)?Next Question How do you design a resilient retry mechanism with exponential backoff and jitter?

Continue Learning on Being SDE

Explore deep-dive system architecture modules, low-level object-oriented designs, and interview cheat sheets.