Bloom Filter Simulator

Runs in browser

Visualize how probabilistic data structures efficiently test for set membership

Configuration

How to Use

Add items and check their existence.

You will see:

  • Multiple bits set per item (k hashes)
  • False Positives (Maybe Present)
  • Definite Negatives (Not Present)

Bloom Filter Visualization

Add Element

Check Existence

Bit Array (m=50)

0
0
1
0
2
0
3
0
4
0
5
0
6
0
7
0
8
0
9
0
10
0
11
0
12
0
13
0
14
0
15
0
16
0
17
0
18
0
19
0
20
0
21
0
22
0
23
0
24
0
25
0
26
0
27
0
28
0
29
0
30
0
31
0
32
0
33
0
34
0
35
0
36
0
37
0
38
0
39
0
40
0
41
0
42
0
43
0
44
0
45
0
46
0
47
0
48
0
49
0
Empty (0)
Set (1)
Last Hashed

The Definitive Guide to Bloom Filters

A Bloom Filter is a remarkably space-efficient probabilistic data structure designed to answer one specific question extremely fast: "Is this element a member of a set?". It yields either "Possibly in set" or "Definitely not in set". This means it has a 0% false-negative rate, but a non-zero false-positive rate.

Architecture & Mechanics

Under the hood, a Bloom Filter completely discards the actual data you feed it. It only retains a mathematical footprint of the data, which is how it achieves its incredible space efficiency.

The Core Components

  • The Bit Array (m): The foundation is a simple array of bits (0s and 1s), initialized entirely to 0.
  • The Hash Functions (k): A set of independent, fast, and uniformly distributed hash functions (like MurmurHash or fnv). Cryptographic hashes like SHA-256 are too slow for this purpose.

Insertion (Add)

When you add an item (e.g., "apple"), you feed it through all k hash functions. Each function outputs an integer, which is mapped to an index in the bit array (modulo m). The bits at those specific indices are flipped from 0 to 1.

Query (Check)

To check if "apple" exists, you run it through the exact same k hash functions yielding the same indices. If all of those bits are 1, the item is probably in the set. If even one bit is 0, the item is definitely not in the set.

The Mathematics of False Positives

Because multiple different items can hash to the exact same bits (hash collisions), it is entirely possible to query an item you never added, and find that all its corresponding bits were coincidentally set to 1 by other prior insertions. This is a False Positive.

The probability of a false positive (p) depends on three variables:

  • n = expected number of elements to be inserted
  • m = size of the bit array
  • k = number of hash functions

The Optimal Hash Formula:

k = (m / n) * ln(2)

By carefully selecting m relative to your expected n, you can mathematically guarantee a targeted false-positive rate (like 0.01%, or 1 in 10,000).

Production Use Cases

Bloom filters shine when dealing with massive datasets where traditional HashMaps would exhaust RAM, or as a protective layer in front of expensive disk/network I/O.

1. LSM-Tree Databases

Databases like Cassandra, HBase, and RocksDB strictly use Bloom Filters to optimize Read queries. Before checking a massive SSTable on disk to see if a row exists, it checks the Bloom Filter in RAM. If the filter says "No", it safely skips a slow disk read entirely.

2. Malicious URL Defense

Browsers (like Google Chrome historically) embed a Bloom Filter containing millions of known malicious URLs. When you visit a site, the local fast filter checks the URL. Only if it returns a positive match does it make a slow network request to Google's servers to verify.

3. Weak Password Checks

When signing up for a service, rather than querying a massive 50GB file of top breached passwords, the API checks a highly compressed 50MB Bloom Filter in-memory to instantly reject common passwords like `password123`.

4. CDN Caching Strategy

Akamai uses Bloom Filters as a "One-Hit Wonder" defender. Content isn't cached the first time it's requested (saving massive space). The request is added to a Bloom Filter. Only if it's requested again (Triggering a Positive in the filter) is it actually moved to the costly cache.

The Deletion Dilemma

By default, you cannot delete data from a standard Bloom filter. Because hash functions overlap, unsetting a bit from 1 back to 0 for "apple" might actually break the record for "banana" which coincidentally relied on that same bit.

Solution: If your system strictly requires removing items, you must use a variant like the Counting Bloom Filter, which replaces single bits with small integer counters that increment on addition and decrement on deletion, at the cost of significantly higher memory consumption.

Further Reading