← engineering lobe

LSM trees: write now, sort later

2026-11-307 min read#deep-tech#databases#storage

Cassandra and RocksDB both store data in something called a Log-Structured Merge tree, or LSM tree 1 2. So does LevelDB, which RocksDB was forked from 2 3. It’s the design you pick when writes are what hurts.

In this article, let’s see how it works, with a small one you can write to and read from.

Problem

Most databases you know keep data in a B-tree. The data lives in fixed-size pages on disk, sorted by key. To update a key, you find its page, change it, and write the page back to the same place.

That’s a random write for every update. If you insert a lot (e.g., an event log, a history table, metrics), the disk spends its time jumping around to rewrite pages, often to change a few bytes in each.

The 1996 paper that introduced the LSM tree starts from exactly this. Its example is a bank’s history table that needs an index by account id. Keeping a B-tree index on it in real time “will effectively double the I/O cost of the transaction” 4.

So what if we never update anything in place?

The idea

Appending to the end of a file is cheap. Writing sorted data in big chunks is cheap. So an LSM tree only ever does those two 2 3:

  1. Every write is appended to a log on disk. This is the write-ahead log. It’s only there to recover after a crash.
  2. Then it goes into a sorted structure in memory, called the memtable. In RocksDB it’s a skiplist by default.
  3. When the memtable is full (about 4MB in LevelDB), it is written to disk in one go as a sorted file that never changes again. These files are called SSTables (sorted string tables). A fresh memtable takes over, and the old log can be deleted.

A write never reads anything, and never touches a file that’s already on disk. Cassandra’s docs call it “a write path free of read lookups” 1.

But now files pile up, and the same key can be in many of them with different values. That’s what the “merge” part of the name is about.

Compaction

Files on disk are arranged in levels 3:

  1. Fresh files from the memtable go to L0. Their key ranges can overlap, since each one is just whatever was in memory at the time.
  2. In L1 and below, files in the same level never overlap. Each level is one big sorted run, cut into files.
  3. Each level is about 10 times bigger than the one above. In LevelDB, L1 holds 10MB, L2 100MB, and so on.

When L0 has 4 files, LevelDB merges them with every L1 file they overlap, and writes out new L1 files. When any other level grows past its size, one file from it is merged with the overlapping files in the next level down 3. Every input is already sorted, so this is the merge step from merge sort: walk the files side by side and write out one sorted result.

If a key shows up more than once during a merge, only the newest version survives. “Compactions drop overwritten values” 3.

Try it. Each cell is a key and its version number. Press Write until the memtable flushes, then Write 10 a few times until compactions start:

an lsm tree · each cell is key + version
you wrote
written to disk
write amplification

To give you a summary of what you just saw:

  1. Writes only land in memory.
  2. The disk only sees big, sorted writes: a flush or the output of a compaction.
  3. Old versions get thrown away on the way down.

Because each level is 10x the one above, most of the data sits in the last level. RocksDB’s docs put it at 90% 5.

Now look at the readout. An entry that survives is written once on flush, then again each time a compaction carries it down a level. The ratio of bytes the disk wrote to bytes you wrote is the write amplification. In Cassandra’s words, “Every write of data in Cassandra is re-written multiple times” 1. An LSM tree writes more bytes in total, but every one of those writes is sequential.

Note: the number stays small in the widget because there are only 16 keys. Most writes replace an older version, and those get dropped before they travel far. With mostly new keys, every entry makes the full trip down.

Deletes

You can’t remove a key from a file that never changes. So a delete is just another write: a special entry called a tombstone (a “deletion marker” in LevelDB). It hides older values of that key in older files 3.

The tombstone itself can only go once there’s nothing older left that it could be hiding. LevelDB drops one during compaction only “if there are no higher numbered levels that contain a file whose range overlaps the current key” 3. Press Delete above: the struck-out cells are tombstones, and they disappear in a compaction once nothing below them could still hold an older value.

Reads

So writes are cheap. What did that cost? A key can now be in the memtable, in any L0 file, or in one file per lower level. A read checks them newest first and stops at the first hit 3 5:

  1. The memtable.
  2. Every L0 file whose range covers the key, newest to oldest.
  3. In each lower level, the one file whose range covers the key. RocksDB finds it with a binary search over the files’ start and end keys.

If the first hit is a tombstone, the key is deleted.

Finding a key that exists is fine. The bad case is a key that doesn’t exist: you have to look everywhere before you can say no, and each look is a disk read.

Bloom filters

The fix is to give every file a tiny summary that can answer “is this key in here?” without reading the file. That summary is a Bloom filter, from a 1970 paper by Burton Bloom 6.

It’s an array of bits, all 0 at the start. To add a key, hash it with a few hash functions and set those bits to 1. To check a key, hash it the same way and look at its bits:

  1. If any of them is 0, the key was definitely never added.
  2. If all of them are 1, it may have been. Other keys could have set those bits.

So it can say “maybe” for a key that isn’t there (a false positive), but it never says “no” to a key that is. Try it:

a bloom filter · 32 bits, 3 hash functions
words added
bits set
false positive chance

That’s exactly what a read wants. “No” means skip the file, no disk read. “Maybe” means read it, and a false positive only costs one wasted read. RocksDB’s docs use about 10 bits per key as the example, which gives a false positive rate of about 1% 7.

Here is the same tree with reads. Pick a key and press Get, then turn the filters off and try the same key again:

an lsm tree · reads check newest first

For a missing key like c or m, the filters usually skip every file, so it costs no disk reads at all. Turn them off and the same miss reads every file whose range covers it.

Final Thoughts

LSM trees aren’t free. A few things worth knowing:

  1. Writes are cheap, but each byte gets written several times over its life 1.
  2. Bloom filters fix point lookups, not range scans. A scan has to merge from every level at once. RocksDB’s docs: “Most LSM-tree engines cannot support an efficient range scan API because it needs to look into multiple data files” 2.
  3. Compaction has to keep up. If writes come in faster than it can merge, L0 files pile up and every read gets slower. LevelDB’s notes suggest slowing writes down on purpose when that happens 3, and RocksDB will stall writes when flushes can’t keep up 2.
  4. Compaction is a knob. RocksDB’s default (leveled) keeps disk usage low. Its universal compaction writes fewer bytes, but uses more space and makes reads slower 2.

The 1996 paper already said where it fits: the LSM tree “is most useful in applications where index inserts are more common than finds that retrieve the entries” 4.

Footnotes

  1. Apache Cassandra, Storage Engine. ↩ ↩2 ↩3 ↩4

  2. RocksDB wiki, RocksDB Overview. Fork from LevelDB in section 1; memtable, logfile and sstfile in section 3; range scans, compaction styles, write stalls and the skiplist memtable further down. ↩ ↩2 ↩3 ↩4 ↩5 ↩6

  3. LevelDB, implementation notes (doc/impl.md). Log files, sorted tables, levels, compactions, deletion markers and the timing section. ↩ ↩2 ↩3 ↩4 ↩5 ↩6 ↩7 ↩8 ↩9

  4. Patrick O’Neil, Edward Cheng, Dieter Gawlick and Elizabeth O’Neil, The Log-Structured Merge-Tree (LSM-Tree), Acta Informatica, 1996. Both quotes are from the abstract. ↩ ↩2

  5. RocksDB wiki, Leveled Compaction. ↩ ↩2

  6. Burton H. Bloom, Space/Time Trade-offs in Hash Coding with Allowable Errors, Communications of the ACM, 1970. ↩

  7. RocksDB wiki, RocksDB Bloom Filter. ↩

Connected thoughts

neurons this one fires into: shared ideas first, then neighbours in time.