Bloom Filter

Designing the class: config, hashing, bits, locks, bytes

Design a Bloom filter as a reusable class: sized from an item count and a false positive rate, with pluggable hashing, a compact bit array, thread-safe adds, merging and a byte format.

Overview

A Bloom filter answers 'have I seen this item?' with a certain no or a probable yes, using a bit array instead of storing the items. Adding an item sets k bits chosen by hashing it; a lookup checks those same k bits. If any is 0, the item was never added. If all are 1, it probably was, but other items may have set those bits between them. So there are false positives, and never false negatives, as long as no bit is ever lost.

As a low level design question, the math is the easy part; the interviewer wants a class other teams can use safely. That means five decisions: how callers size it (they know an item count and an error rate, not bits), where the hash functions come from, how the bits are stored, what happens when two threads add at once, and how a filter is saved, loaded and merged. Each decision is one small class.

Scoping the problem

Ask these questions before you write code; each answer changes the design. Each comes with the answer most interviewers accept.

  • How many items, and what false positive rate is acceptable? This sets the size m and the hash count k. For example, 1,000 items at 1%.
  • Must items be removable? A plain bit filter cannot forget. Usually no; removal is a follow-up (a counting filter).
  • What if more items arrive than planned? The error rate climbs past the target. Report it; a scalable filter is a follow-up.
  • Many threads, or one? A lost bit update is a false negative. Usually many threads add and check.
  • Saved to disk or sent between services? Then it needs a byte format that records m, k and the hash scheme. Usually yes.
  • Built in parts and combined? A union needs the same m, k and hashing. Usually yes: shards are merged.

Functional requirements

  • BloomConfig.for_items(n, p) computes bits m and hashes k; it refuses n <= 0 or p outside (0, 1)
  • add(item) records an item; might_contain(item) returns False only if the item was never added
  • expected_fp_rate() from the item count and observed_fp_rate() from the bits set
  • union(other) merges two filters with the same m, k and hash scheme, and refuses otherwise
  • to_bytes() and from_bytes() save and load a filter with a versioned header; a wrong header or short body is refused
  • Removal is not supported; say so (see the counting filter follow-up)

Non-functional requirements

  • add and might_contain are O(k); memory is m bits plus a small header, whatever the items are
  • Positions are the same on every machine and every run, so saved filters stay valid
  • Safe with many threads: bit updates happen under one lock, so no bit is lost
  • The hash scheme is a strategy object; replacing it does not change callers

Interview questions

  • Why does add need a lock? Setting a bit sounds atomic.
  • How would you support removing an item?
  • What happens when more items arrive than the filter was sized for?
  • Two services each built a filter. How do you combine them?
  • Why not use Python's built-in hash() for the positions?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio