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.
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.
| Result | Meaning | Action |
|---|---|---|
| At least one bit is 0 | Key is definitely absent | Skip this SSTable entirely |
| All bits are 1 | Key is probably present | Consult 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 SSTable | Filter size | Bits per key | False-positive rate |
|---|---|---|---|
| 10,000 | 20 KB | 16 | ~0.05% |
| 10,000 | 12.5 KB | 10 | ~1% |
| 10,000 | 6.25 KB | 5 | ~10% |
| 100,000 | 12.5 KB | 1 | ~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.