Bloom Filters

μž‘μ„±μž

μΉ΄ν…Œκ³ λ¦¬:

← ν”Όλ“œλ‘œ
DEV Community · Gouranga Das Samrat · 2026-08-16 개발(SW)
Cover image for Bloom Filters

Gouranga Das Samrat

One-liner: A probabilistic data structure that tells you if an element is definitely not in a set, or possibly in a set β€” using very little memory.

πŸ“Œ The Problem

You have 1 billion URLs in a database. Before adding a new URL, you want to check if it already exists.

Naive approach: Query the database every time.

  • Cost: 1 DB query per URL check β†’ slow, expensive

Bloom Filter approach: Check the bloom filter first (microseconds, no DB hit).

  • If filter says NO β†’ URL definitely not in DB β†’ safe to insert
  • If filter says YES β†’ URL might be in DB β†’ query DB to confirm

πŸ’‘ How Bloom Filters Work

Structure

An array of m bits, all initialized to 0.

Bit array: [0, 0, 0, 0, 0, 0, 0, 0, 0, 0]  (m=10 bits)
            0  1  2  3  4  5  6  7  8  9

Enter fullscreen mode Exit fullscreen mode

Insertion (k hash functions)

To insert element x:

  1. Hash x with k different hash functions
  2. Each hash function returns a position in the bit array
  3. Set those positions to 1
Insert "google.com":
  hash1("google.com") = 2 β†’ bit[2] = 1
  hash2("google.com") = 5 β†’ bit[5] = 1
  hash3("google.com") = 8 β†’ bit[8] = 1

Bit array: [0, 0, 1, 0, 0, 1, 0, 0, 1, 0]

Enter fullscreen mode Exit fullscreen mode

Lookup

To check if y is in the set:

  1. Hash y with the same k functions
  2. Check if ALL those positions are 1
Check "google.com":
  hash1 β†’ bit[2] = 1 βœ…
  hash2 β†’ bit[5] = 1 βœ…
  hash3 β†’ bit[8] = 1 βœ…
  β†’ POSSIBLY in set (correct! it was inserted)

Check "bing.com":
  hash1 β†’ bit[1] = 0 ❌
  β†’ DEFINITELY NOT in set (100% accurate)

Enter fullscreen mode Exit fullscreen mode

⚠️ False Positives (Not False Negatives)

False Positive Example

Insert "amazon.com":
  hash1 = 2, hash2 = 7, hash3 = 3

Bit array (after inserting google.com + amazon.com):
[0, 0, 1, 1, 0, 1, 0, 1, 1, 0]

Check "yahoo.com":
  hash1("yahoo.com") = 2 β†’ bit[2] = 1 βœ…
  hash2("yahoo.com") = 7 β†’ bit[7] = 1 βœ…
  hash3("yahoo.com") = 3 β†’ bit[3] = 1 βœ…
  β†’ "POSSIBLY in set" ← FALSE POSITIVE! (yahoo.com was never inserted)

Enter fullscreen mode Exit fullscreen mode

Bits were set by OTHER elements, creating a false positive.

Key Guarantees

Result Guarantee DEFINITELY NOT in set 100% accurate (no false negatives) POSSIBLY in set Might be wrong (false positives possible) Cannot delete Once a bit is set, you can’t safely unset it

πŸ“Š Tuning Bloom Filters

Two parameters control accuracy:

  • m = size of bit array (more bits β†’ fewer false positives)
  • k = number of hash functions (optimal k depends on m and n)
  • n = number of expected elements

False positive rate formula:

p β‰ˆ (1 - e^(-kn/m))^k

Enter fullscreen mode Exit fullscreen mode

Practical sizing:

1% false positive rate β†’ ~10 bits per element
0.1% false positive rate β†’ ~15 bits per element

For 1 billion URLs, 1% FP rate:
β†’ 10 Γ— 1B bits = 10 billion bits = 1.25 GB
vs
β†’ Storing 1B URLs as strings = ~50-100 GB
β†’ 40-80Γ— memory savings!

Enter fullscreen mode Exit fullscreen mode

🌍 Real-World Use Cases

System Use Case Google Chrome Malicious URL check (local bloom filter) Apache Cassandra Skip SSTables that don’t contain a key Bitcoin SPV wallets filter transactions Akamai CDN Avoid caching one-hit-wonder URLs Medium “Have you seen this article?” check Email spam filters Fast first-pass spam detection HBase/BigTable Reduce disk reads for non-existent rows

Example: Web Crawler (Avoid Revisiting URLs)

from pybloom_live import BloomFilter

bf = BloomFilter(capacity=1_000_000_000, error_rate=0.01)

def should_crawl(url):
    if url in bf:
        return False  # probably already crawled (or false positive)
    bf.add(url)
    return True  # definitely not crawled yet

Enter fullscreen mode Exit fullscreen mode

Example: Username Availability

User types username "rahul123"
β†’ Check bloom filter (microseconds)
β†’ Filter says NO β†’ username definitely available βœ… (no DB query needed)
β†’ Filter says YES β†’ query DB to confirm (might be false positive)

Enter fullscreen mode Exit fullscreen mode

πŸ†š Bloom Filter vs Hash Set

Feature Bloom Filter Hash Set Memory Very small (bits) Large (full values) Lookup O(k) very fast O(1) fast False positives Possible Never False negatives Never Never Deletion Not supported Supported Count elements No Yes

πŸ”„ Variants

Variant Feature Counting Bloom Filter Supports deletions (use counters instead of bits) Scalable Bloom Filter Grows dynamically as elements are added Cuckoo Filter Supports deletion, similar performance

🎨 Diagram

The diagram shows:

  • Bit array with positions labeled
  • Three hash functions pointing to different positions
  • Insertion of “google.com” setting 3 bits
  • Lookup showing definite miss vs possible hit
  • False positive scenario with two elements colliding

πŸ”‘ Key Takeaways

  • Bloom filters use tiny memory to check set membership
  • No false negatives β€” if it says NO, it’s definitely NO
  • False positives possible β€” if it says YES, verify with DB
  • Perfect for pre-filtering expensive DB/disk lookups
  • Tune with bits-per-element to control false positive rate

μ›λ¬Έμ—μ„œ 계속 β†—

μΆ”μΆœ λ³Έλ¬Έ Β· 좜처: dev.to Β· https://dev.to/gouranga-das-khulna/bloom-filters-4j30