Chapter 16 · Randomized, Amortized, Streaming

Bloom Filter

A few bits per item answer "have I seen this?" with no false negatives and a small, predictable rate of false positives (FP). Insert, query, catch a false positive in the act, then measure the rate against the formula.

Inserted (click to query):
bit = 1 bit = 0 bit set just now / false positive probed, already 1 probed, 0 → definitely not true "maybe" (really inserted)
Query history
No queries yet.

Measured false-positive rate vs the formula

For the current m, k and hash family: insert n random items (n = 0 … 1.5m), and after each step ask 2,000 items that were never inserted. Dots = measured fraction of false "maybe"s; line = (1 − e−kn/m)k.

Which k is best? (formula, at the current n)

How to read this

Try this

  1. Press Lesson example (m = 10, k = 2, toy hashes). 12 → bits 2, 7; 37 → bits 7, 2; 41 → bits 1, 4. Query 45 (bit 5 is 0: definitely not), 14 (bit 3 is 0), then 22: bits 2 and 7 are both on, so the filter says "maybe", a false positive.
  2. Insert a few items and query each one again: every inserted item always gets "maybe". There are no false negatives.
  3. Switch to double hashing, m = 64, k = 3, and press Insert 5 random twice (n = 10). The formula predicts about 5.2%. Press Measure: the dots should hug the line.
  4. Keep n = 10, m = 64 and press Set k = (m/n) ln 2 (6.4 × 0.693 ≈ 4.4 → k = 4, about 4.7%). In the k chart, k = 1 gives about 14% and k = 8 about 6.7%: too few hashes check too little, too many fill the array.
  5. Keep inserting past n = m. The rate climbs toward 100%: a Bloom filter must be sized for the number of items you expect (about 9.6 bits per item for a 1% rate).

Go deeper