Sub topic 5
SSTables: immutable sorted files
This chapter assumes you know hash maps, sorted arrays, binary search, and the difference between sequential and random I/O. It also assumes you can read short Python programs.
What an SSTable is
A sorted string table (SSTable) is a file that holds key-value pairs in key order. It is written once and never changed. When the memtable fills up, the engine writes its contents out as a new SSTable. Later writes to the same key go into a newer SSTable, and the old file stays as it is until compaction removes it.
Two properties matter:
- Sorted: keys appear in order, so you can find a key with binary search and read a range of keys from one contiguous region.
- Immutable: the file is never edited in place. The engine needs no locks on it, readers never see a half-updated file, and writing it is one sequential pass.
A minimal SSTable
The class below keeps keys and values in two parallel lists. A real SSTable stores them in a file, but the lookup logic is the same.
import bisect
class SSTable:
"""Immutable sorted run of (key, value) pairs."""
def __init__(self, items):
self.keys = [k for k, _ in items]
self.values = [v for _, v in items]
def get(self, key):
i = bisect.bisect_left(self.keys, key)
if i < len(self.keys) and self.keys[i] == key:
return True, self.values[i]
return False, None
table = SSTable(sorted({"pear": 3, "apple": 1, "mango": 2}.items()))
print(table.get("mango")) # (True, 2)
print(table.get("kiwi")) # (False, None)get returns a pair. The first item says whether the key is in this table. The second is the value. The separate flag matters, because later chapters store a special marker value for deleted keys. The reader must be able to tell "this table has a marker for the key" from "this table has no entry for the key".
Why sorted and immutable
Sorting costs something at write time, but the memtable pays it once per flush. In return, each SSTable supports binary search, and several SSTables can be merged in a single linear pass. Compaction relies on that merge.
Immutability trades update-in-place for simplicity. To change a value, you write a newer entry elsewhere. This is why one key can exist in several SSTables at once, and why the read path has to check newer files before older ones.
Common errors
- Treating a missing key as a deleted key. A table that lacks a key says nothing about older tables. Only an explicit marker in a newer table hides an older value.
- Searching oldest first. The first match found must be the newest one, so search from newest to oldest.
- Assuming an SSTable can be patched. Any change means writing a new file.
Exercise
A key user:7 is stored in SSTable 2 with value "a" and in SSTable 5, which is newer, with value "b". Which value does a read return, and why does the engine not simply overwrite the entry in SSTable 2?
Show answer
The read returns "b", because the newest table containing the key is searched first. The engine does not overwrite the entry in SSTable 2 because SSTables are immutable. Editing a sorted file in place would need random writes and could leave readers with a half-written file. Compaction later merges the tables and discards the stale "a".