The Membership Problem and Bloom Filters
Overview
Systems often need to answer "is this item in my big collection?" billions of times a day. Storing every item in memory is costly, and asking a slow disk or database every time wastes effort. A Bloom filter is a tiny structure that answers most of these questions instantly.
Detailed Notes
What is a Bloom filter
A Bloom filter is a small, fast, probabilistic data structure that tells you whether an item is in a set. "Probabilistic" means the answer is not always exact. It gives one of two replies:
- "Definitely not in the set": always correct.
- "Probably in the set": usually correct, but sometimes wrong.
A wrong "probably" is called a false positive (the filter says yes, the item was never added). A wrong "definitely not" would be a false negative, and a standard Bloom filter never produces one.
The problem it solves
Imagine a database that keeps its data in 50 files on disk. A request asks for the key user:999, which was never written. Without help, the database must read all 50 files just to say "not found". That is 50 slow disk reads for nothing.
You could keep a full list of every key in memory, but with billions of keys that is tens of gigabytes. A Bloom filter gives you most of the benefit using a few bits per item.
Most "does it exist?" checks in real systems are for items that do not exist, so a cheap "definitely not" removes most of the work.
How it works
Think of a Bloom filter as a long row of light switches, all off at the start. You also need a few hash functions. A hash function turns any item into a number, always the same number for the same item.
- Add an item: run it through, say, 3 hash functions. Each gives a position. Flip those 3 switches on.
- Check an item: compute the same 3 positions. If any switch is off, the item was never added, so the answer is "definitely not". If all are on, the answer is "probably yes", because other items might have switched on those same positions by coincidence.
That coincidence is exactly where false positives come from. As more items are added, more switches are on and false positives become more likely.
Switches are never turned off, so an added item always finds its switches on. That is why false negatives cannot happen.
A simple example
A small filter with 10 switches and 2 hash functions:
add "cat" -> positions 2 and 7 -> switches 2, 7 on
add "dog" -> positions 4 and 7 -> switches 4, 7 on
check "cow" -> positions 1 and 4 -> switch 1 is off => definitely not
check "cat" -> positions 2 and 7 -> both on => probably yes
check "emu" -> positions 2 and 4 -> both on => probably yes (false positive!)
"emu" was never added, but "cat" and "dog" happened to switch on both of its positions.
System design example
A database like Cassandra keeps a Bloom filter in memory for each data file. For a read, it checks each filter. Files that answer "definitely not" are skipped, so the database reads only the one or two files that might hold the key. A false positive only costs one extra, harmless read that finds nothing.
Summary
- A Bloom filter answers "is X in the set?" with definitely not or probably yes.
- It uses very little memory compared with storing every item.
- False positives can happen; false negatives cannot.
- It is a cheap check placed in front of an expensive lookup.
- It is a hint, never the source of truth.
Architecture Diagram
A cheap in-memory check avoids most expensive lookups.
- 1A request asks whether key K exists.
- 2The Bloom filter is checked in memory.
- 3If it says no, return not-found with no disk read.
- 4If it says maybe, query the real store for the true answer.
Common Interview Questions
Finished this lesson?
0 of 91 lessons done · 0% of the course

