Skip to the workspace
Pebellslearn by building
Bits & Machine Numbers  ›  Lesson

Bloom Filters

Bloom Filters 30 minA row of bits and a few hashes that can say "definitely not" with certainty and "maybe" with a known error

dsa / Bloom Filters

Explore each scene at your own pace.

01 · How it works

How it works

Bloom Filters

Definition
A Bloom filter is a compact structure made of a bit array and several hash functions that answers whether a key might have been added: a definite no if any of its positions is 0, and a maybe if all are 1.
In plain words
It is a doorman with a small notepad of marks who can say for certain that you have never been here, but can only say you might have when your marks are all present. It uses little memory, and the cost is the occasional wrong maybe.

In short: a Bloom filter is m bits plus k hash functions. Inserting a key sets its k positions; querying checks them, and if any is still 0 the key was definitely never inserted, while all-1s means "maybe". Insert and query both cost O(k) time, and the whole filter costs O(m) space, about 10 bits per key for a 1% false-positive rate — far less than storing the keys themselves. It can never produce a false negative, because nothing ever clears a bit, but it can be fooled into a false positive once enough other keys have filled in the same positions.

An 8-bit row where inserting "cat" and "dog" sets five of the bits green, and querying "fox" stops at bit 5, which is still 0

Quarry writes records into segment files on disk. Looking a record up means asking each segment "do you have it?", and each question costs a disk read. That's fine when the answer is yes. It's pure waste when the answer is no, and most answers are no: a key lives in one segment, not in the twenty others you'd have to rule out.

What Quarry wants is a tiny in-memory summary per segment that can say "definitely not here" without touching the disk. It's allowed to sometimes say "maybe" wrongly, since that only costs a wasted read, but it must never say "not here" about a key that is there, because that would lose data. A Bloom filter is exactly that. It's a row of bits plus a few hash functions, and it costs about 10 bits per key for a 1% false-positive rate, however long the keys are.

How it works

A Bloom filter is an array of m bits, all 0 at the start, and k hash functions that each map a key to a position in 0 … m − 1.

  • Insert a key: compute its k positions and set those bits.
  • Query a key: compute the same k positions. If any of those bits is 0, the key was never inserted. That's a certain "no". If all are 1, the answer is "maybe". The bits might have been set by other keys.

Step through a 16-bit filter with k = 3. Three records go in, then two lookups happen, one of which is fooled:

watch it move · array

Inserts only ever set bits. A query stops at the first 0 it finds, and it can be fooled only when other keys have set every one of its bits.

Insert rec-11. Its three hashes land on 4, 3 and 2, so those bits are set. 16 cells.

1 / 6

Two facts fall out of this picture:

  1. No false negatives. An inserted key's bits were set, and nothing ever clears a bit, so its query always finds all ones.
  2. False positives grow as the filter fills. The more bits are 1, the likelier a random key's k probes all land on ones.
What does this print?
bits = 0
for p in (3, 9, 3):
    bits |= 1 << p
print(bin(bits).count("1"), bits >> 9 & 1)
Scene 1 of 7
Project context and further resources
You're building a piece ofQuarry — the record encoder
This piece — bloom_lookup(): Answers "definitely not stored" without touching disk.
Scenario A lookup for a missing record used to read every segment on disk. Now each segment carries a Bloom gate of about 10 bits per key. Most segments answer "definitely not" from memory, and only a false positive, about 1 time in 120, costs a wasted read.
bloom_lookup.py
Call the function you wrote — just like your app's frontend would. Edit the inputs and hit Call.
bloom_lookup(stored, queries, m, k) → listMaybe (True) or definitely not (False) for each query.
try:
returned
Press Run to execute your code.
Press Check to run the tests for this lesson.
Hover to wake the cannons. Drag to move, or use arrow keys when focused. Press Enter to fire a shot.