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.
writes are sequential, and then written again
hover a stage.
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):
| leveled | tiered (RocksDB calls it universal) | |
|---|---|---|
| shape | each level below L0 is one sorted run of non-overlapping files, each level about 10x the one above | each level holds several sorted runs, merged when there are enough of them |
| a key lives in | at most one file per level | possibly every run on a level |
| write amplification | higher: a file is rewritten every time it's merged into the next level | lower, the reason it exists |
| read amplification | lower: one file per level to check | higher: more sorted runs to check |
| space amplification | lower: few stale copies | higher: 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
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.
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:
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;| bits_per_keynumeric | kfloat8 | false_positive_pctnumeric |
|---|---|---|
| 5 | 3 | 9.185 |
| 10 | 7 | 0.819 |
| 16 | 11 | 0.046 |
| 20 | 14 | 0.007 |
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.