Video in progress
Why are LSM-tree databases like Cassandra or RocksDB very fast at writes but sometimes slow at reads?
An LSM-tree first appends a write to the WAL and an in-memory memtable, then flushes it to sorted files (SSTables) — only sequential writes reach the disk. On reads, a key may be in several files; Bloom filters and compaction soften this, but compaction itself eats disk and CPU.
📖 Answer
A B-tree (the indexes in PostgreSQL, MySQL) updates a record in place: it finds the right page and changes it. With lots of random writes, the disk is busy with random I/O.
An LSM-tree works differently:
- The write is appended to the WAL (for recovery after a crash) and to the sorted in-memory memtable.
- When the memtable fills up, it's flushed to disk as an immutable, sorted SSTable file — one sequential write.
- In the background, compaction merges small files and cleans out old versions and deletion markers (tombstones).
That's why writes are very cheap — ideal for "write-heavy, read-light" loads like logs, metrics, events and IoT.
Reads have a cost: the key may be in the memtable, or in one of several SSTables. Each file has a Bloom filter that says "this key definitely isn't here" and cuts out unnecessary disk reads. Range queries and keys with many versions still touch many files.
Problems seen on prod:
- if compaction falls behind, the number of files grows and read latency spikes;
- tombstones: after many deletes, reading an "empty" range wades through thousands of tombstones (Cassandra gives a dedicated warning for this);
- write amplification — a single write is rewritten several times during compactions, eating into SSD lifetime.
The choice question is this: if writes dominate and access patterns are simple by key — LSM. Complex queries, joins, many secondary indexes — a relational database with B-trees.
Sign up to mark studied questions and save your progress.