Skip to content
derpx06.dev
WritingExplainersNotesAbout
works on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisationworks on my machineit's always DNSship itLGTMTODO: sleepoff by oneread the docsgit push --force-with-lease404: sleep not foundhave you tried turning it off and on againrubber duck says: explain itpremature optimisation

Learning in public.

A student's notebook on system design, distributed systems and databases. Shared as I learn.

ReadWritingExplainersNotesAbout
ElsewhereGitHubLinkedInHugging FaceLeetCode
Say hellomanaskhairnar1511@gmail.com
derpx06
NORMALmain© 2026 Derpx06Pune, Indiatop ↑
Blog/Databases

What is a Bloom filter? A tiny "definitely not" checker

A tiny bit array that answers 'definitely not' or 'probably yes'. Here is why databases use it to skip slow disk reads.

Derpx0616 Nov · 5 min read
Contents
What are the only two answers it gives?How do you build one?Why is "no" always right but "yes" isn't?How often is it wrong?What can't it do?Where do databases use it?Why is this trick useful?Sources
0% read · 5 min leftBack to top ↑
On this page0%
Contents
What are the only two answers it gives?How do you build one?Why is "no" always right but "yes" isn't?How often is it wrong?What can't it do?Where do databases use it?Why is this trick useful?Sources

Is "apple" in a list of 10 million words? A Bloom filter can answer in a few memory lookups, using under 10 bits per word. The catch: it's allowed to be wrong, but only in one direction.

That is the whole deal, and it is simpler than it sounds. A Bloom filter is a tiny memory-saver that answers one question: "have I seen this thing before?" Burton Bloom described it in 1970, in a paper called Space/Time Trade-offs in Hash Coding with Allowable Errors. The title names the trade: give up a bit of accuracy, get a lot of space back.

What are the only two answers it gives?

Definitely not

The thing was never added. This answer is always right.

Probably yes

The thing was maybe added, or maybe it's a false alarm. This answer can be wrong.

So "no" is a promise, and "yes" only means "maybe, go check."

Plain listKeep every name in a big list. Exact answers, but it eats your memory.
→
Bloom filterKeep a tiny bit array. 'No' is certain, 'yes' is only probable.

But how can something that stores no words answer anything? Let's build one.

How do you build one?

Two parts. A row of bits (tiny switches, each 0 or 1), all starting at 0. And a few hash functions: recipes that turn any word into a position number in that row.

Say the row has 16 bits and we use 3 hash functions. To add "apple", run it through all 3 recipes. Say they give positions 2, 7 and 11. Flip those three bits to 1.

Click Next to see "apple" get added, then looked up, then a different word ("banana") get checked.

Word
Hash 1
Hash 2
Hash 3
Bit row

Add apple

Step 1 of 5
The numbers 2, 7, 11 are made up for illustration; real hash functions give whatever positions they give.

Notice that nothing is stored except those switches. The word "apple" itself is gone. The positions 2 and 11 in the banana check are also made up, just to show an overlap.

The filter only needs a row of bits, not the words. So why is a 0 a promise but a 1 only a "maybe"?

Why is "no" always right but "yes" isn't?

If a word had been added, its bits would have been flipped to 1. A bit never goes back to 0, so seeing a 0 means the word was never added.

All 1s is only a "probably" because other words may have flipped those same bits by accident. The filter cannot tell whose bits they are.

So how often is a "yes" wrong? There is a formula for that.

How often is it wrong?

Say the row has mmm bits, you've added nnn words, and you use kkk hash functions. The chance of a false alarm is about

ε≈(1−e−kn/m)k\varepsilon \approx \left(1 - e^{-kn/m}\right)^kε≈(1−e−kn/m)k

This can be checked against the exact version, (1−(1−1/m)kn)k\left(1 - (1 - 1/m)^{kn}\right)^k(1−(1−1/m)kn)k, and for big mmm they match closely. With m=10,000m = 10{,}000m=10,000, n=1,000n = 1{,}000n=1,000 and k=7k = 7k=7, the result is about 0.8%.

