Bloom filter — probabilistic data structure that answers "element is probably in set" or "definitely not in set". Uses bit array + multiple hash functions. Memory-efficient (kilobytes for millions of items), but false positives are possible (no false negatives). Used in Cassandra (skip disk reads for missing keys), CDN (dedupe), DB indices, Chrome URL safety checks.
Below: details, example, related terms, FAQ.
Free online tool — HTTP header checker: instant results, no signup.
// Redis RedisBloom
> BF.RESERVE users 0.01 100000
> BF.ADD users alice
> BF.EXISTS users alice → 1 (probably)
> BF.EXISTS users bob → 0 (definitely not)A Bloom filter is a space-efficient probabilistic data structure that allows for fast membership checks of elements in a set. It operates on the principle of utilizing a bit array and multiple hash functions. Here’s how it works:
m, initialized to all zeros.k independent hash functions, each producing a hash value that maps to an index in the bit array.x to the Bloom filter:k hash values for x.y is in the set:k hash values for y.y is probably in the set; if any bit is 0, y is definitely not in the set.This mechanism allows Bloom filters to use significantly less memory compared to traditional data structures, making them ideal for applications requiring efficient space usage.
Implementing a Bloom filter can be achieved using various programming languages. Below are examples in Python and Java:
from bitarray import bitarray
import mmh3
class BloomFilter:
def __init__(self, size, hash_count):
self.size = size
self.hash_count = hash_count
self.bit_array = bitarray(size)
self.bit_array.setall(0)
def add(self, item):
for i in range(self.hash_count):
index = mmh3.hash(item, i) % self.size
self.bit_array[index] = 1
def check(self, item):
for i in range(self.hash_count):
index = mmh3.hash(item, i) % self.size
if self.bit_array[index] == 0:
return False
return True
# Usage
bloom = BloomFilter(1000, 7)
bloom.add('example_item')
print(bloom.check('example_item')) # Output: True
print(bloom.check('not_in_set')) # Output: Falseimport java.util.BitSet;
import java.util.Random;
public class BloomFilter {
private BitSet bitSet;
private int[] hashSeeds;
private int size;
public BloomFilter(int size, int hashCount) {
this.size = size;
bitSet = new BitSet(size);
hashSeeds = new int[hashCount];
Random rand = new Random();
for (int i = 0; i < hashCount; i++) {
hashSeeds[i] = rand.nextInt();
}
}
public void add(String item) {
for (int seed : hashSeeds) {
int hash = Math.abs(item.hashCode() ^ seed) % size;
bitSet.set(hash);
}
}
public boolean check(String item) {
for (int seed : hashSeeds) {
int hash = Math.abs(item.hashCode() ^ seed) % size;
if (!bitSet.get(hash)) {
return false;
}
}
return true;
}
}
// Usage
BloomFilter bloom = new BloomFilter(1000, 7);
bloom.add("example_item");
System.out.println(bloom.check("example_item")); // Output: true
System.out.println(bloom.check("not_in_set")); // Output: falseBloom filters are versatile data structures that find application in various domains where space efficiency and quick membership queries are crucial. Here are some common use cases:
These use cases illustrate the practical benefits of Bloom filters, particularly in scenarios where memory conservation and rapid lookup times are essential.
Pre-filter before expensive ops: check before DB query, before CDN miss. If >95% queries are "not in set" — saves huge load.
Trade-off: lower FPR = bigger filter. 1% FPR = 10 bits/element. 0.1% = 14 bits/element. Choose by downstream cost.
HLL counts unique elements (cardinality), not membership. Different use cases.
Free plan — 10 monitors, checks every 5 min, no card required. Upgrade for 1-minute interval and multi-region monitoring.