Part 7 of 12 · Data Structures

How do you check "have we seen this?" for millions of items cheaply?

An exact set of a million emails took 56 MB; a Bloom filter answering the same question with 1% false positives took 1.2 MB. Bloom vs cuckoo vs an exact set, measured: memory, false positives, deletes, and what happens when you overfill them.

A painting of an old earthenware colander full of small holes, a picture of a Bloom filter

The verdict, which the measurements below will earn: if you can tolerate an occasional “maybe” for an item you’ve never seen, use a Bloom filter. It never says “no” to something it has seen, and it costs about 10 bits per item for a 1% false-positive rate. A million items took 1.2 MB instead of the 56 MB of an exact hash set. If you also need to delete items, use a cuckoo filter. If a wrong “yes” is never acceptable, keep the exact set, or use the filter only to skip work and confirm against the real data.

The case is a familiar one. A question as small as “have we seen this before?” asked a million times, and the obvious suspect, an exact set, quietly eating memory. I’ve loved Bloom filters since I first met one, because they crack this case in such an unexpected way: they answer the question by deliberately forgetting almost everything about the items. The trade is a good one: a little uncertainty in one direction, in exchange for a fraction of the memory. Let’s measure how good.

The code is in systems-data-structures (Go, standard library only). make demo reruns every number.

Case file

The case “Have we seen this?” for a million items eats memory
Suspects An exact hash set
Culprit Storing every item to answer a yes/no question
The fix Bloom filter at ~1%, or a cuckoo filter when items are deleted
Evidence Go, standard library only, make demo, 1,000,000 items

The two suspects, in two sentences each #

  • Bloom filter (Burton Bloom, 1970): a bit array and k hash functions. Adding an item sets k bits; checking it asks whether all k are set. If any is 0, the item was never added. If all are 1, it probably was, unless other items happened to set those bits (a false positive).
  • Cuckoo filter (Fan et al., 2014): a table of short fingerprints (16 bits here), where each item can live in one of two buckets. A lookup checks two buckets. A delete removes the fingerprint, which a Bloom filter can’t do, because its bits are shared between items.

The first clue: a million items #

1,000,000 items added (strings like user123@example.com), then 1,000,000 different items checked:

Structure Memory Bits per item False positives Insert Lookup Deletes?
Exact hash set (Go map) 55.9 MB* 447 0 102 ns 80 ns yes
Bloom, sized for 1% (k = 7) 1.2 MB 9.6 1.00% 98 ns 73 ns no
Bloom, sized for 0.1% (k = 10) 1.8 MB 14.4 0.10% 129 ns 72 ns no
Bloom, sized for 0.01% (k = 13) 2.4 MB 19.2 0.008% 159 ns 74 ns no
Cuckoo, 16-bit fingerprints, 95% full 2.1 MB 16.8 0.012% 79 ns 47 ns yes
Chart: Memory for 1,000,000 items (MB)Chart: Memory for 1,000,000 items (MB)
Diagram source
xychart-beta
  title "Memory for 1,000,000 items (MB)"
  x-axis ["Exact set", "Bloom 1%", "Bloom 0.1%", "Bloom 0.01%", "Cuckoo"]
  y-axis "MB"
  bar [55.9 , 1.2 , 1.8 , 2.4 , 2.1]

* The map’s own memory, not counting the strings. If the strings aren’t already in memory for another reason, add them too.

What the evidence tells us:

  • The size depends on the error rate, not the item size. This is the part worth admiring. A Bloom filter stores no part of the item. Emails, URLs and 2 KB documents all cost the same ~9.6 bits each at 1%. Each 10× better error rate costs about 4.8 more bits per item: m/n = -ln(p) / (ln 2)².
  • The measured rates matched the formula: 1.0007% for 1%, 0.1013% for 0.1%. Theory and the stopwatch agree, which is always a good sign in an investigation.
  • At very low error rates a cuckoo filter is smaller. Its paper reports cuckoo filters (with their space optimisations) using less space than Bloom filters below about 3%. My simple version has none of those optimisations, and still used 16.8 bits for 0.012%, where a Bloom filter needs about 18.8 bits. It also had the fastest lookups (only two places to check), and it supports deletes.

The second clue: what happens when you put in more than you sized for #

Filter Sized for Items added False positives
Bloom, 1% 1,000,000 1,000,000 1.0%
Bloom, 1% 1,000,000 2,000,000 15.7%
Bloom, 1% 1,000,000 4,000,000 67.8%
Cuckoo, 1,048,576 slots - first insert refused at 1,004,245 items (95.8% full) -

The two designs disagree about how to fail, and both choices are thoughtful. A Bloom filter degrades silently: at twice its planned size, one lookup in six for a new item wrongly says “seen”. A cuckoo filter fails loudly: it refuses the insert, which you can catch and handle by building a bigger one. Either way, size for growth, watch the count, and rebuild before you pass the plan.

