Bloom filters in front of a cache
Deriving the false positive bound, choosing the hash count, and implementing double hashing to drop negative lookups before they hit disk.
Contents (7)
In key-value stores and edge caches, a large share of tail latency comes from negative lookups. Every request for a missing key misses memory and falls through to disk or an origin server.
Placing a Bloom filter in front of the backing store lets a node reject missing keys in memory. Below is the request path, the false positive derivation, and a compact double-hashing implementation in TypeScript.
Lookup path
Incoming reads check the L1 memory cache first. On a miss, the node tests its local Bloom filter. When the filter reports absence, the node returns 404 immediately and skips origin I/O.
Keeping filters in sync on writes
When multiple application instances write to shared storage, edge nodes update their filters from snapshots or an asynchronous event stream. Workloads that need deletes use a counting filter or rebuild a fresh bit array on a schedule.
False positive bound and optimal hash count
Let be the bit array size, the number of inserted keys, and the number of independent hash functions. One hash leaves a given bit unset with probability . After inserting keys, the probability that a bit remains is approximately:
A missing key triggers a false positive when all probed positions are :
Optimal number of hashes
For fixed and , setting the derivative of to zero gives the optimal hash count and minimum false positive rate:
Allocating 10 bits per key () with keeps the false positive rate near . More than 99% of negative lookups never reach the origin.
State transitions and avoided origin reads
If a TTL cache models item temperature across Hot, Warm, and Cold states, unit-time transitions form a stochastic matrix :
With arrival rate and exponential residency decay , the expected number of avoided origin reads over is:
Because climbs as grows, production caches rotate to a larger bit array before the rate crosses a fixed threshold such as 2%.
Double hashing implementation
Computing separate hash functions is unnecessary. By the Kirsch and Mitzenmacher result, two independent 32-bit hashes and generate bit indices via without hurting the asymptotic false positive rate.
export class BloomFilter {
private readonly bits: Uint8Array;
private readonly bitCount: number;
private readonly hashCount: number;
constructor(expectedItems: number, falsePositiveRate = 0.01) {
const m = Math.ceil(
(-expectedItems * Math.log(falsePositiveRate)) / Math.LN2 ** 2,
);
const k = Math.max(1, Math.round((m / expectedItems) * Math.LN2));
this.bitCount = m;
this.hashCount = k;
this.bits = new Uint8Array(Math.ceil(m / 8));
}
private hashPair(key: string): [number, number] {
let h1 = 0x811c9dc5;
let h2 = 0x01000193;
for (let i = 0; i < key.length; i += 1) {
const code = key.charCodeAt(i);
h1 = Math.imul(h1 ^ code, 0x01000193) >>> 0;
h2 = Math.imul(h2 ^ (code + i), 0x85ebca6b) >>> 0;
}
return [h1, h2 || 1];
}
add(key: string): void {
const [h1, h2] = this.hashPair(key);
for (let i = 0; i < this.hashCount; i += 1) {
const pos = (h1 + i * h2) % this.bitCount;
this.bits[pos >>> 3] |= 1 << (pos & 7);
}
}
mightContain(key: string): boolean {
const [h1, h2] = this.hashPair(key);
for (let i = 0; i < this.hashCount; i += 1) {
const pos = (h1 + i * h2) % this.bitCount;
if ((this.bits[pos >>> 3] & (1 << (pos & 7))) === 0) {
return false;
}
}
return true;
}
}
Operational constraints
- Replication lag after a write can cause temporary false negatives. Reads inside that window must bypass the filter or query through a short grace period.
- When memory bandwidth dominates, a blocked Bloom filter fits all probes inside a single 64-byte CPU cache line.
- Monitor bit fill ratio and rotate before it exceeds 50%, where false positives rise sharply.