Courses System design · Part 5 · Lesson 8
Storage Engines: B-Tree or LSM-Tree
Under every database there is a storage engine: the part that decides how rows are laid out on disk, how they are found again, and how a write survives a crash. Two designs cover almost every database you will meet: the B-tree and the LSM-tree. Knowing the difference explains why Postgres is great at reads, and why Cassandra can take an enormous number of writes.
The B-tree: a sorted tree of pages
A disk is read in pages, blocks of a few kilobytes (8 KB in Postgres, 16 KB in MySQL’s InnoDB). A B-tree keeps the keys sorted in a tree of such pages:
- The root page holds a few hundred keys, each pointing to a page one level down.
- Each level splits the range further, until the leaf pages, which hold the rows themselves or pointers to them.
- With a few hundred keys per page, three or four levels are enough for hundreds of millions of rows: 500 × 500 × 500 = 125 million.
Reading one row means following one path from the root: three or four page reads, and the top levels are almost always in memory. This is the database index lab on a large scale: each step throws most of the rows away.
Writing means finding the right leaf page and changing it in place. If the page is full, it splits into two, and the parent gets one more key. To survive a power cut in the middle of that, every change is first written to a write-ahead log.
So a B-tree write touches the log and at least one page somewhere on the disk: a random write.
The LSM-tree: write now, sort later
The log-structured merge tree is built for writes:
- A write goes to the log (for crashes) and to a sorted table in memory (the memtable). That is all. Both are fast: the log is only appended to.
- When the memtable is full (say 64 MB), it is written to disk in one go as a sorted, read-only file, an SSTable.
- Over time there are many SSTables. A background job, compaction, merges them into bigger sorted files and drops old versions and deleted keys.
Reading a key means checking the memtable, then the SSTables from newest to oldest. To avoid opening every file, each SSTable has a Bloom filter: a small structure that answers “this key is definitely not here” or “it might be here”. Most files are skipped without reading them.
So an LSM write is only sequential appends, which disks love. The price is paid later and in the background.
What each one costs
| B-tree | LSM-tree | |
|---|---|---|
| Write | slower: a random page write, sometimes a split | fast: append to the log and memory |
| Read | fast and steady: 3–4 pages | can check several files; Bloom filters help |
| Space | pages are partly empty | compact files, compress well |
| Background work | little | compaction uses disk and CPU, and can slow reads when it falls behind |
| Range scans | very good: leaves are in order | good, but must merge several files |
The words to know: write amplification (one write from the app causes several writes on disk; LSM compaction rewrites data again and again, B-trees rewrite whole pages) and read amplification (one read checks several places; worse in LSM).
Who uses which
- B-tree: PostgreSQL, MySQL (InnoDB), SQL Server, Oracle, SQLite. The default for relational databases.
- LSM-tree: Cassandra, ScyllaDB, HBase, RocksDB and LevelDB (which many other systems use inside), and the newest data of many time-series databases.
The rough rule: mostly reads, updates in place, and range queries → B-tree. A huge stream of writes such as events, logs, metrics or messages → LSM-tree.
In an interview
You will rarely be asked to design a storage engine. But when you choose a database for a write-heavy part, such as location updates in a ride-sharing app or a chat app’s messages, one sentence helps: “this is write-heavy and mostly appends, so an LSM-based store like Cassandra fits; the read side is small and goes through a cache.” It shows you know why, not only what.
Summary
- A storage engine lays rows out on disk. Almost all are B-trees or LSM-trees.
- A B-tree finds a row in a few page reads and updates pages in place: steady reads, slower writes.
- An LSM-tree appends writes and sorts them later with compaction: fast writes, more work on reads.
- Choose by the shape of the work: read-heavy → B-tree; write-heavy streams → LSM.