← Back to System Design, page 2

system-design-0032 · Medium mid

What is a Bloom filter, how do you size it, and where is it used in system design?

A Bloom filter is a compact bit array with k hash functions that answers "definitely not in the set" or "probably in the set". It has no false negatives but a tunable false positive rate, and plain Bloom filters do not support deletes. For n items and false positive rate p, use about m = -n ln p / (ln 2)^2 bits and k = (m / n) ln 2 hash functions.

Full answer

How it works

Start with an array of m bits, all 0, and k independent hash functions.

  • Add an item: compute its k hashes, set those k bit positions to 1.
  • Check an item: compute its k positions. If any bit is 0, the item was never added (definitely not present). If all are 1, it was probably added, but other items may have set those bits (a false positive).

You cannot remove an item by clearing its bits, because other items share them. Counting Bloom filters (small counters instead of bits) or cuckoo filters support deletion.

Sizing

For n items and a target false positive rate p:

  • bits: m = -n x ln(p) / (ln 2)^2
  • hash functions: k = (m / n) x ln 2

Example: 1 million usernames with p = 1%:

  • m = 1,000,000 x 4.605 / 0.4805 = about 9.59 million bits = about 1.2 MB
  • k = 9.59 x 0.693 = about 6.6, so use 7 hash functions

That is about 9.6 bits per item regardless of how long the usernames are. Storing the usernames themselves in a hash set would take many times more memory.

Where it is used

  • LSM-tree databases (Cassandra, RocksDB, HBase): each on-disk file has a Bloom filter, so a read skips files that definitely do not contain the key, saving disk reads.
  • Cache protection: check a filter of existing IDs before querying the database, so requests for non-existent keys (including attacks with random IDs) do not hit the database.
  • Username or short-code availability: a fast "definitely free" check before the authoritative database check.
  • Web crawlers: "have we seen this URL?" over billions of URLs.
  • CDNs: only cache an object on its second request ("one-hit wonders" skip the cache) using a filter of seen objects.

Trade-offs

  • False positives are acceptable only when a positive is followed by an exact check or when an occasional wrong "yes" is harmless.
  • The filter must be sized for the final n; inserting more items than planned raises p sharply. Scalable Bloom filters add new layers as they fill.
  • Rebuilding is needed for deletes in the plain version.

By level

Fresher: Explain the bit array and hashes, and that the answer is "definitely not" or "maybe yes". Saying that it can wrongly say yes but never wrongly say no is the key point.

Mid: Size a filter for given n and p, explain false positive implications and where it saves database reads. Expect the sizing arithmetic for a given item count.

Senior: Discuss distributed and scalable filters, rebuild strategies, memory versus accuracy trade-offs at scale, and alternatives such as cuckoo filters and quotient filters. The senior answer weighs memory against accuracy at large scale.

Key points

  • Answers definitely-not or probably-yes; no false negatives
  • m = -n ln p / (ln 2)^2 and k = (m/n) ln 2
  • About 9.6 bits per item for 1% false positives
  • Plain Bloom filters cannot delete; counting or cuckoo filters can
  • Used in LSM databases, crawlers, cache protection and CDNs

Common mistakes

  • Claiming Bloom filters can have false negatives
  • Trying to delete by clearing bits
  • Overfilling a filter sized for fewer items
  • Using it where a false positive causes real harm without a follow-up check

Follow-ups

  • How would you use a Bloom filter to speed up a URL shortener's custom alias check?
  • Why do LSM-tree reads benefit so much from Bloom filters?
  • How does a cuckoo filter differ?

Tips

  • Explain add and check with a small bit array
  • Do the sizing arithmetic for one million items
  • Name one database that uses Bloom filters internally

Evaluation rubric

  • Correct mechanism and error types
  • Correct sizing formulas and arithmetic
  • Real use cases
  • Discusses deletion and overfilling limits