Skip to content
AI360Xpert

Storage Engine Internals

Storage Engine Internals architecture
Storage Engine Internals architecture

Overview

A storage engine is the component inside a database that actually writes data to and reads it from disk. The two dominant designs are B-tree-based engines, which update data in place and favor reads, and LSM-tree-based engines, which append writes and merge them in the background to favor high write throughput.

🧠 Mental model: B-tree is like a well-organized filing cabinet - every document has a fixed slot, fast to find, but rearranging on every insert is work. LSM-tree is like a pile of sticky notes - write fast by just adding to the pile, then sort and merge them later.

Key Concepts

A B-tree-based engine keeps keys in a balanced, sorted tree and updates pages in place. Reads are fast and predictable because a key sits in exactly one place, but writes may require random disk I/O and page splits.

An LSM-tree-based engine (log-structured merge-tree) buffers writes in an in-memory table (a memtable), then flushes them as immutable sorted files (SSTables) on disk. A background compaction process merges these files to reclaim space and keep reads bounded.

Because a key can exist in several SSTables, a read may check multiple files; engines mitigate this with in-memory summaries and Bloom filters that quickly rule out files that cannot contain the key.

Aspect B-tree LSM-tree
Write path In-place update Append to memtable, later compaction
Write pattern Random I/O Sequential I/O
Read performance Fast and predictable May scan several SSTables
Best for Read-heavy, transactional Write-heavy, high-ingest

These engines are what make Database Indexing concrete: a B-tree index updates in place on every write, while an LSM index absorbs writes cheaply and pays later during compaction.

Trade-offs

LSM-trees accept read amplification (checking several files) and background compaction cost in exchange for very high, sequential write throughput. B-trees accept write amplification and random I/O in exchange for stable, low-latency reads. Neither wins outright - the right engine follows whether the workload is write-heavy ingest or read-heavy transactions.

Interview Tips

  • When a system is write-heavy (metrics, events, chat history), reach for an LSM-backed store and say why.
  • For read-heavy transactional workloads, a B-tree engine is the safer default.
  • Mentioning compaction and Bloom filters signals real depth on LSM internals.

Summary

  • The storage engine decides whether a database is read- or write-optimized.
  • B-tree engines update in place: fast, predictable reads with random-I/O writes.
  • LSM-tree engines append and compact: high sequential write throughput with read amplification.
  • LSM reads use Bloom filters to skip SSTables that cannot hold a key.
  • Match the engine to the workload: LSM for write-heavy ingest, B-tree for read-heavy transactions.