Bloom Filters

The problem

Two reads are slow in an LSM engine: a key last written long ago (buried in an old SSTable), and a key that does not exist at all. In both cases the engine must consult multiple files, each costing a sparse-index lookup and a disk read.

What it is

A compact in-memory bit array stored alongside each SSTable, answering one probabilistic question: does this key appear in this SSTable?

Says "No"definitively — absent means absent
Says "Probably yes"usually right, occasionally a false positive

How it works

Construction, at flush time: for each key in the SSTable, hash it to a set of integers, treat each as a bit index, set those bits to 1. All other bits stay 0.

A 16-bit Bloom filter built from two keys. handbag hashes to bits 2, 9 and 4; handsome hashes to bits 0, 7 and 13. Each key sets its three bits to 1. The resulting array has bits 0, 2, 4, 7, 9 and 13 set, which reads 1010100101000100 from bit 0 to bit 15, and is written into the SSTable as its filter block.
Each key sets its bits; the filter is the OR of them all

The bit array is written into the SSTable file on disk as a filter block, next to the index block holding the sparse index. When the SSTable is opened, both are loaded into RAM — so every filter check is an in-memory bit test with no disk I/O.

Query, at read time: hash the query key the same way and inspect those bits.

Querying the 16-bit Bloom filter built from handbag and handsome, which has bits 0, 2, 4, 7, 9 and 13 set. The query key handheld hashes to bits 6, 11 and 2. Bit 2 is 1, bit 6 is 0, bit 11 is 0. At least one probed bit is 0, so the key is definitely not in this SSTable and the file is skipped with no index lookup and no block read. Only if all three bits were 1 would the key be probably present.
One zero among the probed bits proves the key is absent
ResultMeaningAction
At least one bit is 0Key is definitely absentSkip this SSTable entirely
All bits are 1Key is probably presentConsult the sparse index and decode the block

Why "probably"? Multiple keys can set the same bit. If "handbag" and "handle" both hash to index 9, bit 9 is 1 regardless of which you're searching for. A query for "handpick" might find all its bits set — not because it's stored, but because other keys happened to set those bits. That is a false positive.

False positives and filter sizing

A false positive costs one unnecessary disk read and no correctness issue — the engine simply continues to the next segment. The filter size must be chosen from the expected key count; once the SSTable is written, the filter is immutable and fixed.

Keys in SSTableFilter sizeBits per keyFalse-positive rate
10,00020 KB16~0.05%
10,00012.5 KB10~1%
10,0006.25 KB5~10%
100,00012.5 KB1~60% — a filter that outgrew its estimate is nearly useless

10 bits per key ≈ 1% false-positive rate

each additional ~5 bits/key drops the rate by roughly 10×

Worked: 10,000 keys × 10 bits = 100,000 bits = 12.5 KB filter. 100,000 keys × 10 bits = 1,000,000 bits = 125 KB filter.

Why the filter doesn't "max out": as more keys are added the array fills and false positives rise — but the size is chosen upfront from an estimated key count, and once written it never grows. If the actual key count exceeds the estimate, the FP rate rises and queries get slower, but they still return correct answers. Compaction then creates new SSTables with new filters sized to their actual key counts.

Why this matters

Without a filter, a read for a non-existent key costs a sparse-index lookup plus a disk read on every SSTable before the engine can conclude absence. With one: check the filter (nanoseconds, RAM) → if any bit is 0, skip the file entirely → only if all bits are 1 do you pay for index + block. For read-heavy workloads with many misses (caches, analytics engines) this eliminates the majority of disk I/O. The Numbers on disk part puts real numbers on it: 300 ms → a few ms for a 100-file miss on spinning disk, and the residue is the filter's own ~1% false positives.

The Invariants

  • Guarantees: Zero false negatives — if the filter says "absent," the key is absent, so skipping the file is always safe.
  • Does not guarantee: That "present" means present, or that the FP rate stays at its design point — a filter sized for fewer keys than it received silently degrades toward useless without ever returning a wrong answer.