We will build a Bloom filter on paper, with ten bits and two entries, and then scale the same idea up to a million. The problem it solves is simple to state. You have a very large set of items, say registered usernames, and you want to ask “is this one in the set?” cheaply, without storing the whole set in memory and without visiting the disk for every query.

Step 1: an empty row of bits

Take ten bits, numbered 0 to 9, all set to zero. Pick three hash functions. Each one takes any text and turns it into a position between 0 and 9. We will pretend the hashes behave like random dice, which good ones roughly do.

Step 2: add two names

To add “kumar”, run it through the three hashes. Suppose they give positions 1, 4 and 7. Set those three bits to one. To add “lakshmi”, suppose the hashes give 2, 4 and 9. Set those. Position 4 was already one, which is fine; setting a bit twice changes nothing.

The array now has ones at positions 1, 2, 4, 7 and 9. Six settings, five bits, because one overlapped. The names themselves are not stored anywhere. Only the bits remain.

Step 3: ask about a name that is not there

Now someone asks about “zoya”. The hashes give 0, 4 and 7. Check the bits. Position 4 is one, position 7 is one, but position 0 is zero. If zoya had been added, bit 0 would have been set. It is not, so zoya was never added. The answer is a firm no.

This is the property to remember. A single zero bit proves absence. A Bloom filter never gives a false negative: anything you added will always find all of its bits set.

Step 4: ask about another name that is not there

Now “devika”, who was never added. Her hashes give 1, 2 and 9. Check: bit 1 is one (from kumar), bit 2 is one (from lakshmi), bit 9 is one (also lakshmi). All three are set, so the filter answers maybe. It is wrong, because devika is not in the set. This is a false positive: other names happened to cover all of her positions between them.

So the answer has two shapes. “Definitely not present” is always true. “Possibly present” can be false. The usual way to use this is as a gatekeeper in front of something expensive. On a “no”, skip the expensive lookup. On a “maybe”, go and do the real check.

How often is “maybe” wrong?

With m bits, k hashes and n items inserted, the false positive probability is approximately (1 minus e to the power of minus kn/m), all raised to the power k. For our toy filter, with m = 10, k = 3 and n = 2, that comes to about 9.2 percent. Ten bits was deliberately cramped; that is why we saw a false positive within two queries.

Now scale up. Suppose a service in Guwahati has one million usernames and gives the filter 10 bits per item, so 10 million bits, which is 1.25 MB. With seven hashes, the same formula gives about 0.8 percent. Roughly 99 out of 100 lookups for names that do not exist are answered “no” from memory, instantly. The set itself might be hundreds of megabytes on disk. The best number of hashes for a given size is about 0.69 times the bits per item, which is where seven comes from at ten bits per item.

Two knobs are visible here. More bits per item lowers the false positive rate. And the filter degrades as it fills: past the number of items you planned for, more bits become ones and the “maybe” answers become less useful.

What it cannot do

  • It cannot delete. Clearing a bit might erase evidence for another item that shares it. Variants such as counting Bloom filters keep a small counter per position to allow removal, at the cost of more space.
  • It cannot list its members or tell you how many there are exactly.
  • It cannot be trusted as the final authority when a wrong “yes” is costly. It only ever saves work.

Where you will meet it

Storage engines built on sorted files, such as Cassandra, HBase and RocksDB, commonly keep a Bloom filter per file so that a read for a missing key can skip files without touching the disk. Caches use them to avoid looking up keys that were never stored. Some systems use them to check a candidate against a large blocklist before doing a slower exact match. In each case the shape is the one we just built by hand: a cheap “no”, an occasional unnecessary “maybe”, and nothing worse.

Figures and names in the examples are illustrative.

Leave a Reply

Trending