Pranit PatilMumbai IST
All notes

Databases · Updated 28 Nov 2026

How an LSM tree keeps writes fast

Write path, SSTables, reads and compaction, from building a Redis-compatible store in Go.

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

  1. SET user:1 Alice

    parsed from RESP

  2. Append to WAL

    wal.log: time|SET|key|value

  3. Insert in memtable

    sorted slice, binary search

  4. Full? Freeze it

    becomes the immutable memtable

  5. Flush to disk

    new sstable-N.db file

Fig 1 A SET in Small-Redis. The client gets its OK after step 2; flushing happens in the background.

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

Fig 2 The layout of one SSTable file in Small-Redis. Opening a file reads the footer first, then the index, so a lookup can seek straight to one entry.

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

Fig 3 A GET looks in the numbered order: memory first, then each SSTable from newest to oldest. The first version found is the answer.

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.

  1. 5 SSTables

    duplicates and tombstones

  2. Merge by key

    newest version wins

  3. Drop tombstones

    deleted keys disappear

  4. 1 SSTable

    old files deleted

Fig 4 Small-Redis compacts once there are five SSTables: merge them, keep the newest version of each key, drop tombstones, write one new file and delete the old ones.

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:

Next noteText to SQL with an agent loopHow DataBot turns a plain-English question into a safe query and a chart