SkillSprout
Chapter 27 · Lesson 2
Intermediate
4 min read

Sizing, Variants and Real Uses

Overview

A Bloom filter is only useful if you tune it properly and place it in the right spot. This lesson covers how big to make it, what to do when you need to delete items, and the places it shows up in real systems.

Detailed Notes

What is sizing

Sizing means choosing how many switches (bits) and how many hash functions to use. Three numbers are linked:

  • n: how many items you expect to add.
  • m: how many bits the filter has.
  • p: the false-positive rate you can accept (for example 1%).

More bits means fewer false positives but more memory. As a rule of thumb, about 10 bits per item gives roughly a 1% false-positive rate, and every extra ~5 bits per item cuts the rate by about 10x further. About 7 hash functions is a good fit at that size.

The problem it solves

If the filter is too small for the number of items, almost every switch ends up on and it answers "probably yes" for everything. It becomes useless. If it is far too big, you waste memory. Sizing for your expected item count and target error keeps it effective.

How it works

Pick the false-positive rate you can live with, estimate n, and calculate m and the number of hash functions from standard formulas (libraries do this for you). Remember these properties:

  • Filters cannot grow after creation. If you add far more than n items, the error rate climbs. Teams rebuild a bigger filter, or chain several ("scalable" filters).
  • A standard filter cannot delete. Clearing a switch might erase evidence of other items.

Variants solve the deletion issue:

  • Counting Bloom filter: each position holds a small counter instead of an on/off bit. Adding increases the counters, removing decreases them. It needs about 3-4 times more memory.
  • Cuckoo filter: stores short fingerprints of items in a table. It supports deletion and can use less space than a Bloom filter at low error rates.

A simple example

A spam filter must remember 10 million known-bad email addresses. A plain set might need hundreds of megabytes. A Bloom filter at 1% error needs about 12 MB. When a new email arrives, "definitely not bad" lets it through quickly, and "maybe bad" triggers a precise check.

System design example

  • Databases: a filter per data file avoids useless disk reads, as in the earlier example.
  • Web crawlers: "have I already crawled this URL?" across billions of URLs. A false positive just means skipping a rarely missed page.
  • Cache protection: put a filter of valid IDs in front of the cache and database. Requests for IDs that do not exist (often an attack or bug) are rejected instantly instead of hitting the database every time, because a cache cannot store a "miss" for every made-up ID.
  • Browsers and CDNs: quickly check "is this URL on a malicious list?" or "have we cached this object?" before doing heavier work.

Avoid Bloom filters when a wrong "yes" is unacceptable, such as checking whether a payment was already processed. There, use an exact check.

Summary

  • Size the filter for expected item count and acceptable error; about 10 bits per item gives roughly 1%.
  • Standard filters cannot delete or grow; counting and cuckoo filters support deletion.
  • It shines when most lookups are for absent items.
  • Use it in front of disks, caches, databases and crawlers.
  • Do not use it where exact answers are mandatory.

Architecture Diagram

Filter as a front door to cache and database
Architecture
Clients
API
Bloom Filtervalid ids
Cache
Database

Impossible requests are rejected before they cost anything.

  1. 1A client requests an item by id.
  2. 2The filter rejects ids that are definitely invalid.
  3. 3Possibly valid ids check the cache.
  4. 4Cache misses fall through to the database.

Common Interview Questions

Chapter quiz

Bloom Filters · 5 questions

  1. 1.A Bloom filter says 'definitely not in the set'. What does that mean?

  2. 2.Which error can a standard Bloom filter produce?

  3. 3.What is the main benefit of a Bloom filter?

  4. 4.Which filter type supports removing items?

  5. 5.Where is a Bloom filter a poor choice?

0 of 5 answered

Finished this lesson?

0 of 91 lessons done · 0% of the course