← Lessons

quiz vs the machine

Gold1450

Databases

LSM Trees

The write optimized structure behind many modern key value stores.

6 min read · core · beat Gold to climb

Why Not Just B-Trees

B-trees update data in place, which means random disk writes. A log structured merge tree (LSM tree) instead buffers writes in memory and flushes them as sorted files, turning random writes into fast sequential ones.

How It Works

  • New writes land in an in memory memtable, kept sorted.
  • When the memtable fills it flushes to an immutable sorted file called an SSTable.
  • A background compaction process merges SSTables and drops deleted keys.
  • Deletes are recorded as a marker called a tombstone.

Reads And Their Cost

  • A read may check the memtable then several SSTables, so reads can be slower.
  • A bloom filter per file quickly tells the engine which files to skip.
  • The cost of compaction and extra reads is the price for very fast writes.

Key idea

An LSM tree buffers writes in memory and flushes sorted immutable files, trading slower reads and background compaction for very high sequential write throughput.

Check yourself

Answer to earn rating on the learn ladder.

1. LSM trees make writes fast mainly by:

2. A bloom filter in an LSM tree helps by:

3. A delete in an LSM tree is recorded as: