Sub topic 14
B-trees versus LSM-trees
This chapter assumes you know how a B+tree is searched, how inserts cause page splits, and what write amplification means at a basic level (one logical write causing several physical writes). It compares the B-tree design with the log-structured merge-tree (LSM-tree), the other common way to store an index on disk.
Two ways to handle writes
A B-tree updates pages in place. To insert a key, the database finds the right leaf page, changes it, and writes the page back. Reads are cheap because each key has one home in a sorted structure. Writes are random, because each one touches a page somewhere in the file.
An LSM-tree never updates in place. It does this instead:
- Writes go to a sorted in-memory buffer called the memtable. A write-ahead log on disk protects it from a crash.
- When the memtable is full, it is written to disk as one immutable sorted file called an SSTable (sorted string table).
- A background process called compaction merges SSTables into larger ones. It drops overwritten values and deleted keys as it goes.
Writes are therefore sequential appends and batch merges. The cost moves from the write path to reads and background work.
Read path
A key may live in the memtable or in any SSTable that has not yet been compacted away. A point lookup checks the memtable first, then SSTables from newest to oldest. To avoid reading files that cannot contain the key, most LSM engines keep a Bloom filter per file. A Bloom filter is a small probabilistic structure. It says either "definitely not here" or "maybe here".
A range scan must merge results from several files, which costs more than walking the linked leaves of a B+tree.
Amplification
Three ratios describe the trade-off.
- Write amplification: bytes written to disk per byte the application wrote.
- Read amplification: pages or files read per lookup.
- Space amplification: bytes on disk per byte of live data.
A B-tree rewrites a whole page, for example 8 KB, even when one small row changed. It also writes the log record. An LSM-tree rewrites each piece of data every time compaction moves it to a deeper level. Under leveled compaction this can be ten or more rewrites over its lifetime. The exact numbers depend on configuration and workload, so measure rather than assume.
Comparison
| Property | B+tree | LSM-tree |
|---|---|---|
| Write pattern | Random in-place page writes | Sequential flushes and merges |
| Point reads | One path from root to leaf | Memtable plus several files, helped by Bloom filters |
| Range scans | Fast, follows leaf links | Needs a merge across files |
| Space use | Fragmentation and bloat from half-full pages | Old versions linger until compaction |
| Latency | Steady | Can spike when compaction falls behind |
| Typical systems | PostgreSQL, MySQL InnoDB, SQL Server | RocksDB, Cassandra, LevelDB, HBase |
Choosing between them
Prefer a B+tree when the workload is read-heavy, needs predictable latency, or relies on range scans and transactions with strong consistency. This is the common case for general-purpose relational databases.
Prefer an LSM-tree when the workload has a very high write rate, such as event logs, metrics, or time-series ingestion. It also helps when you want compact files, since compaction can compress large sorted runs well.
Exercise
A service ingests 50,000 small sensor readings per second and runs a lookup by sensor ID only occasionally. Which index structure is the better fit, and what cost do you accept by choosing it?
Show answer
An LSM-tree. It turns the many small writes into sequential flushes and batch merges instead of random page updates. The cost is slower or more complex reads, since a lookup may check several files (reduced by Bloom filters), plus background compaction that uses I/O and can cause latency spikes.
Exercise
Why does an LSM-tree have higher space amplification than a B-tree right after many updates to the same keys?
Show answer
An update writes a new version into a newer file and leaves the old version in older SSTables. The old versions stay on disk until compaction merges the files and discards them, so for a while the disk holds more bytes than the live data needs.