System design lab · Episode 18

Bloom filter: “definitely not” or “maybe”

A web crawler has seen a billion addresses; a database wants to skip files that do not hold a key. Keeping every key costs a lot of memory. A Bloom filter keeps only a row of bits, and answers one question: “have I seen this?” with “definitely not” or “maybe”. Follow the lessons next to the picture.

  1. Episode 1Load balancer
  2. Episode 2Cache
  3. Episode 3Rate limiter
  4. Episode 4Message queue
  5. Episode 5Database index
  6. Episode 6Replication and consensus
  7. Episode 7Circuit breaker
  8. Episode 8Backpressure
  9. Episode 9Retries and jitter
  10. Episode 10Consistent hashing
  11. Episode 11Partitioned log
  12. Episode 12Write-ahead log
  13. Episode 13Quorums
  14. Episode 14Unique IDs
  15. Episode 15The last ticket
  16. Episode 16Saga
  17. Episode 17Cache problems
  18. Episode 18Bloom filter
MAYBENO08162432404856
Dark raised cell: a bit that is on. Red line: a bit that is off, so the answer is noInput at the left, the hash box, then the row of bits

Controls

Number of bits
Hash functions per key
Keys to store
Time

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 benchWhat it means
Input boxThe key being stored or checked, and the answer lamps.
Hash boxThe hash functions; the lit wheels are the ones in use.
Row of cellsThe bits. Raised and dark: on.
LinesThe 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.