Problem
Checking disk, a remote cache or another service for keys that mostly do not exist wastes time and I/O. Holding every key in memory is too costly. A small structure that rules out most misses would save the expensive lookup.
Also known as: probabilistic-membership-filter
Cheaply answer whether an item is definitely absent or possibly present, avoiding expensive lookups for misses.
Checking disk, a remote cache or another service for keys that mostly do not exist wastes time and I/O. Holding every key in memory is too costly. A small structure that rules out most misses would save the expensive lookup.
A Bloom filter is a probabilistic set. It answers "definitely not present" or "maybe present". Databases like Cassandra, HBase and RocksDB keep one per data file so reads skip files that cannot hold the key.
Adding and testing with three hash functions.
bits: 0 0 0 0 0 0 0 0 0 0
add "alice": h1=1 h2=4 h3=8 -> bits 1,4,8 set
test "bob": h1=2 -> bit is 0 -> definitely absent
test "carol": bits all 1 (set by others) -> maybe present
false positive rate ~ (1 - e^(-kn/m))^k
The optimal number of hashes is about (m/n) times ln 2.
Variants address the limits. Counting Bloom filters replace bits with small counters so items can be removed. Cuckoo filters support deletion and often use less space at low error rates. Scalable Bloom filters add new filter layers as the set grows. In an LSM engine, each SSTable has its own filter built at flush time, and because SSTables are immutable the filter never needs deletion. A filter in front of a cache can stop requests for nonexistent keys from reaching the database, which blocks a common abuse pattern.
Low - a few bits per element
Low - rebuild periodically
Medium - probabilistic reasoning
Filter saturates as items exceed design size and false positives climb
Treating a positive as proof of existence and skipping the real check
Deleted keys remain set, so stale positives persist until rebuild