Case 1: “is this email already registered?” #

Situation: sign-up must reject duplicate emails, at high volume. A false positive here means telling a real new user their email is taken, which isn’t acceptable.

The false lead is to let the filter decide. What I’d use instead: the database’s unique index is the answer; it’s exact and it’s the only thing that’s safe against two sign-ups racing. A filter can sit in front of it as a cheap first check, only for the “no” answer:

  • “definitely not seen” → skip the lookup, insert, and let the unique index catch the rare race;
  • “maybe seen” → check the database.
Flowchart: New sign-up email, Bloom filter: seen?, Skip the lookup, insert, Check the database, Unique index catches the rare raceFlowchart: New sign-up email, Bloom filter: seen?, Skip the lookup, insert, Check the database, Unique index catches the rare race
Diagram source
flowchart TD
  A["New sign-up email"] --> B{"Bloom filter: seen?"}
  B -->|definitely not| C["Skip the lookup, insert"]
  B -->|maybe| D["Check the database"]
  C --> E["Unique index catches the rare race"]

That saves a query for most new sign-ups, at 1.2 MB of memory per million users. It’s worth it only if that query is a real cost.

Case 2: a crawler that shouldn’t fetch the same URL twice #

Situation: billions of URLs. An exact set would need tens of gigabytes; a false positive means skipping a URL you haven’t actually crawled.

What I’d use: a Bloom filter sized for the expected count at around 1%. Skipping 1 URL in 100 is usually fine (many pages are linked from several places), and the memory saving is two orders of magnitude. Partition it by host if it gets too big to rebuild in one piece.

Case 3: skipping disk reads #

Situation: a key-value store that keeps data in many files on disk (an LSM tree; see the B-tree vs LSM post). To find a key, it may have to look in every file.

What they use: a Bloom filter per file, kept in memory. RocksDB, LevelDB and Cassandra all do this. Before reading a file, the store asks the file’s filter; a “no” skips that file’s disk read entirely. A false positive costs one wasted read, not a wrong answer, because the read then finds nothing. This is the textbook use, and the most elegant: the filter saves work, and the real data stays the source of truth. A wrong answer from the filter costs almost nothing, which is exactly the situation it was born for.

Case 4: sets that change #

Situation: “users who are currently online”, “tokens revoked in the last hour”, “jobs in progress”: items come and go.

What I’d use: a cuckoo filter, because a Bloom filter can’t delete. Clearing one item’s bits would clear bits other items share. Only delete items you added: deleting an item that merely collides with another removes that other item’s fingerprint, creating a false negative. If deletes are rare, rebuilding a Bloom filter periodically from the source also works. For a time window, keep one filter per window and drop old ones.

The fine print people forget #

Every bit of that compactness comes from not storing the items, so the limits follow naturally.

  • You can’t list or count what’s in a filter. It only answers “seen?”. Keep the source of truth somewhere.
  • You can’t resize a Bloom filter. Rebuild it from the source, or chain a second filter when the first is full (scalable Bloom filters).
  • The hash function matters. Similar inputs must spread out. The demo uses FNV-1a with a final mixing step; weak hashing silently raises the false-positive rate.
  • Off-the-shelf beats hand-written: RedisBloom (BF.ADD, CF.ADD; part of Redis Stack and Redis 8), or a maintained library in your language.

Remember this

  • A Bloom filter never says “no” to something it has seen.
  • About 10 bits per item buys a 1% false-positive rate, whatever the item size.
  • Use a filter to skip work, never as the final answer where a false positive hurts.

What to do on Monday #

  1. Exact first. If the set fits in memory, or the database can answer it with an index, do that. Filters are for when it doesn’t fit, or when the check itself is the expensive part.
  2. Use a filter to skip work, never as the final answer where a false positive hurts.
  3. Bloom at ~1% for append-only sets; cuckoo when items are removed or you need very low error rates; size both for growth.
  4. Monitor the item count against the planned size, and rebuild before you pass it.

What I like most about these filters is the honesty of their answers: “definitely not” or “maybe”. Used where a “maybe” only means a little extra work, a few bits per item can save you a great deal. For where they earn their keep in a storage engine, the B-tree vs LSM post is the next case to read.

Measured Nov 2026 with Go 1.27 on a laptop (Intel i7-8550U), 1,000,000 items. False-positive rates and bits per item follow from the algorithms and repeat; nanoseconds depend on the machine. Code and full results: systems-data-structures.

Like (sign in first) 0 likes 💬 0

Written by

Vishal Kumar

Associate Director at CapGrid, running product and engineering in a CPTO capacity. 10+ years building distributed, multi-tenant SaaS and AI platforms. AWS Certified Solutions Architect - Professional. Based in Gurugram.

Comments

No comments yet.

Sign in to comment. We'll email you a link; no password needed.