Sub topic 1
The problem: fast membership tests in little memory
Prerequisites
This chapter assumes you can read basic Python and know what a hash function and a hash table are at a high level. You should also be comfortable with bits and bytes, and with the idea of a probability. No Bloom filter knowledge is needed.
The membership problem
A membership test asks one question: is this item in the set? Many systems ask it millions of times per second. A web crawler asks whether it has already visited a URL. A database asks whether a key might exist on disk before it pays for a slow read. A signup form asks whether a username is taken.
The exact answer is easy to get. Store every item in a hash set and look the item up. The cost is memory, because the set must hold every item in full.
seen = set()
seen.add("https://example.com/a")
print("https://example.com/a" in seen) # True
print("https://example.com/b" in seen) # FalseThis works well for thousands of items. It becomes a problem at hundreds of millions. A set of 100 million URLs averaging 60 bytes each needs about 6 GB for the text alone. Python's per-object overhead and the hash table's spare slots add more. That data may not fit in RAM, and a lookup that goes to disk or the network is thousands of times slower than one in memory.
Trading exactness for space
A Bloom filter gives up exactness to cut memory. It does not store the items. It stores a small fixed-size array of bits, and each item only flips a few of those bits. A query looks at those bits and answers one of two things:
- Definitely not in the set. This answer is always correct.
- Possibly in the set. This answer is usually correct, but it can be wrong.
The wrong "possibly" answer is called a false positive. The filter never gives a false negative, so an item that was added is never reported as missing. The following chapters explain why.
For a 1% false positive rate, a Bloom filter needs about 9.6 bits per item, whatever the size of the item. A 60-byte URL takes 480 bits in a plain set, so the filter is roughly 50 times smaller for that data.
| Structure | Stores items | Memory per item | Wrong answers |
|---|---|---|---|
| Hash set | Yes | Size of the item plus overhead | None |
| Bloom filter | No | About 9.6 bits at 1% error | False positives only |
Where the filter fits
The filter is useful when a "no" answer saves expensive work and an occasional unnecessary "maybe" is cheap to handle. The usual pattern puts the filter in front of a slow, exact check.
Suppose 99% of queries are for items that are not stored. The filter answers nearly all of them from memory. Only the true hits and about 1% of the misses reach the slow store. A false positive costs one wasted lookup, and it never produces a wrong final answer, because the exact check still runs.
Some uses accept the "maybe" without any follow-up, such as skipping a URL that was probably crawled already. Those uses need a low error rate, since nothing corrects the mistakes.
Requirements the filter must meet
The design has to satisfy three goals at once:
- Memory use is a small, fixed number of bits per item, chosen in advance.
- Each add or lookup takes constant time that does not depend on how many items are stored.
- The error is one-sided, so a "no" can always be trusted.
Later chapters build the structure from two parts. A bit array holds the state, and hash functions turn each item into positions in that array.
Exercise
A service checks a slow database for each requested key. About 95% of requested keys do not exist. You put a Bloom filter with a 1% false positive rate in front of the database. Roughly what fraction of all requests still reach the database?
Show answer
The 5% of requests for existing keys always reach the database, because the filter never gives a false negative. Of the 95% that do not exist, about 1% are false positives, which is 0.95 × 0.01 ≈ 0.95% of all requests. The total is about 5.95%, so roughly 6% of requests reach the database instead of 100%.
Exercise
Why can a Bloom filter in front of a slow exact store never cause a wrong final answer, even though the filter itself can be wrong?
Show answer
The filter's only error is a false positive, which sends the query on to the exact store. The exact store then gives the correct answer. A "definitely not" from the filter is always correct, so no valid item is ever turned away.