Sub topic 3
The memtable: absorbing writes in memory
This chapter assumes you can read short Python programs, know how hash maps and sorted arrays work, and understand the difference between memory and disk I/O. It covers the first stop for every write in an LSM tree: the memtable.
What the memtable is
The memtable is an in-memory table that holds the most recent writes. A put or delete never touches a data file directly. The engine updates the memtable, and that update costs a memory write and nothing more.
This is how an LSM tree turns many small random writes into a few large sequential ones. Writes pile up in memory until the table is full. The engine then writes the whole table to disk in one pass, in sorted order. That step is called a flush, and a later chapter covers it.
Behavior that matters
The memtable has three properties that the rest of the design depends on.
- It keeps only the latest value for each key. If you write
user:1three times before a flush, the memtable holds one entry, and the two older values never reach disk. - Its contents are readable. A lookup must check the memtable first, because it holds the newest data.
- Its contents are lost if the process crashes. Memory is volatile, so a real engine pairs the memtable with a write-ahead log. The next chapter covers that log.
Hash map or sorted structure
The example store in this tutorial uses a Python dict for the memtable and sorts the keys only at flush time. This is the simplest choice. A dict gives fast reads and writes, and one sorted() call at flush produces the order that an SSTable needs.
Production engines usually keep the memtable sorted at all times. LevelDB and RocksDB use a skip list by default. A sorted memtable makes the flush a straight scan, and it lets range queries read the memtable in key order without extra work.
| Structure | Write and point read | Sorted output | Typical use |
|---|---|---|---|
| Hash map (`dict`) | Fast | Sort needed at flush | Teaching examples |
| Skip list | Slightly slower | Already sorted | LevelDB, RocksDB |
Size limits and the flush trigger
The memtable cannot grow without bound. The engine sets a limit and flushes when the table reaches it. Real engines measure the limit in bytes, for example 64 MB. The example below counts entries, which is easier to follow.
class Memtable:
def __init__(self, limit=4):
self.data = {}
self.limit = limit
def put(self, key, value):
self.data[key] = value # an overwrite replaces the old value
def get(self, key):
return self.data.get(key)
def is_full(self):
return len(self.data) >= self.limit
m = Memtable(limit=3)
m.put("a", 1)
m.put("b", 2)
m.put("a", 10) # overwrite: still two entries
print(len(m.data), m.get("a"), m.is_full()) # 2 10 False
m.put("c", 3)
print(m.is_full()) # TrueThe overwrite of "a" leaves the entry count at two. The table becomes full only when a third distinct key arrives. A full memtable is then frozen and flushed, and a fresh empty memtable takes over so that writes can continue.
Common mistakes
- Treating the memtable as durable. Without a log, a crash loses every write since the last flush.
- Setting the limit too small. The engine then creates many small files, and later compaction work grows.
- Setting the limit too large. A crash then forces a longer recovery, and memory use rises.
Exercise
You call put("x", 1), put("y", 2), put("x", 3) on an empty memtable with a limit of 3 entries. Is the memtable full, and what does get("x") return?
Show answer
It is not full, because it holds only two distinct keys (x and y) and the limit is 3. get("x") returns 3, since the overwrite replaced the earlier value.