17. Bloom Filters

A probabilistic set — fast, compact, and only wrong in one direction

Imagine you're building a web browser and you need to check every URL the user visits against a list of millions of known-malicious sites. You could store the entire list in memory and do an exact lookup — but that list might be hundreds of megabytes. What if you could answer "is this URL in the dangerous list?" using just a few megabytes, with the tradeoff that you're occasionally wrong — but only in one direction?

That's a Bloom filter: a space-efficient probabilistic data structure that can tell you "definitely not in the set" or "probably in the set." It was invented by Burton Howard Bloom in 1970, and it's everywhere in systems programming.

The Problem

You have a huge set of items — millions of URLs, billions of database keys, or a dictionary of English words. You need to answer membership queries: "Is this item in the set?" And you need to do it fast, ideally in constant time, using as little memory as possible.

A hash set gives you O(1) lookups, but it stores every element. For a million 50-character URLs, that's at least 50 MB of raw string data, plus hash table overhead. A Bloom filter can answer the same question using roughly 1.2 MB — with a less-than-1% chance of being wrong.

The Idea: A Bit Array + Multiple Hash Functions

A Bloom filter has two components:

  • A bit array of m bits, all initially set to 0
  • k independent hash functions, each mapping an element to a position in [0, m-1]

Two Operations

Insert(x): Compute h1(x), h2(x), ..., hk(x). Set each of those k bit positions to 1.

Query(x): Compute the same k hash positions. If any of those bits is 0, the element is definitely not in the set. If all bits are 1, the element is probably in the set.

Why It Works

The key insight is asymmetric certainty:

No False Negatives

If you inserted an element, its k bits were set to 1. Bits in a Bloom filter are never cleared. So when you query that element, all k bits are guaranteed to still be 1. The filter will always say "probably yes" for elements that were actually inserted.

Possible False Positives

When you query an element that was never inserted, all k of its bit positions might still be 1 — set there by other elements. The more items you insert, the more bits are set to 1, and the more likely this coincidence becomes.

This is why Bloom filters are described as "only wrong in one direction." A "no" answer is always trustworthy. A "yes" answer might be a false positive.

A Simple Worked Example

Let's use a tiny Bloom filter: m = 10 bits and k = 3 hash functions.

Initial state: all bits are 0

Index:  0  1  2  3  4  5  6  7  8  9
Bits:  [0][0][0][0][0][0][0][0][0][0]
  1. Insert "cat"

    Hash functions produce: h1("cat") = 1, h2("cat") = 4, h3("cat") = 7. Set bits 1, 4, and 7 to 1.

    Index:  0  1  2  3  4  5  6  7  8  9
    Bits:  [0][1][0][0][1][0][0][1][0][0]
  2. Insert "dog"

    Hash functions produce: h1("dog") = 3, h2("dog") = 4, h3("dog") = 8. Set bits 3, 4, and 8 to 1. Note that bit 4 was already 1 — that's fine.

    Index:  0  1  2  3  4  5  6  7  8  9
    Bits:  [0][1][0][1][1][0][0][1][1][0]
  3. Query "cat" — was it inserted?

    Check bits 1, 4, 7. All are 1. Answer: "probably in the set" — and in this case, it really was inserted, so this is a true positive.

  4. Query "bird" — was it inserted?

    Hash functions produce: h1("bird") = 0, h2("bird") = 4, h3("bird") = 6. Check bit 0 — it's 0! Answer: "definitely not in the set." We didn't even need to check the other bits.

When would a false positive happen?

Suppose we query "fish" and its hash positions are 1, 3, 8. All three bits happen to be 1 — set by "cat" (bit 1) and "dog" (bits 3 and 8). The filter reports "probably in the set" even though "fish" was never inserted. That's a false positive.

The Math: False Positive Probability

After inserting n elements into a Bloom filter with m bits and k hash functions, the probability of a false positive is approximately:

False Positive Rate

p ≈ (1 - e^(-kn/m))^k

Where:

  • p — the probability a query for an element not in the set returns "probably yes"
  • m — the number of bits in the bit array
  • n — the number of elements inserted
  • k — the number of hash functions
  • e — Euler's number (≈ 2.718)

The term (1 - e-kn/m) approximates the probability that any single bit is 1 after n insertions. Raising it to the k-th power gives the probability that all k bits for a non-member are 1.

Choosing Optimal k and m

Given a target false positive rate p and an expected number of elements n, you can compute the optimal parameters:

Optimal bit array size

m = -(n × ln(p)) / (ln(2))²

