Bloom Filters Explained: How Databases Answer Maybe

Bloom filters explained in plain terms means understanding the tiny data structure that answers one question, is this item in the set, with a definite no or a cautious maybe. The central question is how a database can check millions of keys without storing them all, and the answer is to keep a compact summary of the set, and accept that the summary can sometimes be wrong in one direction only.

A Bloom filter is a probabilistic data structure, invented by Burton Bloom in 1970, that tests whether an item is a member of a set. It uses far less memory than storing the items themselves, it answers in constant time, and it never gives a false negative: if it says an item is not in the set, that is certain, while if it says it might be, that might be wrong.

The idea is simple: hash the item several times, and mark the positions. A Bloom filter is an array of bits, all starting at zero, and adding an item means feeding it through several hash functions and setting the bits they produce. Checking an item means hashing it the same way: if any bit is zero, the item was never added, and if all are one, it probably was. This article explains what Bloom filters are, how they work, and where they are used.

Key takeaways

  • A Bloom filter tests set membership with a definite no or a maybe.
  • It never gives false negatives, but it can give false positives.
  • It uses a few bits per item, far less than storing the items.
  • It answers in constant time, whatever the size of the set.
  • It suits skipping expensive work, such as disk reads, when the answer is no.

What Bloom filters are

A Bloom filter is a space-efficient summary of a set, built from a bit array and a handful of hash functions. The bit array might hold a few bits per item in the set, compared with the tens or hundreds of bytes that storing the item itself would need, which is why filters are used where memory is scarce and the set is huge.

The one-sided error is the key to understanding it. A filter can never say that an item is absent when it is present, because adding an item sets all of its bits, and those bits stay set. But bits are shared between items, so an absent item can find all of its bits already set by other items, and the filter will wrongly say it might be present. That false positive rate is controlled by the size of the array and the number of hash functions.

The trade-off is tunable and predictable. A larger array, or more hash functions, lowers the false positive rate, at the cost of more memory or more work per check, and the mathematics lets a designer pick the size that gives the error rate they accept. Ten bits per item and seven hash functions, for example, give a false positive rate of about one per cent. Our guide to hashing explains the functions a filter is built on, and our guide to caching explains the layer a filter usually protects.

How Bloom filters work

Adding an item starts with hashing. The item is fed through each of the filter’s hash functions, and each one produces a position in the bit array, so an item with three hash functions sets three bits. The bits are set to one, and the item itself is never stored, which is why the filter is so small.

Checking an item repeats the same hashing. The item is fed through the same functions, and the bits at the resulting positions are read. If any of them is zero, the item cannot have been added, because adding it would have set that bit, so the answer is a certain no. If all are one, the item may have been added, or other items may have set those bits by coincidence, so the answer is a maybe.

Removing an item is the one thing a basic Bloom filter cannot do. Clearing its bits would also clear bits shared with other items, creating false negatives, the one error the filter promises never to make. Counting Bloom filters store a small counter at each position instead of a single bit, and allow removal at the cost of more memory.

The false positive rate grows as the filter fills up. An empty filter never says maybe, a half-full filter says maybe rarely, and a nearly full filter says maybe for almost everything, which makes it useless. That is why a filter is sized for the items it will hold, and rebuilt when the set outgrows its design.

Choosing the number of hash functions is a balance. Too few, and the filter is weak; too many, and the array fills up too fast, which also raises the error rate. For a given size and item count there is an optimum, usually around seven hash functions for ten bits per item.

A laptop on a dark desk with its screen showing a glowing green row of switches, some lit, some dark, standing for a set of bits
A laptop showing a glowing green row of switches, some lit and some dark, shows how a Bloom filter works: a few bits standing for a whole set.

Comparing Bloom filters with other lookups

A Bloom filter is one of several ways to test membership, and each suits a different need. A hash table gives exact answers, a B-tree gives exact answers in order, and a cache holds the items themselves, but each has its own trade-offs.

The table below sets out the main contrasts between the leading approaches.

Aspect Bloom filter Hash table B-tree
Answers Definitely no, or maybe Exact yes or no Exact yes or no
Memory Tiny, a few bits each One entry per key One entry per key
Speed Constant time Constant time Logarithmic time
False positives Possible None None
Best for Skipping disk reads Exact lookups Ordered scans

