Log-Structured Merge Tree (LSM) Simulator
Deep Dive
Log-Structured Merge Trees (LSM) trade read amplification and background compaction I/O to achieve unrivaled write throughput by turning random mutations into sequential disk writes.
Anatomy of an LSM Tree
An LSM-based storage engine consists of three core layers:
- MemTable (RAM):
A concurrent in-memory sorted index (typically a SkipList or Red-Black Tree). All incoming
PUTandDELETE(tombstone) requests enter the MemTable immediately. - Write-Ahead Log (WAL): An append-only sequential file on disk. Before updating the MemTable, writes are appended to the WAL to provide durability across system crashes.
- Sorted String Tables (SSTables on Disk): Immutable files divided into sorted key-value blocks. When a MemTable reaches capacity (e.g. 64MB), it is frozen and flushed as a Level 0 SSTable.
- Bloom Filters: A compact probabilistic bit array prepended to each SSTable. It guarantees zero false negatives: if the Bloom filter reports a key is absent, the engine safely skips reading that SSTable from disk!