Write fast to memory, flush to sorted files, and merge them later.
An LSM tree (Log-Structured Merge tree) is a way to store data that makes writes very fast. It never edits data in place. It collects writes in memory, saves them to disk as sorted files, and merges those files in the background.
A B-tree updates pages in place, which means many small random writes to disk. Disks (and SSDs) are much happier with big, sequential writes. An LSM tree is built around that idea: turn lots of small random writes into a few big sequential ones.
| Part | What it is | Job |
|---|---|---|
| Write-ahead log (WAL) | An append-only file on disk | If the machine crashes, replay it to rebuild the memtable |
| Memtable | A sorted structure in memory (skip list or tree) | Absorbs writes and serves the newest reads |
| SSTable | An immutable file of sorted key-value pairs, with an index and a bloom filter | Durable storage, easy to binary search |
| Bloom filter | A tiny bit array that can say “definitely not here” or “maybe here” | Skip files that cannot hold the key |
| Compaction | A background job that merges SSTables | Drops old versions and tombstones, keeps reads fast |
| Tombstone | A marker that says the key was deleted | Hides older versions until compaction removes them |
A teaching model: writes follow a fixed, repeatable key order (keys repeat after 23 writes, which are updates), the memtable flushes by key count, and compaction merges all of level 0 into one level 1 file. Bloom filters are simulated with a fixed rule that never misses a stored key but gives a few false alarms. Real engines such as RocksDB and Cassandra use many levels or size tiers, and write-ahead logs for safety.
| Term | Meaning in easy words |
|---|---|
| LSM tree | Log-Structured Merge tree: memory plus immutable sorted files merged in the background |
| Memtable | The sorted in-memory table that takes new writes |
| SSTable | Sorted String Table: an immutable sorted file on disk |
| Write-ahead log (WAL) | An append-only file used to rebuild the memtable after a crash |
| Flush | Writing a full memtable out as a new SSTable |
| Compaction | Merging SSTables to drop old versions and keep reads fast |
| Tombstone | A marker that records a delete |
| Bloom filter | A compact structure that says “definitely not present” or “maybe present” |
| Write amplification | Bytes written to disk per byte the user wrote |
| Read amplification | How many files or pages are read for one lookup |
| Choice | We gain | We lose |
|---|---|---|
| Bigger memtable | Fewer flushes and files | More memory, longer crash recovery |
| Size-tiered compaction | Cheap writes | More files per read, more space |
| Leveled compaction | Fast reads, less space | More write amplification |
| Bloom filters | Fewer wasted reads | A little memory, and a small false positive rate |
| Lazy compaction | More write capacity now | Slower reads and a bigger backlog later |
| LSM tree (vs B-tree) | Write speed | Read simplicity and steady latency |