Bloom Filters
Deriving Bloom filters from first principles: trading exact storage for a compact bit array that answers 'definitely absent' with zero false negatives, why deletion breaks, and how LSM engines skip disk reads.
Suppose your storage engine holds 1 billion keys (average 32 bytes each) on disk, and 90% of incoming lookups ask for keys that don’t exist in a given file.
Keeping an exact HashSet of all 1 billion keys in RAM takes 32+ GB (plus
pointer and hash-table overhead — closer to 64 GB). Yet you don’t actually need
to retrieve the key from RAM; you only need a fast gatekeeper that says
“skip disk — this key is definitely not here.”
From a HashSet to an m-bit array
Drop the keys themselves and keep only an m-bit array initialized to 0,
plus k independent hash functions:
- Insert(
x): computeh₁(x), h₂(x), ..., hₖ(x) mod mand set thosekbits to1. - Query(
x): check the samekbit positions.- If any bit is
0:xwas never inserted — 0% false negatives. - If all
kbits are1:xis probably present — or other keys happened to set those exactkbits, causing a false positive.
- If any bit is
The interactive 32-bit filter below starts with 5 words inserted (redis,
kafka, raft, wal, sstable) and queries "mysql". Notice how "mysql"
was never inserted, yet its 3 hash positions (11, 26, 9) were already set
to 1 by redis and sstable — a real false positive. Click ? apple or
? mongo to see a guaranteed definitely absent hit.
interactive · toggle words to set 3 bits, then test queries for false positives
query "mysql" → FALSE POSITIVE (bits 11, 26, 9 all 1)never inserted · collided with redis + sstable
Sizing the filter (bits vs false positives)
If a filter is too small (m too low), bits saturate toward 1 and false
positives explode. For n inserted items, m bits, and k hashes, the
false-positive probability is approximately:
p ≈ (1 − e^(−k·n / m))^kOptimal hash count is k ≈ (m / n) × ln(2) ≈ 0.693 × (m / n). Two rules of
thumb cover almost every system design interview:
~10 bits / key
1% false-positive rate (k = 7)
~14.4 bits / key
0.1% false-positive rate (k = 10)
1.2 GB
RAM for 1 billion keys at 1% FP
That is 1.2 GB instead of 64 GB — small enough to stay pinned in memory on a single node while eliminating 99% of wasted disk I/O on missing keys.
Why you cannot delete from a standard Bloom filter
Suppose "redis" and "sstable" both set bit 9 to 1. If you try to delete
"redis" by flipping bits 9, 10, 11 back to 0, you silently break
"sstable" — turning future queries for "sstable" into false negatives,
violating the Bloom filter’s one ironclad guarantee.
Production systems handle deletion in three ways:
- Rebuild on compaction: LSM-Tree engines (RocksDB, Cassandra) treat each SSTable as immutable, so its Bloom filter never needs deletes — a fresh filter is built when compaction merges files.
- Counting Bloom Filter: replace each 1-bit slot with a 4-bit counter (increment on insert, decrement on delete). Supports deletion at 4× RAM.
- Cuckoo Filter: stores compact fingerprints in a cuckoo hash table. Supports deletion natively and often uses less space than a Bloom filter when target false-positive rate is <3%.
note
Standard Bloom filters only answer exact key membership (k = "user:42").
They cannot accelerate range scans (WHERE id BETWEEN 100 AND 200), which is
why RocksDB also supports prefix Bloom filters on a fixed key prefix.
Probabilistic data structures cheat sheet
| Structure | Question answered | Error mode | Typical size |
|---|---|---|---|
| Bloom Filter | “Is x in the set?” |
False positives only (no deletes) | ~10 bits / key (1% FP) |
| Cuckoo Filter | “Is x in the set?” (with deletes) |
False positives only | ~12 bits / key (1% FP) |
| HyperLogLog | “How many unique items?” | ±0.8% cardinality error | ~12 KB for billions |
| Count-Min Sketch | “How frequent is x?” |
Overestimates counts only | Fixed w × d counters |
Where this shows up in system design
- RocksDB / Cassandra / LevelDBEach immutable SSTable carries an in-memory Bloom filter so point reads for missing keys skip disk seeks instead of checking every LSM level.
- Chrome Safe BrowsingShips a compact probabilistic filter of malicious URL prefixes to every browser so safe URLs are cleared locally without phoning Google on every click.
- CDN One-Hit-Wonder Caching (Akamai)A Bloom filter tracks URLs seen at least once and refuses to write a URL to SSD cache until its second request, saving ~50% of SSD wear.
- Medium / Feed Seen-ArticlesPer-user Bloom filters check whether a candidate recommendation was already read without querying a massive user-article join table.
Check yourself: system design scenarios
interactive · pick an architecture for each prompt to test your intuition
1. Cutting wasted disk reads on an LSM-Tree key-value store
point lookups · immutable files · 80% misses
Your RocksDB-backed service has 5 LSM levels of immutable SSTables on NVMe. 80% of point lookups (`GET key`) are for keys that do not exist, causing 5 wasted disk reads per miss.
Select an option to check the trade-off.
2. Dynamic allowlist with frequent additions AND removals
50M keys · frequent deletes · compact RAM
Your edge proxy maintains 50 million active session tokens in RAM to reject forged cookies before calling Auth. Users log out constantly, so tokens must be removed within seconds.
Select an option to check the trade-off.
3. Protecting a CDN SSD cache from one-hit wonders
write amplification · one-hit wonders · tiny RAM budget
70% of URLs requested through your CDN edge are requested once and never again. Writing every first miss to SSD destroys flash endurance and evicts genuinely hot assets.
Select an option to check the trade-off.
false positiveprobability
Reporting that an item is present when it was never inserted; Bloom filters allow false positives but never false negatives.
compactionstorage
Background merging of immutable SSTables that drops overwritten values and tombstones to bound read amplification and disk size.
SSTablestorage
Sorted String Table: an immutable, key-sorted file flushed to disk from an LSM-Tree MemTable and later merged by compaction.