Notesstorage🌱 seedling

Memtable

Also: Memory Table · Write Buffer

The in-memory, sorted buffer at the top of an [[LSM-Tree]] where every write lands first. When it fills, it is frozen and flushed to disk as one sorted, immutable [[SSTable]].

Why it exists

Writes arrive in random key order, but an LSM-tree only ever writes sorted files sequentially. The memtable absorbs random inserts in memory, where sorting is cheap, and turns them into one large sequential write.

How it works

  • Each write is appended to the Write-Ahead Log for durability, then inserted into the memtable: usually a skip list, sometimes a balanced tree.
  • Reads check the memtable first, because it holds the newest version of any recent key.
  • At a size limit (RocksDB's default is 64 MB), the memtable becomes immutable and a fresh one takes new writes while the old one is flushed in the background.
  • Once the flush completes, its log records are no longer needed; a WAL file is deleted once everything in it is flushed.
  • Deletes are inserted as tombstones, like any other write.

Trade-offs

Bigger memtable Smaller memtable
fewer, larger flushes; fewer L0 files less memory, faster recovery
more updates to the same key coalesce in RAM more flushes and more compaction work
longer WAL replay after a crash less coalescing of hot keys

In practice

  • RocksDB lets several immutable memtables queue for flush; if they pile up faster than flushes finish, it stalls writes.
  • Cassandra flushes memtables per table, by size or on a commit-log threshold.

Pitfalls

  • Memtable memory is per column family or table. Many tables with generous memtables can quietly exhaust RAM.
  • A crash loses the memtable, so durability depends entirely on the WAL settings, not on the memtable.

Recall

These cards are also in your review deck, scheduled with spaced repetition.