· 7 min read
LSM tree vs B-tree: how storage engines trade writes for reads
LSM tree vs B-tree explained: how memtables, SSTables and compaction make writes cheap, what it costs reads, and when to pick each storage engine.

An LSM tree (log-structured merge tree) is a storage engine design that makes writes cheap by never updating data in place: it buffers writes in memory, flushes them as immutable sorted files, and merges those files in the background. A B-tree does the opposite, keeping one sorted structure on disk and updating its pages in place. In short, LSM trees trade read and background work for fast writes, while B-trees trade write cost for predictable reads.
Most databases you use pick one of the two. PostgreSQL and MySQL's InnoDB store tables and indexes in B-trees. RocksDB, LevelDB, Cassandra and ScyllaDB are built on LSM trees. Knowing which one sits under your data explains a lot of behavior that otherwise looks random: why deletes don't free space, why reads slow down after a write burst, why disk I/O spikes when nothing is happening.
How does a B-tree store data?
A B-tree is a balanced tree of fixed-size pages (blocks of a few kilobytes, 8 KB in PostgreSQL). Each page holds sorted keys and pointers to child pages. Lookups walk from the root to a leaf, which takes three or four page reads even for very large tables, and the upper levels are usually cached in memory.
Writes find the right leaf page and modify it. If the page is full, it splits into two and the parent gets a new pointer. To survive crashes, the change is first appended to a write-ahead log (WAL), a sequential file of changes that can be replayed after a crash, and the modified page is written back later.
The cost shows up on writes. Changing a 100-byte row still means eventually writing a whole page, and random inserts touch pages scattered across the disk. Reads, though, are simple: one path, one place where each key lives.
How does an LSM tree handle writes?
An LSM tree splits the job into stages:
- The write goes to a WAL for durability and into the memtable, a sorted in-memory structure.
- When the memtable reaches a size limit, it is written to disk as an SSTable (sorted string table), an immutable file of sorted key-value pairs.
- A background process called compaction merges SSTables, keeps the newest version of each key, and drops overwritten or deleted data.
Nothing on disk is ever modified. An update is just a newer entry for the same key, and a delete is a special entry called a tombstone that marks the key as gone. Every disk write is a large sequential write, which is what makes LSM trees fast for write-heavy workloads.
Here is a toy version that shows the moving parts:
TOMBSTONE = object()
class TinyLSM:
def __init__(self, memtable_limit=4):
self.memtable = {}
self.sstables = [] # newest first; each is a sorted list of (key, value)
self.limit = memtable_limit
def put(self, key, value):
self.memtable[key] = value
if len(self.memtable) >= self.limit:
self.flush()
def delete(self, key):
self.put(key, TOMBSTONE) # deletes are writes too
def flush(self):
self.sstables.insert(0, sorted(self.memtable.items()))
self.memtable = {}
def get(self, key):
if key in self.memtable:
v = self.memtable[key]
return None if v is TOMBSTONE else v
for table in self.sstables: # newest to oldest
for k, v in table: # real engines binary-search + Bloom filter
if k == key:
return None if v is TOMBSTONE else v
return None
def compact(self):
merged = {}
for table in reversed(self.sstables): # oldest first, newer wins
merged.update(table)
live = [(k, v) for k, v in sorted(merged.items()) if v is not TOMBSTONE]
self.sstables = [live]
Writing twelve values across six keys and then deleting one leaves three SSTables plus a memtable holding the tombstone. Reads still return the right answers because they check newest data first. After a flush and a compaction, there is one SSTable with five live keys; the overwritten versions and the deleted key are gone.
Why are LSM tree reads slower?
A key can live in the memtable or in any SSTable, so a read may have to check several places. A read for a key that doesn't exist is the worst case, since it has to rule out every file.
Real engines limit the damage in three ways:
- Bloom filters per SSTable let a read skip files that definitely don't contain the key. The mechanics are covered in how Bloom filters work and how to size them.
- Sparse indexes and block caches let the engine jump to the right block inside a file instead of scanning it.
- Compaction keeps the number of files that can hold a key small.
Range scans are harder. A Bloom filter can't help with "all keys between A and M", so the engine merges iterators from every overlapping file. B-trees just walk adjacent leaf pages.
What is write, read and space amplification?
These three numbers are the cleanest way to compare the designs.
- Write amplification: bytes written to disk per byte your application wrote.
- Read amplification: disk reads per logical read.
- Space amplification: bytes on disk per byte of live data.
You can't minimize all three at once. Compaction strategy decides where LSM trees land:
| B-tree | LSM, leveled compaction | LSM, size-tiered compaction | |
|---|---|---|---|
| Write cost | Page rewrites, random I/O | Data rewritten roughly once per level | Lowest; files merged less often |
| Point reads | Predictable, few pages | Close to one file per level | More files to check |
| Range scans | Strong | Good | Weaker |
| Space overhead | Page fill and fragmentation | Low | Can temporarily need extra free disk during merges |
| Deletes | Space reusable after cleanup | Tombstones until compaction | Tombstones linger longer |
Leveled compaction organizes SSTables into levels where each level is a fixed multiple larger than the one above (RocksDB's default multiplier is 10), and files within a level don't overlap. Size-tiered compaction waits until several files of similar size exist and merges them together. Leveled favors reads and space; size-tiered favors writes.
As a rough estimate, with a multiplier of 10 a piece of data can be rewritten about ten times per level as it moves down. With four levels that is write amplification in the tens. It sounds bad, but those writes are large and sequential, which disks and SSDs handle far better than scattered page updates.
When should you use an LSM tree vs a B-tree?
Lean toward an LSM-based store when:
- Writes dominate: event logs, metrics, time series, message histories, activity feeds.
- You ingest at a steady high rate and can tolerate background I/O.
- Data is mostly appended, and reads are usually by key or recent ranges.
Lean toward a B-tree when:
- Reads dominate, or you need consistent read latency.
- You run many range scans, joins and secondary index lookups.
- Rows are updated in place often and you want space to be reused promptly.
Two operational traps come with LSM trees. First, deletes don't free space immediately; a tombstone has to survive until compaction can be sure no older copy of the key remains. Cassandra, for example, holds tombstones for gc_grace_seconds, ten days by default, so a table with heavy deletes can grow while you delete from it. Second, if writes arrive faster than compaction can merge, files pile up, reads get slower, and engines eventually slow down or stall writes to let compaction catch up. Monitor pending compaction work the way you'd monitor queue depth.
Key takeaways
- B-trees update sorted pages in place; LSM trees append to immutable sorted files and merge them later.
- LSM trees make writes sequential and cheap, and pay for it with extra read work and background compaction.
- Bloom filters, block indexes and compaction keep LSM reads fast, but range scans still touch many files.
- Leveled compaction favors reads and space; size-tiered favors write throughput.
- In LSM stores, deletes are writes, and space comes back only after compaction.
FAQ
Does PostgreSQL use an LSM tree?
No. PostgreSQL's default index type is a B-tree, and table data lives in heap pages updated through its WAL.
Why does my LSM database use more disk after deleting data?
Each delete writes a tombstone. The old values and the tombstones stay on disk until compaction merges the files that hold them, and some engines deliberately keep tombstones for a grace period first.
Is an LSM tree always faster for writes?
For sustained write-heavy workloads it usually is, because disk writes are batched and sequential. If compaction falls behind, though, the engine may throttle writes, so total throughput depends on compaction keeping up.