Understanding Bloom Filters
Discover how probabilistic data structures can answer "Does this exist?" using tiny amounts of memory. Learn when approximate answers are better than exact ones.
Imagine checking if a username exists among millions without loading everything into memory. Bloom filters let you say "definitely not here" with certainty, or "maybe here" with some doubt—all while using incredibly little space.
EXERCISE
1The Problem: Checking Millions of Usernames Instantly
Why traditional approaches fail at scale, and what Instagram does differently when recommending content you have never seen.
The Scenario
You are building Instagram. Users watch hundreds of reels daily. Your recommendation engine must never show the same reel twice.
The challenge: For each user, track every reel they have watched. Check this list before recommending.
Scale: 1 billion users × 500 reels/day = 500 billion checks daily.
The Naive Approach: Store Everything in a Set
For each user, maintain a set:
User 12345:
{reel_98234, reel_45123, reel_87234, ...}
Checking before recommending:
- Get user_id
- Load their "watched reels" set into memory
- Check: Is reel_99999 in the set?
- If not present → Recommend it
- If present → Skip it
This works. But...
Why This Approach Fails at Scale
Problem 1: Memory Explosion
Each reel ID = 8 bytes (64-bit integer)
User watches 10,000 reels = 80 KB per user
1 billion users = 80 TB of memory
Cost: Millions of dollars in RAM. Unsustainable.
Problem 2: Disk Storage Limitations
"Just store sets on disk!" you say.
Reality: You cannot check a set on disk efficiently.
Sets are typically implemented as hash tables or binary search trees. To check membership, you must load the entire structure into memory.
Loading 80 KB per user for every recommendation check? Your system crawls to a halt.
The Real-World Constraint
Question Instagram engineers asked:
"Can we trade perfect accuracy for massive space savings?"
Key insight: Once a user watches a reel, they cannot "unwatch" it.
This changes everything.
What We Actually Need
Two guarantees:
- When we say NO (user never watched this): Must be 100% accurate
- When we say YES (user might have watched this): Can be wrong sometimes
Why this trade-off works:
If we accidentally skip recommending one reel out of a million, the user never notices. They still get endless content.
But if we show a reel they definitely never watched, that is always fresh content.
This is where Bloom Filters shine.
Real-World Applications Beyond Instagram
Medium: Track articles you have read. Never recommend the same article twice.
Tinder: Remember profiles you have swiped on. Never show the same person twice.
Web Crawlers: Mark URLs already visited. Avoid crawling the same page repeatedly.
All face the same problem: Millions of items, billions of checks, limited memory.
The Core Question
Can we check "Does X exist in this massive list?" using kilobytes instead of gigabytes?
Yes. But we sacrifice something.
We cannot remove items once added. And sometimes we are wrong (but in a controlled, acceptable way).
This is the trade-off that makes Bloom Filters worth using.
EXERCISE
2The Bloom Filter Mechanism
Understand the elegant simplicity: just bits, hash functions, and probabilistic guarantees.
What Is a Bloom Filter?
Definition: A space-efficient probabilistic data structure that tests whether an element is a member of a set.
Probabilistic = Sometimes wrong, but in a predictable way.
Space-efficient = Uses bits instead of bytes.
The Core Components
1. Bit Array
Instead of storing actual data, we use a simple array of bits (0s and 1s).
Example: 8-bit array (1 byte)
[0, 0, 0, 0, 0, 0, 0, 0]
0 1 2 3 4 5 6 7 (indices)
2. Hash Functions
Functions that convert your data into numbers.
Example: Hash("apple") → 2
We use multiple hash functions for accuracy.
Adding Elements: Step-by-Step
Start with empty bit array:
[0, 0, 0, 0, 0, 0, 0, 0]
Add "apple":
Hash("apple") % 8 = 2
Set bit 2 to 1
[0, 0, 1, 0, 0, 0, 0, 0]
Add "ball":
Hash("ball") % 8 = 6
Set bit 6 to 1
[0, 0, 1, 0, 0, 0, 1, 0]
Add "cat":
Hash("cat") % 8 = 2 (collision!)
Bit 2 already 1, remains 1
[0, 0, 1, 0, 0, 0, 1, 0]
We stored 3 words using just 8 bits.
Checking Membership
Does "dog" exist?
Hash("dog") % 8 = 5
Check bit 5 → 0
Answer: NO (dog definitely not present)
Does "elephant" exist?
Hash("elephant") % 8 = 6
Check bit 6 → 1
Answer: YES (but wait... we never added elephant!)
The False Positive
Bit 6 was set by "ball", not "elephant."
Bloom Filter says: "elephant might be present"
Reality: elephant is not present
This is a false positive.
The Two Rules of Bloom Filters
Rule 1: When Bloom Filter says NO, it is 100% correct.
The element definitely does not exist.
Rule 2: When Bloom Filter says YES, it might be wrong.
The element may or may not exist.
Why This Works for Instagram
Scenario 1: Bloom Filter says "User never watched reel_9999"
→ Recommend it confidently. Guaranteed fresh content.
Scenario 2: Bloom Filter says "User might have watched reel_8888"
→ Skip it. Better safe than showing duplicate.
Impact of false positive: User misses one reel out of millions. No one notices.
Impact of false negative: NONE. Bloom Filters never have false negatives.
Space Savings: The Numbers
Traditional set approach:
3 words: "apple" (5 bytes) + "ball" (4 bytes) + "cat" (3 bytes) = 12 bytes
Bloom Filter approach:
8 bits = 1 byte
12× space reduction (and this scales dramatically with more data)
Real-World Example: Medium
Problem: Track 10 million articles read by 50 million users.
Traditional approach:
Article ID = 8 bytes
10 million articles × 8 bytes = 80 MB per user
50 million users = 4 PB (Petabytes) of memory
Bloom Filter approach:
10 KB per user (tuned for 1% false positive rate)
50 million users = 500 GB of memory
8,000× space reduction.
The Core Insight
We do not store the actual data. We only store evidence of existence using bits.
This is what makes Bloom Filters work at scale.
EXERCISE
3Managing False Positives
Learn how to control accuracy, when to resize, and how major systems tune their Bloom Filters for production.
What Is a False Positive?
Definition: Bloom Filter claims an element exists, but it actually does not.
Example:
Added: "apple", "ball", "cat"
Check: "elephant"
Bloom Filter says: YES (but elephant was never added)
This is a false positive.
Why False Positives Happen
As you add more elements, more bits flip to 1.
Timeline:
Start: [0, 0, 0, 0, 0, 0, 0, 0] (all zeros)
After adding 3 items: [0, 0, 1, 0, 0, 0, 1, 0] (2 bits set)
After adding 10 items: [1, 1, 1, 0, 1, 1, 1, 1] (7 bits set)
After adding 100 items: [1, 1, 1, 1, 1, 1, 1, 1] (all bits set!)
When all bits are 1: Every check returns YES. False positive rate = 100%.
The False Positive Rate Formula
The math (you do not need to memorize this):
p = (1 - e^(-kn/m))^k
Where:
p = false positive probability
k = number of hash functions
n = number of elements inserted
m = size of bit array
Key takeaway: False positive rate increases as you add more elements.
Controlling False Positive Rate
Three levers you can adjust:
1. Increase Bit Array Size (m)
Bigger array = more space for bits = fewer collisions
Example:
8-bit array: False positives after 5 items
80-bit array: False positives after 50 items
800-bit array: False positives after 500 items
Trade-off: More memory usage
2. Use More Hash Functions (k)
More hash functions = more bits set per element = better distribution
Example:
1 hash function: Each element sets 1 bit
3 hash functions: Each element sets 3 bits
5 hash functions: Each element sets 5 bits
Trade-off: Slower insertions and lookups
3. Limit Number of Elements (n)
Know your maximum capacity upfront.
Example:
Plan to store 1 million usernames → Design Bloom Filter for 1 million elements with 1% false positive rate
Calculator tells you: Need 1.2 MB bit array with 7 hash functions
Real-World Configuration: Medium
Requirements:
- Store 10 million article IDs per user
- Tolerate 1% false positive rate (1 in 100 checks wrong)
Configuration:
- Bit array size: 1.44 MB
- Hash functions: 7
- Result: 10 KB per user (after optimization)
This means: Out of 10 million articles, Bloom Filter might incorrectly say "you read this" for 100,000 articles. But users never notice because they still get 9.9 million fresh recommendations.
The Scaling Problem
You cannot resize a Bloom Filter.
Why?
Hash functions depend on array size.
Example:
8-bit array: Hash("apple") % 8 = 2
16-bit array: Hash("apple") % 16 = 10 (different!)
If you resize, all mappings break.
When You Must Recreate
Scenario:
Started with 1 KB Bloom Filter.
Planned for 10,000 items.
Actually inserted 100,000 items.
False positive rate now 80% (unacceptable).
Solution:
- Create new Bloom Filter with 10 KB size
- Iterate through your (source of truth)
- Re-insert all 100,000 items into new filter
- Replace old filter with new one
This is expensive. Better to overprovision from the start.
Best Practices
1. Overprovision Bloom Filter size
If expecting 1 million items, design for 10 million.
2. Monitor false positive rate
Track percentage of "YES" responses. If climbing rapidly, time to resize.
3. Use separate database as source of truth
Bloom Filter is never your primary storage. It is an optimization layer.
4. Choose acceptable false positive rate
- Critical systems: 0.1% (1 in 1,000)
- Recommendation engines: 1% (1 in 100)
- layers: 5% (1 in 20)
Why Overprovisioning Is Worth It
Cost comparison:
1 KB Bloom Filter = $0.000001/user/month
10 KB Bloom Filter = $0.00001/user/month
Traditional set = $0.01/user/month
10× overprovisioning is still 1,000× cheaper than the alternative.
The Core Principle
Bloom Filters save massive amounts of space.
Even with overprovisioning, they remain the most efficient solution for membership checking at scale.
EXERCISE
4Bloom Filters in Production Systems
See how Instagram, Medium, and Tinder use Bloom Filters to handle billions of checks per day.
Redis: Bloom Filters as a Service
Redis offers native Bloom Filter support through the RedisBloom module.
Why Redis?
- No implementation needed: Use as a black box
- Production-ready: Battle-tested at scale
- Simple API: Add, check, done
Basic operations:
BF.ADD user:12345:watched reel_98765
→ Adds reel to user's Bloom Filter
BF.EXISTS user:12345:watched reel_98765
→ Returns 1 (might have watched) or 0 (definitely not watched)
Configuration:
When creating a Bloom Filter, specify:
- Expected number of items
- Desired false positive rate
- calculates optimal size automatically
Instagram: Recommendation Engine
Problem: 2 billion users, each watches 500 reels/day.
Solution: Bloom Filter per user tracking watched content.
Architecture:
User opens Instagram
↓
Recommendation engine generates 100 candidate reels
↓
For each reel: Check Bloom Filter
↓
Filter returns:
- NO (0) → Definitely fresh, include in feed
- YES (1) → Might be duplicate, skip
↓
Show 50 fresh reels
Configuration:
- Bloom Filter size: 5 KB per user
- Stores 10,000 watched reels
- False positive rate: 2%
Impact of false positives:
Out of 100 candidates, 2 fresh reels wrongly marked as "watched."
User still gets 48 fresh reels instead of 50.
No one notices.
Space savings:
Traditional approach: 10,000 reels × 8 bytes = 80 KB per user
Bloom Filter: 5 KB per user
16× reduction
2 billion users × 75 KB saved = 150 TB saved
Medium: Article Recommendations
Problem: 50 million users, 200 million articles.
Solution: Per-user Bloom Filter tracking read articles.
Architecture:
User finishes reading article
↓
BF.ADD user:{id}:read article_{id}
↓
Mark article as read in Bloom Filter (instant)
↓
Background: Log to database (eventual consistency)
Why Bloom Filter first?
- Speed: Instant check (microseconds)
- Database: Slow query (milliseconds)
For recommendations: Check Bloom Filter, not database.
Configuration:
- Expected reads per user: 5,000 articles
- False positive rate: 1%
- Bloom Filter size: 7.2 KB per user
Total memory: 50 million users × 7.2 KB = 360 GB
Versus traditional: 50 million users × 40 KB = 2 TB
5.5× reduction
Tinder: Swipe History
Problem: Show profiles user has not seen before.
Solution: Bloom Filter per user tracking swiped profiles.
Architecture:
User swipes on profile_456
↓
BF.ADD user:123:swiped profile_456
↓
When loading new profiles:
For each candidate profile:
BF.EXISTS user:123:swiped profile_{candidate}
↓
Return only profiles with result = 0 (definitely not seen)
Why false positives are acceptable:
In a pool of 100,000 local profiles, missing 1,000 due to false positives = user still has 99,000 options.
Configuration:
- Expected swipes per user: 10,000
- False positive rate: 3%
- Bloom Filter size: 10 KB per user
Web Crawlers: Avoiding Duplicate Crawls
Problem: Internet has billions of URLs. Crawlers must not visit the same page twice.
Solution: Global Bloom Filter tracking visited URLs.
Architecture:
Crawler finds new URL
↓
BF.EXISTS visited_urls {url}
↓
If 0: Crawl the page, then BF.ADD visited_urls {url}
If 1: Skip (already crawled)
Scale:
- URLs crawled: 10 billion
- Bloom Filter size: 1.44 GB (for 1% false positive)
- Traditional storage: 10 billion × 100 bytes/URL = 1 TB
700× reduction
False positive impact: Crawler skips 1% of pages (100 million). But these might be duplicates anyway, or low-value pages.
Common Pattern Across All Use Cases
1. Bloom Filter is a cache layer
Never the source of truth. Always backed by a database.
2. Check Bloom Filter first
Fast path: If Bloom Filter says NO, done.
Slow path: If Bloom Filter says YES, query database to confirm.
3. Acceptable inaccuracy
All these systems tolerate 1-5% false positives because speed and space savings outweigh occasional misses.
When NOT to Use Bloom Filters
Scenario 1: Financial transactions
Cannot tolerate false positives. Missing a duplicate transaction = losing money.
Scenario 2: Security systems
Cannot afford false negatives. Bloom Filters never have false negatives, but if you need 100% accuracy on positives too, use a database.
Scenario 3: Small datasets
If you have 100 items, just use a hash set. Bloom Filters shine at millions+ scale.
The Production Mindset
Bloom Filters are optimization tools, not silver bullets.
Use them when:
- Scale is massive (millions+ items)
- Speed matters (microsecond checks)
- Occasional false positives are tolerable
They save money, improve performance, and make recommendations fast.
