LSM tree
A log-structured merge tree buffers writes in a sorted in-memory table and flushes it to disk as immutable sorted files, which background jobs merge into larger files over time. It exists because appending sorted runs is sequential I/O, so a disk or SSD can absorb tens of thousands of random-key writes per second that would otherwise be random page writes.
What it costs
| Operation | Cost | Note |
|---|---|---|
| Insert / update / delete | O(log m) memory + 1 sequential WAL append | m is the memtable size. No disk page is modified; a delete writes a tombstone (a marker meaning "deleted"). |
| Point lookup | O(levels) file probes, ~1 real read | Check memtable, then each level newest to oldest; a Bloom filter per file skips files that cannot hold the key. |
| Range scan | O(levels × log n + k) | Merge iterators across memtable and every level; slower than a B-tree range because results come from several files. |
| Flush memtable | O(m) sequential write | Happens when the memtable fills (64–256 MB); the old memtable keeps serving reads until the file is on disk. |
| Compaction | ~10× total bytes rewritten over a key’s life (levelled) | Background merge of overlapping files; the price paid for cheap writes. |
Memory: One or two memtables (64–256 MB each), a Bloom filter per file (about 10 bits per key, so 1.25 GB for a billion keys), and a block cache for hot file blocks.
Memtable, flush, levels and compaction
Writes land in the log and the memtable. A full memtable flushes as an immutable SSTable at level 0; compaction merges files downward into exponentially larger, non-overlapping levels. A read probes top to bottom, skipping files via Bloom filters.
How it works
- A write appends the key and value to the write-ahead log (a sequential file that survives a crash), then inserts them into the memtable, an in-memory sorted structure, usually a skip list. The write is acknowledged now; nothing on disk was modified in place.
- When the memtable reaches its size limit it becomes read-only, a fresh memtable takes new writes, and the full one is written out in key order as an SSTable: a sorted string table, an immutable file of sorted key-value pairs with a small index of block offsets and a Bloom filter listing which keys it holds.
- Freshly flushed files sit at level 0 and may overlap each other in key range. Compaction picks files from one level, merges them with the overlapping files in the next level into new sorted files, drops older versions of a key and any key under a tombstone, and deletes the inputs. Each level is about ten times larger than the one above.
- A lookup checks the memtable, then level 0 files newest first, then one file per deeper level, because within a level below 0 the key ranges do not overlap. Before opening a file it consults that file’s Bloom filter, so most files are skipped with one hash check.
- The newest version of a key wins, so an update is simply a new entry that shadows the old one until compaction physically removes it. A tombstone shadows older values the same way and is removed only when it reaches the deepest level.
Why it is shaped this way
- Writes never seek: appending to the log and flushing a sorted run are both sequential, which is what spinning disks and SSDs do fastest and what wears SSDs least.
- Reads pay instead: a key may be in any of several files, so Bloom filters and a block cache exist to make the common case one real read.
- Immutable files are easy to replicate, snapshot and cache, and they never suffer page corruption from a half-written update.
Worked example
Chat message store, one shard taking 70K messages per second at peak (a tenth of the 700K per second fleet-wide peak)
- Each message is about 200 B with a key of (conversation_id, seq). 70K × 200 B = 14 MB per second of memtable inserts. A 128 MB memtable fills every 9 seconds and flushes as one sequential 128 MB write, about 0.3 s on one NVMe drive.
- On a B-tree the same load is 70K random leaf writes per second, each dirtying an 8 KB page: 560 MB per second of random page writes plus the log. That is several drives’ worth of random I/O for 14 MB of actual data.
- Levelled compaction rewrites each byte about 10 times over its life, so sustained disk writes are 14 MB × 10 = 140 MB per second, sequential. Still comfortably one drive.
- A read of "last 50 messages in conversation 42" is a range scan on a key written recently, so it is almost always in the memtable or level 0: one or two file reads. Reading history from six months ago may touch one file in each of five levels, but Bloom filters skip the ones that cannot contain the conversation.
- Bloom filters for 10 billion stored messages at 10 bits each: 12.5 GB across the cluster, a few hundred MB per node.
When it is the wrong choice
- Read-heavy point lookups with frequent in-place updates (an account balance, a trip row changing state): the B-tree answers in one page read and never pays compaction.
- Workloads that need predictable latency: compaction competes for disk bandwidth and can stall writes for seconds when it falls behind.
- Small datasets that fit in memory: the complexity buys nothing when every structure is already cached.
Interviewer follow-ups
Answer out loud before you open each one.
Why are writes fast but reads potentially slower?
A write touches only the log and memory, both sequential or in RAM. A read may have to look in the memtable and then one file per level, because the newest value for a key could be anywhere. Bloom filters, the block cache and compaction keep that to about one real disk read in practice.
What is a tombstone and why does it stick around?
A tombstone is a small record meaning "this key is deleted", written like any other entry because files are immutable and cannot be edited. It must survive until compaction has pushed it past every older version of the key, otherwise an old value in a deeper level would reappear.
Levelled or size-tiered compaction?
Levelled keeps non-overlapping files per level, so reads check one file per level and space overhead is about 10%, but each byte is rewritten about 10 times. Size-tiered merges files of similar size, rewriting about 4 times, but reads may hit many overlapping files and disk can temporarily double during a merge. Pick levelled for read-and-write stores, size-tiered for append-mostly logs.
How does a Bloom filter help here?
Before reading a file the engine hashes the key and checks the file’s Bloom filter. If the filter says the key is absent, that is certain and the file is skipped; if it says present, it is probably right (about 1% false positives at 10 bits per key). So a point lookup opens roughly one file instead of one per level.
Where it shows up
- Building blocks
- Write-optimised vs read-optimised storage · Append-heavy, time-ordered rows · Know your write amplification · Know your read amplification · Metrics and events over time · Delete without losing history
- Problems
- Chat App (WhatsApp / Messenger) · News Feed (Twitter / X timeline) · Ride Matching (Uber / Lyft)