A tiny bit array that answers 'definitely not' or 'probably yes'. Here is why databases use it to skip slow disk reads.
Is "apple" in a list of 10 million words? A Bloom filter can answer in a few memory lookups, using under 10 bits per word. The catch: it's allowed to be wrong, but only in one direction.
That is the whole deal, and it is simpler than it sounds. A Bloom filter is a tiny memory-saver that answers one question: "have I seen this thing before?" Burton Bloom described it in 1970, in a paper called Space/Time Trade-offs in Hash Coding with Allowable Errors. The title names the trade: give up a bit of accuracy, get a lot of space back.
The thing was never added. This answer is always right.
The thing was maybe added, or maybe it's a false alarm. This answer can be wrong.
So "no" is a promise, and "yes" only means "maybe, go check."
But how can something that stores no words answer anything? Let's build one.
Two parts. A row of bits (tiny switches, each 0 or 1), all starting at 0. And a few hash functions: recipes that turn any word into a position number in that row.
Say the row has 16 bits and we use 3 hash functions. To add "apple", run it through all 3 recipes. Say they give positions 2, 7 and 11. Flip those three bits to 1.
Click Next to see "apple" get added, then looked up, then a different word ("banana") get checked.
Add apple
Notice that nothing is stored except those switches. The word "apple" itself is gone. The positions 2 and 11 in the banana check are also made up, just to show an overlap.
The filter only needs a row of bits, not the words. So why is a 0 a promise but a 1 only a "maybe"?
If a word had been added, its bits would have been flipped to 1. A bit never goes back to 0, so seeing a 0 means the word was never added.
All 1s is only a "probably" because other words may have flipped those same bits by accident. The filter cannot tell whose bits they are.
So how often is a "yes" wrong? There is a formula for that.
Say the row has bits, you've added words, and you use hash functions. The chance of a false alarm is about
This can be checked against the exact version, , and for big they match closely. With , and , the result is about 0.8%.
The best number of hash functions is
Here is the tricky part. More hash functions help you spot a missing word faster, but they also fill the row with 1s faster. So there's a sweet spot, and that formula is it.
Each time you cut false alarms by 10 times, you pay only about 4.8 more bits per item. That is cheap. But there are things a Bloom filter cannot do.
Also, if you keep adding words to a row that's too small, it fills with 1s and nearly everything becomes "probably yes". The filter does not break, it just stops being useful. Size it for the number of words you expect.
So where is this actually worth it?
Many databases keep data in files on disk, and disk is slow. Looking for a key that isn't there is the worst case, because you'd have to check file after file to be sure.
The Bigtable paper from Google describes this exact fix. A Bloom filter for each file lets the database ask whether the file might hold a row, and the paper reports that most lookups for non-existent rows or columns then don't need to touch disk. Bigtable stores data in files called SSTables, the same file style used by LSM-tree stores.
That is the nice part. A false alarm just means a bit of extra work, since the real file is still checked afterwards. And the "no" answers, the ones you can trust, save the most work.
| Question | Plain list of keys | Bloom filter |
|---|---|---|
| Is it definitely missing? | Yes, exact | Yes, exact |
| Is it definitely there? | Yes, exact | Only "probably" |
| Memory used | Grows with key length | A few bits per key |
| Remove a key? | Yes | No (basic version) |
Other systems the Wikipedia article lists as users: Apache HBase, Apache Cassandra, ScyllaDB and PostgreSQL.
You trade a tiny, controlled chance of being wrong for a huge saving in memory. And the trade is safe because the wrong answer is always the harmless kind.