BLOOM FILTER

Probably yes, definitely no: set membership in a few bits per key

L215 MINALGORITHMS
PREDICT

A Bloom filter has m = 256 bits. Each key sets k = 4 of them, and a query says yes if all 4 of its bits are set. It is sized for 32 keys, giving about 2.4% false positives. You insert 96 keys instead. What false positive rate do you measure?

A Bloom filter is an array of m bits and k hash functions. To insert a key you set the k bits it hashes to. To query, you check those k bits: if any is 0 the key was never inserted. If all are 1 it probably was, because other keys may have set them.

The false positive rate is about (1 − e^(−kn/m))^k: the fraction of bits set, to the power k. It depends on n, the number of keys you actually inserted. Size m and k for the n you expect, and the rate holds. Insert more, and every query pays.

▶ SIMULATION
BIT ARRAY (m = 256, k = 4)
········ ········ ········ ········ ········ ········ ········ ········
········ ········ ········ ········ ········ ········ ········ ········
········ ········ ········ ········ ········ ········ ········ ········
········ ········ ········ ········ ········ ········ ········ ········
nothing yet
FILTER
keys inserted: 0 (designed for 32)
bits set: 0 / 256 = 0.0%
bits per key: —
best k for this m, n: —
QUERIES
members checked 0 · false negatives 0
non-members probed 0 · said yes 0
measured FP —
formula (1 − e^(−kn/m))^k(1 − e^(−4·0/256))^4 = 0.0%
fill^k (from the real bits)0.0%^4 = 0.0%
✓ no false negatives: every inserted key answers yes✓ measured false positive rate ≤ 5% target
SCHEDULE
01[INSERT]insert user:0 … user:31 (32 keys)
02[INSERT]insert user:32 … user:63 (32 keys)
03[INSERT]insert user:64 … user:95 (32 keys)
04[PROBE]query every inserted key (looking for false negatives)
05[PROBE]query 500 keys never inserted (guest:0 … guest:499)
06[PROBE]query 500 keys never inserted (guest:500 … guest:999)
07[REPORT]compare measured FP rate with the 5% target
Start of overfilled (n = 96) scenario. 7 steps.
BLOOM FILTER  (m bits, k hash positions per key)

INSERT(x):  for i in 0..k-1:  B[hash(x, seed_i) mod m] = 1
QUERY(x):   if any of those k bits is 0 → DEFINITELY NOT
            else                         → PROBABLY YES

After n inserts:
  fraction of bits set   ≈ 1 − e^(−kn/m)
  false positive rate p  ≈ (1 − e^(−kn/m))^k

Sizing for n keys at target p:
  m = ⌈ −n · ln p / (ln 2)² ⌉      (≈ 9.6 bits per key for p = 1%)
  k = round( (m / n) · ln 2 )      (≈ 7 for p = 1%)

No false negatives, ever. Bits are never cleared, so you cannot
delete (use a counting Bloom filter), and inserting past n
raises p for every query.

Watch the bit array and the invariant strip. OVERFILLED puts 96 keys into a filter sized for 32: three quarters of the bits are set and about 30% of the 1000 non-members answer yes. RIGHT-SIZED uses the same bits, the same hashes and the same probes, and stays under 5%. Neither scenario ever produces a false negative. Drag BITS and HASHES and compare the measured rate with the formula.

TUNE IT NOT YET

The filter now has to hold 96 keys at a 1% false positive rate, and you have at most 1024 bits. Set BITS (m) and HASHES (k) so the measured FP rate over 1000 non-member probes is at or below 1%. Too few hashes leave the filter under-used; too many fill it up.

GOAL: measured FP ≤ 1% with 96 keys

FIX IT NOT YET

Write optimalParams(n, p): the number of bits m and hashes k for n keys at false positive rate p. The grader checks that (1 − e^(−kn/m))^k stays within 5% of p and that m is no more than 10% over the minimum.

Mental model:

a Bloom filter answers “no” with certainty and “yes” with a probability you choose in advance. That probability is set by bits per key, and it only holds for the n you designed for. Pick k = (m/n)·ln 2, and when the key count outgrows the design, rebuild: bits never come back to 0.

IN PRODUCTION

LSM-tree storage engines (RocksDB, Cassandra, HBase) keep one filter per SSTable to skip disk reads for absent keys; Redis has a Bloom filter module; databases use them in hash joins and for filtering rows across the network before a join. The symptom of a filter that outgrew the n it was sized for is skipped work creeping back as data grows.

IN INTERVIEWS

“Design a check for whether a username is taken, at a billion users, in memory.” Explain insert and query, say there are no false negatives, give the formula and the 9.6-bits-per-key rule of thumb for 1%, and say what happens when n grows past the design (rebuild, or a scalable Bloom filter that adds layers).

RECALL CARDS Tap to flip. They join your review deck when you mark the lesson complete.

USED IN

Chapters that build on this lesson.

NEXT: HYPERLOGLOG →