Want the details?
What is a Bloom filter?
A Bloom filter answers “is this key in the set?” using very little memory. It is a row of bits and a few hash functions. Storing a key switches on the bits its hashes point to. Checking a key looks at the same bits: if any is off, the key is definitely not in the set; if all are on, it may be. It never gives a false “no”, but it can give a false “maybe”, called a false positive.
How to read the bench
| Part on the bench | What it means |
|---|---|
| Input box | The key being stored or checked, and the answer lamps. |
| Hash box | The hash functions; the lit wheels are the ones in use. |
| Row of cells | The bits. Raised and dark: on. |
| Lines | The bits of the current key. Red: a bit that is off, which proves “no”. |
The numbers
- The false “maybe” rate is about (1 − e−kn/m)k, for m bits, n keys and k hash functions. The meters show this next to what the bench measures.
- Measured over 1,000 unknown keys, with 3 hash functions: 5 keys in 32 bits, about 5%; 10 keys, about 26%; 20 keys, about 66%; 10 keys in 64 bits, about 7%.
- The best number of hash functions is about 0.7 × m ÷ n. With 10 bits per key that is 7, and the rate is about 1%. A billion URLs at 1% need about 1.2 GB, as in the web crawler case study.
In real systems
- LSM-tree databases (Cassandra, RocksDB, HBase) keep a Bloom filter per file, so a read skips files that cannot hold the key (see storage engines).
- Crawlers use one to skip URLs they have already fetched; a small share of new pages is missed, which is acceptable.
- Caches and CDNs use them to avoid storing items that were seen only once.
- Limits: you cannot delete a key (switching its bits off could remove other keys too), and you cannot list the keys. A counting Bloom filter keeps small counters instead of bits to allow deletes; a cuckoo filter is a newer option.
Words to know
- Bloom Filter
- A bit array with k hash functions that answers “definitely not” or “maybe”.
- False Positive
- A “maybe” for a key that was never stored.
- False Negative
- A “no” for a key that was stored. A Bloom filter never gives one.
- Hash Function
- A function that turns a key into a number, the same one every time.