Say 'definitely not here' or 'maybe here' using very little memory.
A Bloom filter is a tiny, fast structure that answers one question: "Is this item in the set?" It answers either "definitely not" or "maybe yes".
Start with an array of m bits, all set to 0. To add an item, run it through k hash functions. Each hash gives a position in the array. Set those k bits to 1.
To check an item, hash it the same way and look at the same k positions. If any of those bits is 0, the item was never added: the answer is a sure no. If all are 1, the item is probably there. Those bits might have been switched on by other items, which is how a false positive happens.
| What | Formula | Plain meaning |
|---|---|---|
| False positive rate | (1 - e^(-kn/m))^k | Chance that all k bits of an unseen item are already 1 |
| Best number of hashes | k = (m / n) x ln 2 | About 0.69 hashes for each bit available per item |
| Bits needed for target rate p | m = -n x ln(p) / (ln 2)^2 | About 9.6 bits per item for 1%, about 14.4 for 0.1% |
| Variant | What changes | Good and bad |
|---|---|---|
| Standard Bloom filter | Bits only | Smallest and simplest. No delete. |
| Counting Bloom filter | Small counters instead of bits | Supports delete. Uses several times more memory. |
| Scalable Bloom filter | Adds a new, bigger filter when full | Grows with the data. Checks must look in every layer. |
| Blocked Bloom filter | All k bits fall in one cache line | Faster in memory. A slightly higher false positive rate. |
| Cuckoo filter | Stores short fingerprints instead of bits | Delete is supported and lookups touch fewer places. See the Cuckoo Filter topic. |
Every word uses the same fixed hash, so the picture is the same each time. The measured rate comes from 200 fixed words that were never added, so it can differ a little from the formula. The filter has no delete: a bit may belong to several words.
add(item):
for i in 0..k-1:
bits[ hash_i(item) % m ] = 1
maybe_contains(item):
for i in 0..k-1:
if bits[ hash_i(item) % m ] == 0:
return false // definitely not
return true // maybe| Term | Meaning in easy words |
|---|---|
| Bloom filter | A small bit-array structure that answers 'definitely not' or 'maybe yes' for set membership |
| Bit array | The row of m bits (0 or 1) that holds the filter state |
| Hash function | A function that turns an item into a number, so we can pick a bit position |
| k | How many hash functions (and bit positions) are used for each item |
| m | The number of bits in the array |
| n | The number of items added to the filter |
| False positive | The filter says 'maybe yes' for an item that was never added |
| False negative | The filter says 'no' for an item that was added. A Bloom filter never does this |
| Fill ratio | The share of bits that are set to 1 |
| Counting Bloom filter | A variant with small counters instead of bits, so items can be removed |
| Double hashing | Making k hashes out of two: h1 + i x h2 |
| Choice | We gain | We lose |
|---|---|---|
| More bits per item | Fewer false positives | More memory |
| More hash functions (up to the best k) | Lower error | Slower add and check; past the best k the error goes up again |
| Counting filter | Delete support | About 3 to 4 times more memory |
| Bloom vs hash set | Far less memory | Possible false positives, no way to read items back |
| Bloom vs cuckoo filter | Simpler and often faster to insert | No delete, and usually more space at low error rates |
| Rebuild on a schedule | Removes old items, keeps filter small | Extra work and a short period of reduced accuracy |