The best number of hash functions is

k=mnln⁡2k = \frac{m}{n} \ln 2k=nm​ln2

Here is the tricky part. More hash functions help you spot a missing word faster, but they also fill the row with 1s faster. So there's a sweet spot, and that formula is it.

A handy rule of thumb

About 9.6 bits per word gives a 1% false alarm rate, and the best kkk is then about 7. That's under 10 bits per word, no matter how long the words themselves are.

10% false alarms4.8 bits per item
1%9.6 bits per item
0.1%14.4 bits per item
Space needed per item for each false alarm rate, using the formula m/n ≈ -2.08 ln(ε)

Each time you cut false alarms by 10 times, you pay only about 4.8 more bits per item. That is cheap. But there are things a Bloom filter cannot do.

What can't it do?

No deleting, and no listing

You can't remove a word. Clearing a bit might break other words that share it, and then you'd get wrong "definitely not" answers. Variants called counting Bloom filters can delete, but they use roughly 3 to 4 times more space. And you can't get the words back out, because they were never stored.

Also, if you keep adding words to a row that's too small, it fills with 1s and nearly everything becomes "probably yes". The filter does not break, it just stops being useful. Size it for the number of words you expect.

So where is this actually worth it?

Where do databases use it?

Many databases keep data in files on disk, and disk is slow. Looking for a key that isn't there is the worst case, because you'd have to check file after file to be sure.

The Bigtable paper from Google describes this exact fix. A Bloom filter for each file lets the database ask whether the file might hold a row, and the paper reports that most lookups for non-existent rows or columns then don't need to touch disk. Bigtable stores data in files called SSTables, the same file style used by LSM-tree stores.

1Ask the filter (in memory)
2'Definitely not' means skip the disk
3'Probably yes' means read the file
A false alarm only costs one wasted disk read. It never gives a wrong answer.

That is the nice part. A false alarm just means a bit of extra work, since the real file is still checked afterwards. And the "no" answers, the ones you can trust, save the most work.

QuestionPlain list of keysBloom filter
Is it definitely missing?Yes, exactYes, exact
Is it definitely there?Yes, exactOnly "probably"
Memory usedGrows with key lengthA few bits per key
Remove a key?YesNo (basic version)

Other systems the Wikipedia article lists as users: Apache HBase, Apache Cassandra, ScyllaDB and PostgreSQL.

Why is this trick useful?

You trade a tiny, controlled chance of being wrong for a huge saving in memory. And the trade is safe because the wrong answer is always the harmless kind.

Think of it like this

A guard holds a short, rough guest list. If the guard answers "you are not on it", you are definitely not on it. If the guard answers "probably", someone still checks the full list.

Key takeaways
  • A Bloom filter answers "definitely not" or "probably yes". The "no" is always right.
  • It is a row of bits plus a few hash functions. Adding flips bits to 1, checking looks at them.
  • About 9.6 bits per item gives roughly a 1% false alarm rate, with about 7 hash functions.
  • Databases keep one per data file so most lookups for missing keys skip the disk.

Sources

  • Bloom — Space/Time Trade-offs in Hash Coding with Allowable Errors, Communications of the ACM, 1970
  • Chang et al. — Bigtable: A Distributed Storage System for Structured Data, OSDI 2006
  • Bloom filter, Wikipedia (formulas, space figures, list of databases)
ShareXLinkedInEmail
Written by Derpx06A developer who builds AI search tools and the code around them. I explain what I got wrong first, so you can skip it. Spotted a mistake? Tell me — corrections get credited.
Databases · 4 minWhat really happens when you delete a file or a database row?Databases · 4 minB-Trees vs LSM-Trees: how databases store your dataDatabases · 5 minHow does a database keep your data when the power goes off?
Start here
01Why is 0.1 + 0.2 not 0.3 in code?02How does Git work inside? (Snapshots, not changes)03How does your computer know the time? (And why it can be wrong)