The practical result is that a Bloom filter is the right choice when a cheap maybe is enough to skip expensive work, a hash table wins when the answer must be exact and the data fits in memory, and a B-tree is the choice when ordered access matters.

Where Bloom filters are used

Databases are the largest use. A database that stores data in files on disk keeps a Bloom filter for each file, summarising the keys inside it, and a lookup checks the filter first: if it says no, the file is skipped without a disk read, and if it says maybe, the file is read and checked properly. That turns many lookups into a single read.

Web caches use Bloom filters to share what they hold. A cache can publish a filter of its contents, so other caches can ask whether it holds an item without sending the item itself, which saves bandwidth and keeps the summary small enough to exchange often.

Spell checkers were one of the earliest uses. A dictionary is too large to hold in the memory of an early computer, but a Bloom filter of the dictionary fits easily, and it can say with certainty that a word is misspelled, while an accepted word is checked against the full dictionary only when it matters.

Network routers use Bloom filters to track block lists. A router checks each packet against the filter in constant time, and only does the expensive full lookup when the filter says maybe, which keeps the fast path fast under heavy traffic.

Content delivery networks use Bloom filters to avoid fetching what is not there. Before asking an origin server for an object, a node checks a filter of what the origin holds, and a definite no means it can answer at once, saving a round trip for every missing object.

Strengths and limits

The greatest strength of a Bloom filter is its size. A filter holding a million items can fit in a megabyte, where storing the items themselves would need tens of megabytes, and it answers in constant time no matter how large the set grows, which is why it is used wherever memory is tight and sets are huge.

It is also a strength that the filter’s error is one-sided and predictable. A definite no is always right, and the false positive rate can be calculated in advance and tuned by sizing, so a designer knows exactly what a one per cent maybe rate is worth in memory saved.

The limits are real, though. A basic filter cannot remove items, it cannot count how many times an item was added, and it cannot list the items it holds, since it never stored them. When the set grows beyond the filter’s design, the error rate climbs steeply, and the only fix is to build a bigger filter and re-add everything.

Bloom filter checklist

  • Size the filter for the items you expect, plus room to grow.
  • Pick the hash count from the size and the error rate you accept.
  • Use a fast, well-spread hash, since a poor hash raises false positives.
  • Measure the real false positive rate, and compare it with the theory.
  • Rebuild the filter when the set outgrows its design.
  • Use a counting variant if you need to remove items.
  • Never use a maybe as a yes: always check the real store afterwards.
  • Keep the filter in memory, since a disk read defeats its purpose.
  • Document the error rate, so callers know what maybe means.

Frequently asked questions

What is a Bloom filter in simple terms?

A Bloom filter is a tiny summary of a set that can say an item is definitely not in the set, or that it might be. It uses a few bits per item instead of storing the items, and it answers instantly, at the cost of occasional false maybes.

What is a false positive in a Bloom filter?

It is when the filter says an item might be in the set, but it is not. It happens because bits are shared between items, so an absent item can find all of its bits already set by others. The rate is controlled by the filter’s size.

Why are there never false negatives?

Because adding an item sets all of its bits, and those bits are never cleared. If any of an item’s bits is zero, the item was never added, so a no is always certain, which is the guarantee the filter is built on.

Can you remove items from a Bloom filter?

Not from a basic one, because clearing bits would also clear bits shared with other items, which would create false negatives. Counting Bloom filters store a small counter per position, and they do allow removal.

How do you choose the size of a Bloom filter?

From the number of items and the false positive rate you accept. Ten bits per item with about seven hash functions gives roughly a one per cent error rate, and the mathematics gives the size for any target rate.

When should I use a Bloom filter?

When a cheap maybe can save expensive work, such as a disk read or a network round trip, and a definite no is common. If the answer must be exact, or the data must be listed, a hash table or a database is the right tool.

The bottom line

Bloom filters explained in one line is this: hash each item a few times, set those bits, and answer no for certain when any bit is clear, or maybe when they are all set.

They are the smallest useful summary a database can keep, and they quietly save disk reads, round trips, and memory across the systems that store the world’s data.

This article is general information about how Bloom filters work, not advice about designing storage for a particular system.

Latest articles

Scroll to Top