ENع
←Bloom filters
Lesson S2 · Special topics

Bloom filters

14 min9 steps5 questionsIntermediate
About this lesson

A Bloom filter answers "have I seen this before?" using a few bits per item and never stores the items themselves. The price: it can say "maybe" to something it never saw. Watch a 12-bit filter with 3 hash functions add cat and dog, then answer for cat, fox and dove, and see why it cannot delete. It builds on the Software lesson on hash maps and the lesson "Chaining vs probing: expected cost and clustering", which explain what a hash function does.

You'll learn to

Tick what you feel confident about. These self-checks do not affect your lesson score.

Fig S2 · Interactive
01 / 09

Twelve bits, three hashes

A Bloom filter remembers which words it has seen, using very little memory. It is an array of m bits, all 0 to start (here m = 12), plus k hash functions (here k = 3). A hash function turns a word into a number.

bits = [0] * M        M = 12
       [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
K = 3 hash functions, numbered 0, 1, 2
it stores no words, only bits
Loading figure…
or ← →
Read the lesson step by step
  1. Twelve bits, three hashes

    A Bloom filter remembers which words it has seen, using very little memory. It is an array of m bits, all 0 to start (here m = 12), plus k hash functions (here k = 3). A hash function turns a word into a number.

    bits = [0] * M        M = 12
           [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
    K = 3 hash functions, numbered 0, 1, 2
    it stores no words, only bits
  2. A word becomes three spots

    spots("cat") runs the word through the 3 hash functions. Each gives a number from 0 to 11: a spot in the array. For cat they are 1, 5, 6. The same word always gives the same spots.

    spots(w): hash i = sha256("i:w") mod 12
      sha256("0:cat") mod 12 = 1
      sha256("1:cat") mod 12 = 5
      sha256("2:cat") mod 12 = 6
    spots("cat") = [1, 5, 6]
    sha256 is the same on every run;
    Python's built-in hash() of a str is not
  3. Add cat: set its bits

    add() sets the bit at each spot to 1: bits 1, 5, 6. That is all an insert does. The word itself is not stored anywhere, so the filter stays the same size however long the words are.

    add(bits, "cat")
      bits[1] = 1
      bits[5] = 1
      bits[6] = 1
    bits = [0, 1, 0, 0, 0, 1, 1, 0, 0, 0, 0, 0]
    cost: k = 3 hashes, k bit writes
  4. Add dog: bits are shared

    dog's spots are 0, 5, 7. Bit 5 is already 1 from cat, so it just stays 1. Words share bits. Now 5 of the 12 bits are 1, and nothing records which word set which bit.

    add(bits, "dog")
      bits[0] = 1
      bits[5] = 1   (already 1)
      bits[7] = 1
    bits = [1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0]
    5 ones; bit 5 belongs to cat and dog
  5. Ask for cat: all ones

    has() looks at the same spots: 1, 5, 6. All three are 1, so it returns True. That means "maybe": the word was probably added. An added word always finds its own ones, so a Bloom filter never wrongly says no.

    has(bits, "cat")
      bits[1] == 1
      bits[5] == 1
      bits[6] == 1
    no zero found: return True   ("maybe yes")
  6. Ask for fox: one zero

    fox's spots are 6, 11, 9. Bit 6 is 1, but bit 11 is 0, so has() returns False at once and never checks bit 9. A single 0 proves fox was never added: if it had been, that bit would be 1.

    has(bits, "fox")
      bits[6] == 1
      bits[11] == 0  -> return False   ("definitely not")
      bits[9]        never looked at
  7. Ask for dove: a false positive

    dove was never added, but its spots 1, 6, 7 are all 1: bits 1 and 6 came from cat, bit 7 from dog. has() returns True. This wrong "maybe" is a false positive, the price paid for storing only bits.

    has(bits, "dove")
      bits[1] == 1   set by cat
      bits[6] == 1   set by cat
      bits[7] == 1   set by dog
    return True, but "dove" was never added
  8. How often it is wrong

    With 5 of 12 bits set, a new word lands on three ones about (5/12)³ ≈ 7% of the time. In general p ≈ (1 − e^(−kn/m))^k for n words. With 10 bits per word and k = 7, p is under 1%.

    this filter: 5 of 12 bits are 1
      (5/12)^3 = 0.072   about 7%
      formula, n = 2: (1 - e^(-3*2/12))^3 = 0.061
    m/n = 10 bits per word, k = 7: p = 0.0082
    best k = (m/n) ln 2 = 6.93 for m/n = 10
  9. Why you cannot delete

    Try to remove cat by clearing bits 1, 5, 6. Bit 5 was dog's too, so has("dog") now finds a 0 and says no: a false negative. That breaks the filter's one promise, so a plain Bloom filter cannot delete.

    pretend delete: bits[1] = bits[5] = bits[6] = 0
    bits = [1, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0]
    has(bits, "dog")
      bits[0] == 1
      bits[5] == 0  -> return False, but dog WAS added

Quiz Unlocks after the last step.