← Lessons

quiz vs the machine

Gold1350

Databases

The Memtable and Flush

The memtable is the in memory write buffer of an LSM engine, paired with a log so flushes can happen safely without losing data.

4 min read · core · beat Gold to climb

What a Memtable Is

A memtable is the in memory, sorted structure where an LSM engine collects fresh writes before they reach disk. It is usually a balanced tree or skip list so keys stay ordered for a clean flush.

  • Every write is inserted into the memtable.
  • Reads check the memtable first, since it holds the newest data.
  • Because memory is volatile, each write is also appended to a write ahead log on disk.

The Flush

When the memtable reaches a size threshold, the engine performs a flush.

  • The memtable is sealed and becomes read only.
  • Its sorted contents are written out as a new immutable SSTable.
  • A fresh empty memtable begins accepting writes.
  • Once the SSTable is durable, the matching log segment can be discarded.

Why the Log Matters

The write ahead log is what makes the in memory buffer safe. If the process crashes before a flush, the engine replays the log on restart to rebuild the lost memtable, so no committed write disappears.

Key idea

The memtable buffers sorted writes in memory and flushes them as an SSTable, while a write ahead log protects against crashes before the flush.

Check yourself

Answer to earn rating on the learn ladder.

1. Why does every write also go to a write ahead log?

2. What happens during a flush?