LESSON 02 · LSM trees

Buffer in memory, merge on disk

An LSM tree keeps data sorted without editing files in place, then pays for that with background merging.

SYSTEM VIEW

The write path

Diagram view
Sorted files—
Files read—

Follow two parcel writes into a log and a sorted memtable, then watch the files they become answer reads and merge.

  1. An LSM tree never edits a file: inserting into the middle of a sorted one is expensive. File 1, flushed earlier, holds 1203 new and 4410 new. The App writes parcel 8841 as sent, first to the end of the write-ahead log at offset 0, so a crash can’t lose it, then to the memtable, sorted in memory.

    Record
    8841 sent
    WAL offset
    0

    What changed

    • 8841 sent at WAL offset 0
    • 8841 sent in the memtable
  2. Parcel 1203 is sent next. The WAL appends it at offset 1, after 8841, in arrival order. The memtable files it by key, ahead of 8841, so it can be written out already sorted. File 1’s older 1203 new isn’t touched.

    WAL
    8841, 1203
    Memtable
    1203, 8841

    What changed

    • 1203 sent at WAL offset 1
    • Memtable order: 1203, 8841
  3. At a set size, 64 MB here, the engine writes the memtable out as File 2, a sorted file it never edits, and starts an empty memtable. File 2 now holds what WAL offsets 0 and 1 covered, so that part of the log is discarded.

    Flush at
    64 MB
    File 2
    1203, 8841

    What changed

    • File 2: 1203 sent, 8841 sent
    • Memtable empty again
    • WAL offsets 0 and 1
  4. A read of 1203 checks the memtable, now empty, then the files from newest to oldest. File 2 holds 1203 sent, so the read stops there: File 1’s older 1203 new is never read.

    Found in
    File 2
    Not read
    File 1

    What changed

    • 1203 sent found in File 2
    • File 1 not read
  5. Deleting 4410 edits no file: a tombstone goes into the WAL at offset 2 and into the memtable. A read of 4410 meets the tombstone first and answers not found. File 1 still holds 4410 new, now hidden.

    WAL offset
    2
    4410 reads
    not found

    What changed

    • Tombstone for 4410 at WAL offset 2
    • 4410 reads as not found
    • File 1’s 4410 new hidden
  6. A lookup of 7702, never written, would read every file. Each file’s Bloom filter answers definitely absent or possibly present; here both say absent, so no file is read. At 10 bits per key a filter gives a false positive about 1% of the time: with 20 files, about 0.2 needless reads instead of 20.

    Bits per key
    10
    False positives
    about 1%
    With 20 files
    0.2 reads, not 20

    What changed

    • A Bloom filter per file
    • 7702 ruled out, no file read
  7. The memtable flushes again as File 3, holding the tombstone. Compaction merges Files 1 to 3 into one, keeping each key’s newest record. 1203 new is overwritten, so it goes; the tombstone hides 4410 new, and with no older file beneath them, both go.

    Kept
    1203 sent, 8841 sent
    Overwritten
    1203 new
    Deleted
    4410 new + tombstone

    What changed

    • File 3: the 4410 tombstone
    • Merged: 1203 sent, 8841 sent
    • Files 1 to 3
    • WAL offset 2
  8. Compaction costs disk bandwidth, CPU and free space while it merges. When writes outrun it, flushed files pile up and reads slow, since each has more files to check; engines such as RocksDB then throttle incoming writes until compaction catches up.

    Not merged yet
    Files 4 and 5
    Writes
    throttled

    What changed

    • File 4 and File 5, not merged yet
    • WAL offsets 3 and 4 written, then discarded
    • Reads check more files
    • Writes throttled

Space plays · ← → step · Home resets

THE IDEA

How it works

A hash index needs every key in memory and cannot answer range queries. Keeping data sorted on disk fixes both, since a small sparse index is then enough, but inserting into the middle of a sorted file is expensive. A log-structured merge tree (LSM tree) avoids this by never editing files. Each write goes to a write-ahead log and then to the memtable, a sorted in-memory structure. When the memtable reaches a set size, say 64 MB, the engine writes it out as an immutable sorted file and can discard the log that covered it.

A read checks the memtable, then files from newest to oldest, and the first match wins. A delete writes a tombstone, a marker that hides older versions. Looking up a key that does not exist would touch every file, so each file carries a Bloom filter: a compact bit array that answers “definitely absent” or “possibly present”. At 10 bits per key the false-positive rate is about 1%, so with 20 files a missing key costs about 0.2 needless file reads instead of 20.

Compaction is the background job that merges sorted files, keeps the newest version of each key, and drops overwritten values and tombstones with nothing older beneath them. It is also the main cost: it uses disk bandwidth and CPU, and needs free space while merging. If writes outrun it, files accumulate, reads slow down, and engines such as RocksDB throttle incoming writes. RocksDB, LevelDB, Cassandra, and HBase all use this design. It suits write-heavy workloads, though scans must merge several files.

DECISION POINT

Your metrics service runs on an LSM engine. After a week of heavier traffic, write latency is flat, but dashboard reads are twice as slow and the number of on-disk files has doubled. One teammate wants a bigger memtable; another wants to give compaction more disk bandwidth. Which do you try first?

Write a sentence or two to unlock the guidance. Committing first is what makes it stick.

Keep in mind

  • An LSM tree turns scattered writes into sequential file writes, then pays later in compaction.
  • A Bloom filter can rule a file out; a “maybe” still needs a read.
  • Compaction that falls behind shows up as slower reads and more disk use.
CHECK YOUR UNDERSTANDING

Put it into practice.

1 OF 3
DiagnoseQUESTION 01

On an LSM engine without Bloom filters, looking up keys that do not exist is far slower than looking up recent ones. Why?

Choose an answer. You’ll see the explanation once you get it right.

Next lesson

0 of 3 checkpoints answered correctly to complete this lesson.