This gives the minimum number of bits needed for the target false positive rate.

Optimal number of hash functions

k = (m / n) × ln(2)

This minimizes the false positive rate for a given m and n.

The intuition: too few hash functions means each query checks too few bits and can't distinguish members from non-members. Too many hash functions fill up the bit array too quickly. The optimum balances these two effects.

Practical Example: Sizing a Filter

Goal: <1% false positives for 1 million items

Given:
  n = 1,000,000  (items)
  p = 0.01       (target false positive rate = 1%)

Step 1: Compute m (bits needed) m = -(1,000,000 × ln(0.01)) / (ln(2))² m = -(1,000,000 × (-4.605)) / (0.693)² m = 4,605,000 / 0.480 m ≈ 9,585,059 bits m ≈ 1.14 MB

Step 2: Compute optimal k k = (9,585,059 / 1,000,000) × ln(2) k = 9.585 × 0.693 k ≈ 6.64 → round to 7

Result: 9.6 million bits (1.2 MB) and 7 hash functions gives <1% false positive rate for 1 million elements

Compare this to storing 1 million items directly — even just storing 32-bit hashes would take ~4 MB. The Bloom filter uses about 9.6 bits per element, regardless of element size.

What You Can't Do: Deletion

A standard Bloom filter doesn't support deletion. If you clear the bits for an element, you might accidentally clear bits shared by other elements — causing false negatives, which breaks the fundamental guarantee.

Why deletion breaks things

After inserting "cat" (bits 1,4,7) and "dog" (bits 3,4,8):

Index: 0 1 2 3 4 5 6 7 8 9 Bits: [0][1][0][1][1][0][0][1][1][0]

Now "delete" cat by clearing bits 1, 4, 7:

Index: 0 1 2 3 4 5 6 7 8 9 Bits: [0][0][0][1][0][0][0][0][1][0]

Query "dog" — check bits 3, 4, 8. Bit 4 is now 0! Answer: "definitely not in the set." But "dog" IS in the set! We just created a false negative.

Counting Bloom Filters

The fix is straightforward: replace each bit with a counter. Instead of setting a bit to 1, increment the counter. Instead of clearing a bit, decrement it. An element is "present" if all its counters are greater than zero.

Counting Bloom Filter

Insert: increment k counters.
Delete: decrement k counters.
Query: check if all k counters are > 0.

The tradeoff: instead of 1 bit per position, you need 3–4 bits per counter, so the filter uses 3–4x more memory. But you gain the ability to remove elements.

Where Bloom Filters Are Used

Application How it uses a Bloom filter
Chrome Safe Browsing Check URLs against a malicious-site list without storing every URL locally. A "probably yes" triggers a full server check.
CDN caches Avoid caching one-hit-wonder objects. Only cache an item after the Bloom filter says it's been requested before.
Database query optimization Avoid expensive disk reads. Check a Bloom filter first — if the key is "definitely not" on disk, skip the I/O entirely. Used in Cassandra, HBase, and LevelDB.
Bitcoin SPV nodes Lightweight Bitcoin nodes use Bloom filters to request only relevant transactions from full nodes, without revealing exactly which addresses they own.
Spell checkers Check if a word is in the dictionary using far less memory than storing every word. A false positive just means a misspelled word isn't flagged.
Network routers Track which packets have been seen to detect duplicates or loops, using minimal per-flow state.

Key Takeaways

  • A Bloom filter is a bit array with k hash functions — insert sets k bits, query checks k bits
  • If any checked bit is 0, the element is definitely not in the set (no false negatives)
  • If all checked bits are 1, the element is probably in the set (possible false positives)
  • False positive rate is approximately (1 - e-kn/m)k
  • For 1% false positive rate, you need about 9.6 bits per element — regardless of element size
  • Standard Bloom filters don't support deletion; counting Bloom filters do, at the cost of more memory
  • They're used everywhere: browsers, databases, CDNs, cryptocurrency, networking

Try It Yourself — Bloom Filter

Set the bit array size and number of hash functions, then insert and query words. Watch the bits light up and see false positives emerge as the filter fills.

Bit Array

Inserted Words

No words inserted yet.

0
Items inserted
0
Bits set to 1
0%
Fill ratio
0%
Est. false positive rate

Bloom filters are a perfect example of a powerful idea in systems design: sometimes you don't need the exact answer. If you can tolerate a small, controlled probability of error — and that error only goes in one direction — you can save enormous amounts of memory and time. It's a tradeoff that shows up everywhere, from databases to browsers to blockchains.