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.