LSM tree (log-structured merge tree)
A storage design that buffers writes in memory and flushes them as sorted files merged in the background; fast writes, more work on reads.
An LSM tree turns random writes into sequential ones. A write goes to a write-ahead log and an in-memory sorted table; when that fills up, it is flushed to disk as an immutable sorted file. Background compaction merges these files and drops overwritten and deleted values. The design comes from a 1996 paper by Patrick O’Neil and colleagues.
It powers RocksDB, LevelDB, Cassandra, ScyllaDB and the storage of many time-series and queue systems. Writes are cheap, but a read may have to check several files, so each file usually carries a Bloom filter that says “the key is definitely not here” and saves most of those checks.
The trade-offs show up in operations: compaction uses disk bandwidth and CPU, can cause latency spikes, and needs free disk space to rewrite files. Deletes are written as markers (tombstones) that only disappear after compaction.
Sources
- Acta Informatica: O’Neil et al., The Log-Structured Merge-Tree (LSM-Tree) (1996) doi.org
- RocksDB wiki: RocksDB Overview github.com
Related terms
- B-tree: A sorted, wide and shallow tree that stores keys in disk-sized pages; the default index in most relational databases.
- Bloom filter: A tiny bit array that answers “definitely not in the set” or “probably in the set”, used to skip expensive lookups.
- WAL (write-ahead log): A database’s log of every change, written before the data files, which makes crash recovery, replication and point-in-time restore possible.
- Skip list: A sorted linked list with randomly built express lanes, giving fast search and ordered iteration with simple code.
- Database index: An extra data structure a database keeps next to a table so it can find matching rows without reading the whole table.
