I built Small-Redis to understand how a storage engine stays fast on writes and still survives a crash. It speaks Redis's wire protocol (RESP), so redis-cli works against it, and it stores data in a log-structured merge tree, the design behind LevelDB, RocksDB and Cassandra. These are my notes on how the pieces fit.
Why not just update a file in place?
Overwriting a value means finding where it lives on disk and rewriting that spot. Lots of small writes scattered across a file are slow, and a crash halfway through one can leave the file broken. An LSM tree turns every write into an append, which disks handle well, and pays for it later with a background merge.
The write path
SET user:1 Alice
parsed from RESP
Append to WAL
wal.log: time|SET|key|value
Insert in memtable
sorted slice, binary search
Full? Freeze it
becomes the immutable memtable
Flush to disk
new sstable-N.db file
- The WAL comes first. Each write is a line in
wal.logbefore it touches memory, so a crash loses nothing that was acknowledged. On startup the log is replayed. - The memtable is just sorted. In Small-Redis it is a sorted slice with binary search. Production engines usually use a skip list, but the job is the same: keep keys in order so the flush can write them out in one pass.
- Rotation keeps writes moving. A full memtable is frozen and a fresh one takes new writes, while a goroutine writes the frozen one to disk.
What is inside an SSTable
An SSTable (sorted string table) is never modified after it is written. That is what makes it safe to read without locks and easy to merge.
Data
entry · entry · entry … sorted by key
Index
key → byte offset of its entry
Footer
index offset
8 B
entry count
4 B
version
4 B
magic number
4 B
The read path
Because the same key can exist in several places, a read has to look from newest to oldest and stop at the first match.
Memory
Memtable
1 · checked first
Immutable memtable
2 · being flushed
Disk
sstable-3
3 · newest
sstable-2
4
sstable-1
5
sstable-0
6 · oldest
Deletes are writes too. A DEL stores a tombstone, a marker that says the key is gone. For that to work, finding a tombstone has to end the search, or an older value further down would come back to life.
Compaction
Every flush adds a file, and every extra file is one more place a read might have to look. Compaction merges them back down.
5 SSTables
duplicates and tombstones
Merge by key
newest version wins
Drop tombstones
deleted keys disappear
1 SSTable
old files deleted
The trade-off in one line
An LSM tree buys cheap writes with more expensive reads (several places to check) and extra background work (compaction rewrites data that was already on disk). Real engines add Bloom filters, to skip files that cannot hold a key, and levelled compaction, to bound how many files a read touches.
What I would fix next
I keep an issues log in the repo from reviewing my own code. The top items:
- The read path skips the store's lock, so a GET can race with a memtable rotation.
- WAL recovery calls SET and DEL without holding the lock.
- Rotation waits a fixed 100 ms if the previous flush is still running. A flush queue would be the proper fix.