Bloom Filters & Their Successors
Set membership with false positives, and cuckoo and xor filters.
A Bloom filter answers "have I seen this before?" with about 9.6 bits per item at a 1% error rate, whatever the items are. Information theory says 6.6 bits would do. This lesson computes the false-positive rate exactly, proves that lower bound, and follows fifty years of successors that close part of the gap while adding what Bloom filters lack: deletions, cache locality and speed.
What you’ll learn
- Express a Bloom filter's false-positive rate exactly, and prove that the classic formula is a lower bound on it.
- Prove that any approximate-membership structure needs about bits, and place Bloom filters' factor against static and dynamic lower bounds.
- Compare counting, partitioned and blocked Bloom filters by space, false-positive rate and cache misses.
- Explain how quotient, cuckoo and xor filters store fingerprints, and choose among them by space, deletions and static or dynamic use.
The false-positive rate, exactly
Fix the set of inserted keys and a key . The false-positive rate is the probability, over the hash functions, that the Bloom filter answers "present" for . Model the functions as independent and uniform over the bits, as Bloom did. Then the whole analysis turns on one random quantity: the number of bits that are set after the insertions.
Condition on . The positions probed for are uniform and independent of the filter's contents. Given set bits, all probes hit set bits with probability , so
The distribution of . The settings are uniform throws into bits. Exactly bits are hit when the throws map onto some -set, which a Stirling number of the second kind counts:
The classic formula plugs in the mean. Each bit is set with probability , which is therefore , and Bloom's formula is . The map is convex, so Jensen's inequality gives
The inequality is strict for because is genuinely random: the classic formula underestimates the false-positive rate (Bose et al., 2008). Since stays within of its mean, the gap vanishes as grows with and fixed:
| Exact | Classic | ||||
|---|---|---|---|---|---|
| 8 | 2 | 2 | 0.1778 | 0.1712 | 0.1548 |
| 16 | 2 | 2 | 0.0529 | 0.0518 | 0.0489 |
| 64 | 6 | 4 | 0.01016 | 0.00981 | 0.00956 |
| 100 | 10 | 7 | 0.00894 | 0.00840 | 0.00819 |
Optimizing with the approximation gives , at which half the bits are set and . In practice is rounded to an integer, and the positions need not come from separate functions: for has the same asymptotic false-positive rate (Kirsch & Mitzenmacher, 2006), so two hash computations suffice.
Even with truly random hash functions, why is Bloom's classic formula a lower bound on the exact false-positive rate?
- Real hash functions are not truly random
- The formula ignores that two keys can set the same bit
- It uses in place of
- The fraction of set bits is random, and by convexity
Show the answer
D. The fraction of set bits is random, and by convexity
Given the number of set bits, the rate is exactly . Averaging a convex function of a random quantity gives at least the function of its average. Fillings with more set bits than average hurt more than fillings with fewer help.
How much space must any filter use?
What a memory state accepts. Each of the memory states answers "yes" on some set . If represents , then and at most other keys are accepted:
One state serves few sets. A state can represent only -subsets of , at most of them, and each of the possible sets needs a state:
Estimate the ratio. Each factor increases with when , so it is at least its value at :
Conclude. Take logarithms. For a universe much larger than , :
A Bloom filter uses bits, so its overhead is exactly the factor . Is the bound reachable? For a static set, yes: structures built from perfect hashing and retrieval reach bits (Dietzfelbinger & Pagh, 2008; Porat, 2009). For a set that grows by insertions, no: Lovett and Porat (2010) proved that a dynamic structure needs bits for some depending only on , a bound sharpened since.
At , how many bits per element could an optimal static filter save compared with an optimally tuned Bloom filter?
Show the answer
4.4 bits
The Bloom filter needs bits per element, and the lower bound is . The difference, bits per element, is the prize that fingerprint filters compete for.
Engineering Bloom filters
Three engineering problems recur, and each has a standard fix.
- Deletion. A counting Bloom filter (Fan, Cao, Almeida & Broder, 2000) keeps a small counter per position, incremented on insert and decremented on delete. Four-bit counters overflow only with negligible probability at the usual loads, so it costs about four times the space. Deleting a key that was never inserted silently creates false negatives.
- Cache misses. The probes of a standard filter land in up to different cache lines. A blocked Bloom filter (Putze, Sanders & Singler, 2007) first hashes the key to one block the size of a cache line, then sets all bits inside it: one cache miss per query.
- Hashing cost. Double hashing derives all positions from two hash values, and a partitioned filter gives each function its own slice of bits, so a key's positions are always distinct.
| Variant | Space | False-positive rate | Memory accesses per query |
|---|---|---|---|
| Standard | bits per key | baseline | up to cache misses |
| Counting | about standard | same; deletions allowed | up to |
| Partitioned | same | slightly higher | up to |
| Blocked (512-bit blocks) | same | higher, increasingly so at small | 1 cache miss |
#include <cstdint>
#include <vector>
// Blocked Bloom filter: each key touches one 512-bit block, i.e. one 64-byte cache line.
struct BlockedBloom {
std::vector<uint64_t> words; // 8 words per block
uint64_t blocks;
int k; // bits per key, at most 7 here
BlockedBloom(uint64_t nblocks, int k) : words(8 * nblocks), blocks(nblocks), k(k) {}
static uint64_t mix(uint64_t x) { // splitmix64 finalizer
x += 0x9e3779b97f4a7c15ULL;
x = (x ^ (x >> 30)) * 0xbf58476d1ce4e5b9ULL;
x = (x ^ (x >> 27)) * 0x94d049bb133111ebULL;
return x ^ (x >> 31);
}
void add(uint64_t key) {
uint64_t h = mix(key), base = (h % blocks) * 8, g = mix(h);
for (int i = 0; i < k; i++, g >>= 9) // 9 bits: word (3) and bit (6)
words[base + (g >> 6 & 7)] |= 1ULL << (g & 63);
}
bool contains(uint64_t key) const {
uint64_t h = mix(key), base = (h % blocks) * 8, g = mix(h);
for (int i = 0; i < k; i++, g >>= 9)
if (!(words[base + (g >> 6 & 7)] >> (g & 63) & 1)) return false;
return true;
}
};With keys, 10 bits per key and , this filter measured a false-positive rate of 0.97%, against 0.82% for a standard filter of the same size. The splitmix64 finalizer mixes well but has no formal guarantee; add a secret random seed to the key when inputs may be adversarial.
Why the penalty? A block receives a Poisson number of keys with mean , and the false-positive rate averages a convex function of the block's load, the same Jensen effect as before:
At 10 bits per key this predicts 0.96%, matching the measurement. At 20 bits per key with it is about three times the standard rate, so blocked filters usually spend a bit or two more per key.
Fingerprint filters: quotient, cuckoo and xor
Bloom filters spread each key over bits. Their successors store one short fingerprint per key instead, an -bit hash with about plus a few bits, inside a compact hash table. A key that was never inserted is a false positive only if it meets an equal fingerprint where it looks, which happens with probability about per comparison.
A quotient filter (Bender et al., 2012, building on Cleary's compact hash tables of 1984) splits a fingerprint into a -bit quotient, used as the home slot, and an -bit remainder, stored in a slot. Collisions are resolved by linear probing; the remainders of each home slot form a sorted run, and three metadata bits per slot tell the runs apart. A lookup scans one short contiguous region, deletion works, and two filters merge like sorted lists.
A cuckoo filter (Fan, Andersen, Kaminsky & Mitzenmacher, 2014) is a cuckoo hash table of fingerprints with buckets of entries. Key may live in bucket or in . This partial-key cuckoo hashing computes either bucket from the other and the fingerprint alone, so the filter can evict and relocate fingerprints without knowing the keys. A lookup reads two buckets, so , and deletion removes one copy of . Tables fill to about 95% before insertions start to fail, for bits per key, or one bit less with "semi-sorted" buckets; that version beats a Bloom filter below roughly a 3% false-positive rate.
The equations. An xor filter (Graf & Lemire, 2020) is an array of slots of bits. Each key has three slots , one in each third of the array, and the filter must satisfy, for every key of the set,
Peel. Repeatedly take a slot used by exactly one remaining key, push that (key, slot) pair on a stack and delete the key. The keys' slot triples form a random 3-uniform hypergraph, and peeling removes every key with high probability once exceeds the threshold of about ; otherwise, draw new hash functions and retry:
Assign in reverse. Pop the pairs and set each popped slot so that its key's equation holds. That slot belonged to no key still present when its key was peeled, so it occurs in no equation already satisfied, and no later assignment touches a slot of a settled key:
Query. Read three slots and compare their XOR with . For a key outside the set the XOR is unrelated to its fingerprint, so a false positive has probability , at bits per key: within 23% of the lower bound. The price is that the filter is static. Binary fuse filters (Graf & Lemire, 2022) place the three slots in nearby segments, which speeds up construction and brings the space within 13% of the bound.
| Filter | Bits per key | Deletes | Grows after build | Lookup reads |
|---|---|---|---|---|
| Bloom | no | yes | up to bits | |
| Counting Bloom | about | yes | yes | up to counters |
| Quotient | , with at most about | yes | yes; mergeable, resizable | one contiguous run |
| Cuckoo () | , | yes | yes, until about 95% full | 2 buckets |
| Xor | no | no (static) | 3 slots | |
| Binary fuse | about | no | no (static) | 3 slots |
| Lower bound | — | — | — |
Where filters live, and the frontier
Filters earn their keep wherever a quick "no" saves an expensive operation. Bloom's own 1970 example was hyphenation: most words follow simple rules, and a filter of the exceptions decides, without touching the disk, which words need the slow dictionary. Today, log-structured merge (LSM) key-value stores such as LevelDB, RocksDB and Cassandra keep a filter for each sorted file on disk, so a lookup skips every file that cannot contain its key. Web caches have exchanged Bloom filters that summarize their contents, and network devices use filters in packet processing.
The guarantee is for a query fixed in advance. A query that happens to be a false positive fails every time it is repeated, and an attacker who can test queries can collect false positives and replay them. Adaptive filters (Bender, Farach-Colton, Goswami, Johnson, McCauley & Singh, 2018) repair false positives as they are discovered, so the bound holds even for queries chosen after seeing earlier answers. Learned Bloom filters put a trained classifier in front of a small backup filter (Kraska et al., 2018); their guarantees depend on the query distribution (Mitzenmacher, 2018).
Key takeaways
- With ideal hash functions a Bloom filter's false-positive rate is exactly , where counts the set bits; the classic formula is its Jensen lower bound, accurate for large .
- Any approximate-membership structure needs about bits (Carter et al., 1978). Bloom filters use times that; static structures reach times, and dynamic ones provably need a constant factor more (Lovett & Porat, 2010).
- Counting filters add deletions at about four times the space, blocked filters cut each query to one cache miss for a somewhat higher error rate, and double hashing reduces the work to two hash computations.
- Fingerprint filters store about plus a few bits per key: quotient filters (sorted runs, mergeable), cuckoo filters (deletions, two bucket reads) and xor or binary fuse filters (static, within 23% and 13% of the bound).
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
A filter must have false-positive rate over a huge universe. At least how many bits per stored element does any such structure need?
- about 1.44
- about 6
- about 8.7
- about 64
Show the answer
B. about 6
The Carter et al. bound is bits. An optimally tuned Bloom filter needs bits per element; static fingerprint-based structures come close to 6.
A blocked Bloom filter puts all bits of a key into one 512-bit block. At the same total size and , why is its false-positive rate higher than a standard filter's?
- Blocks receive a random, roughly Poisson, number of keys, and overloaded blocks raise the average rate more than underloaded ones lower it
- Bits inside one cache line are correlated in hardware
- It uses fewer hash functions
- It is not higher; only the speed changes
Show the answer
A. Blocks receive a random, roughly Poisson, number of keys, and overloaded blocks raise the average rate more than underloaded ones lower it
The rate is an average of a convex function of each block's load, the same Jensen effect as in the exact analysis. At 10 bits per key and the rate rises from about 0.82% to about 0.96%, and the penalty grows at low error rates, so blocked filters often add a bit or two per key.
You need an approximate-membership filter that supports deletions without multiplying the space by four. Which fits?
- A standard Bloom filter
- An xor filter
- A blocked Bloom filter
- A cuckoo filter
Show the answer
D. A cuckoo filter
A cuckoo filter stores one fingerprint per key in one of two buckets, so deleting a key removes one copy of its fingerprint. Quotient filters also support deletion.
Further reading
- L. Carter, R. Floyd, J. Gill, G. Markowsky & M. Wegman, "Exact and approximate membership testers," Proceedings of the 10th ACM Symposium on Theory of Computing (1978), 59–65.
- P. Bose, H. Guo, E. Kranakis, A. Maheshwari, P. Morin, J. Morrison, M. Smid & Y. Tang, "On the false-positive rate of Bloom filters," Information Processing Letters 108(4) (2008), 210–213.
- B. Fan, D. G. Andersen, M. Kaminsky & M. D. Mitzenmacher, "Cuckoo filter: Practically better than Bloom," Proceedings of ACM CoNEXT (2014), 75–88.
- T. M. Graf & D. Lemire, "Xor filters: Faster and smaller than Bloom and cuckoo filters," ACM Journal of Experimental Algorithmics 25 (2020).
- M. Mitzenmacher & E. Upfal, Probability and Computing, 2nd ed. (Cambridge University Press, 2017), on balls into bins and Bloom filters.