Every database faces the same tension: disks are fastest when they write one long stream but programs want to update individual records scattered across the whole file. A B-tree handles that with in-place updates — but each update can jump to a random location on disk, and random writes are orders of magnitude slower than sequential ones.
Log-Structured Merge Trees (LSM trees), invented by Patrick O'Neil and colleagues in 1996, flip the strategy. Instead of updating data where it lives, every write is appended to an in-memory buffer called the memtable. When the memtable fills up it is sorted and flushed to disk as a compact, immutable file called an SSTable (Sorted String Table). The on-disk files accumulate across levels, and a background process called compaction periodically merges them, discarding obsolete versions and reclaiming space.
The pay-off is dramatic: what was a random write is now a sequential append. The cost is that reads may have to check multiple levels — and that the same bytes may be rewritten several times as they migrate down through the levels, a phenomenon called write amplification.
Modern storage engines — RocksDB, Apache Cassandra, HBase, LevelDB — are all built on LSM trees, making this one of the most consequential data structures of the last three decades.
Comments
Loading comments...