skip to content

LSM trees and bloom filters

memtables, SSTables, compaction, and the filter that says definitely not. the write-optimized design postgres doesn't use.

address
everything.index_internals.lsm_trees
part
part 13, inside indexes
chapter
chapter 11 of 12
reading time
8 min

an LSM tree (log-structured merge tree) never updates a page in place. writes go to a sorted memtable in memory; when it's full it's written out, in one sequential write, as an immutable sorted file (an SSTable). background compaction merges those files into bigger ones, level by level. writes are cheap and sequential; a read may have to look in several files, which is what bloom filters are for: a few bits per key that say "definitely not in this file". postgres doesn't use one. RocksDB, LevelDB, Cassandra and many newer stores do.

the picture

you know how a b-tree finds a leaf and changes it in place, splitting when it fills? every insert is a random write to some page somewhere in the index. on spinning disks that was slow; on SSDs, rewriting 8 KB to change 20 bytes wears the flash. an LSM tree turns every write into an append.

think of a busy office's inbox. new papers go into a tray on the desk, sorted as they arrive (the memtable). when the tray is full, the whole stack is clipped together and dropped in a filing drawer, untouched from then on (an SSTable). every so often someone takes a few clipped stacks and merges them into one sorted folder, throwing out papers that newer ones replaced (compaction). to find something you check the tray, then the stacks newest first, then the folders.

step through one. four keys a file, a merge whenever L0 holds two files:

emptyan empty LSM tree: a memtable in memory, and levels of sorted, immutable files on disk.

this steptombstone
step 1/20
model of a leveled LSM tree: memtable of 4, compaction at 2 L0 files, 4 keys a file
an LSM tree, one operation at a time. a put goes to the memtable; a full memtable is flushed to L0 as a sorted, immutable file; two L0 files get compacted into L1. a delete is a tombstone that hides older values until compaction drops both. the gets at the end show the read path: newest first.

writes are sequential, and then written again

hover a stage.

drawn by hand · the textbook design, after O'Neil et al. 1996 and the RocksDB wiki
the write path of an LSM tree, left to right. hover a stage. everything after the memtable happens in the background, and every arrow after the flush rewrites data that's already on disk.

an LSM write is: append a record to a write-ahead log (for durability), insert into the memtable, done. no page is read, nothing on disk is modified. the memtable flush is one big sequential write of a sorted file.

the bill comes later, in compaction. every byte you write gets rewritten each time a compaction merges the file it's in. that ratio, bytes written to disk per byte written by the application, is write amplification. there are two families of compaction, and they put the cost in different places (the RocksDB wiki's terms):

leveledtiered (RocksDB calls it universal)
shapeeach level below L0 is one sorted run of non-overlapping files, each level about 10x the one aboveeach level holds several sorted runs, merged when there are enough of them
a key lives inat most one file per levelpossibly every run on a level
write amplificationhigher: a file is rewritten every time it's merged into the next levellower, the reason it exists
read amplificationlower: one file per level to checkhigher: more sorted runs to check
space amplificationlower: few stale copieshigher: old versions survive until runs merge

that's the triangle every storage engine sits in: read, write and space amplification. you can lower any two at the cost of the third. a b-tree updates in place (low space, low read), and pays for it with random writes. an LSM tree buys cheap writes with reads and background work.

reads, and the filter that says no

predict the output
a leveled LSM tree with a memtable, 4 L0 files and 3 more levels. you look up a key that doesn't exist. how many places must be checked?
text
get("no-such-key")

a bloom filter (Bloom, 1970) is a bit array and k hash functions. adding a key sets k bits. looking one up checks those k bits: if any is 0, the key was never added, definitely. if all are 1, it maybe was: other keys may have set those bits. so it has false positives, never false negatives, and the false positive rate depends only on bits per key and k.

keys in =
42, 7, 1999, 314, 8086, 64
hashes k =
look up =
15 of 48 bits set (31%). 55 checks bits 3, 32, 29: a 0 among them, so "definitely not"
bits/key =
10
best k = round(10 × ln 2) = 7. false positives = (1 − e−7/10)7 = 0.82%, about 1 lookup in 122 for a key that isn't there.
computed in your browser: the bits from the hashint4 port with double hashing, the rate from the formula
a 48-bit bloom filter holding six keys. pick k and a key to look up: the checked bits decide "maybe" or "definitely not". 170 was never added; try it. then slide bits per key to see the rate a real filter would get, computed from the formula.

the rate, for n keys in m bits with k hashes, is (1 − e−kn/m)k, best at k = (m/n) ln 2. postgres can do the arithmetic:

sqlempty db
SELECT bits_per_key, k,
       round((100 * (1 - exp(-k / bits_per_key)) ^ k)::numeric, 3) AS false_positive_pct
FROM (SELECT b::numeric AS bits_per_key, round(b * ln(2)) AS k
      FROM unnest(ARRAY[5, 10, 16, 20]) AS b) AS x
ORDER BY bits_per_key;
output, from postgres 18.6
SELECT 4
bits_per_keynumerickfloat8false_positive_pctnumeric
539.185
1070.819
16110.046
20140.007
(4 rows)

10 bits per key, 7 hashes: 0.82%. that's RocksDB's default ballpark: its wiki quotes about 1% at 9.9 bits per key, and 0.84% for a standard bloom filter at 10. one byte and a quarter per key, and 99 of every 100 lookups for a missing key skip the file. every extra 5 bits per key cuts the rate about tenfold.

why postgres doesn't

postgres was designed around update-in-place pages: a heap for rows and b-trees that point into it. MVCC keeps old row versions in the heap (an UPDATE is an INSERT), VACUUM cleans up, and the WAL makes in-place page changes durable. an LSM engine would replace most of that, and reads (the common case for most postgres workloads) would get more expensive. the write-heavy cases where LSM shines (ingesting huge streams of keys, write-bound SSDs) are where engines like RocksDB, and MySQL's MyRocks storage engine on top of it, are used instead.

postgres borrowed the good parts in places, though. GIN's pending list is an LSM idea: buffer writes, merge them in bulk. BRIN has a bloom operator class, and the bloom contrib extension builds a whole index out of per-row signatures.

one level deeperwhere the LSM tree comes from

O'Neil, Cheng, Gawlick and O'Neil described it in "The Log-Structured Merge-Tree (LSM-Tree)" (Acta Informatica, 1996): a small component in memory, larger components on disk, and rolling merges between them, for workloads with far more inserts than lookups (their example was a history table of bank transactions). Google's Bigtable paper (2006) gave the in-memory part the name memtable and the on-disk files the name SSTable, and LevelDB, then RocksDB, made the leveled design the common one.

keylines

  • an LSM tree writes to a sorted memtable, then flushes immutable sorted files (SSTables): every write is sequential.
  • compaction merges files level by level; rewriting data again and again is write amplification.
  • leveled compaction favours reads and space, tiered favours writes. read, write, space: pick two.
  • a missing key has to be checked everywhere, so each file keeps a bloom filter: 10 bits a key, about 1% false positives.
  • postgres updates pages in place; its GIN pending list and BRIN bloom borrow LSM ideas.