Core Concepts
Probabilistic Data Structures
Save memory with a Bloom filter for membership checks and HyperLogLog for approximate distinct counts.
A Bloom filter can rule out a missing key before we read from the database. HyperLogLog estimates how many distinct values we have seen without storing them all. A Bloom filter can also say a key might be present, so the application still checks those matches against the original data.
Rule out an absent code with a Bloom filter
The URL Shortener uses a Bloom filter to rule out missing codes before a lookup. Later, we'll extend its click reporting with a sketch that estimates how many distinct visitors followed a link.
A Bloom filter is a row of bits, initially all zero. To insert a code, use a hash to turn it into repeatable positions in that row and set those bits to 1. The example inserts spring at positions 1, 4 and 6.
A lookup checks positions without changing any bits. The code rain checks a zero at position 3, so it cannot have been inserted. The code snow happens to check all ones. We read the database and find no such row. The filter's maybe present for a missing code is a false positive. Bloom's original paper
A complete filter built with compatible hashing and only zero-to-one changes cannot rule out an inserted key. The exact store still controls the answer when all bits are one. After deleting a mapping, leave its bits in place until the next rebuild. Other codes can share any of those positions, so clearing a shared bit can turn an existing code into a false negative.
Protect the negative answer in production
The application has to cover every mapping it lets users read. Suppose the database commits summer but a redirect node has yet to add it to its filter. The redirect can see a zero and incorrectly return 404. Use negatives only when the node can verify that its filter covers every public code. A node whose filter is catching up or has lost updates uses the normal lookup path.
Build a replacement from a consistent snapshot while the complete old filter keeps serving. Apply every later committed creation through a tracked position before enabling the replacement. Treat coverage as a condition for every mapping that becomes public, including after the switch. One implementation adds the mapping to each active filter generation before making it publicly readable. If a node falls behind or cannot verify current coverage, it bypasses negatives until it catches up. A one-time catch-up watermark alone cannot protect later writes. That same fallback matters after a restore.
When more inserted keys share a fixed number of bits, more absent keys test positive. Size the filter for the expected number of keys and the lookup load the system can handle. For a representative mix of 10,000 absent requests per second and a 1 percent false-positive rate, expect about 100 requests per second to continue through the lookup path. A stream of repeated requests for one false-positive code could send many more.
Measure how many absent requests still reach that path and how many bits are set. The fraction of positive tests with no matching row shows how much of the follow-up work was for missing keys. That fraction can change when users request a different mix of codes, even if the filter has stayed the same. To estimate the filter's false-positive rate itself, test a representative sample of known absent keys and divide the possible matches by all the absent keys you tested.
Estimate distinct visitors with HyperLogLog
Suppose the click reporting path records a stable visitor ID with each captured click. An exact set of those IDs grows with every new visitor. HyperLogLog is a compact sketch, a summary that estimates the number of distinct IDs in the captured events. The number of distinct elements is called cardinality.
Hash each stable visitor ID into a number that looks uniformly random. Use some bits to pick a bucket and count the zeros before the first one in the remaining hash. A long run is rare, so seeing one gives evidence that the sketch has processed many distinct inputs. A register is the small number the sketch keeps for each bucket. It stores the largest rank seen there, where rank is the zero-run length plus one. The estimator combines observations across many buckets.
Visitor v7 clicks in both West and East. Its hash gives bucket 1 a rank of 3 in both regions. Repeated visits by v7 leave that contribution unchanged. East also sees v9, whose hash gives bucket 3 a rank of 4. After merging, bucket 1 still holds 3, and bucket 3 holds 4. HyperLogLog paper
Merge compatible sketches by taking the maximum for each register, then estimate from the merged registers. Adding the two estimates counts overlapping visitors in both regional totals. To make the merge work, both regions need the same identity encoding, hash scheme, and register layout. Choose the reporting window to match the question. Merge a week's daily sketches to estimate distinct visitors over that whole week, then expire daily sketches past the reporting horizon. A regular HyperLogLog keeps only register maxima, so it cannot remove an individual visitor.
Redis provides PFADD to observe a visitor, PFCOUNT to estimate a count, and PFMERGE to combine sketches. Its documentation gives up to about 12 KB per sketch and a relative standard error of about 0.81 percent. That figure describes the spread of estimates across samples. A membership check or exact billing count still needs its own source of truth. Redis HyperLogLog
See also
- Caching - follow a redirect that bypasses a fast path and reads the source of truth.
- Redis - place fast shared state behind its durability and resource limits.
- Batch vs Stream Processing - choose when the click summary should appear and when a historical rebuild is needed.
Used in
- URL Shortener - a complete Bloom filter rules out missing codes before the mapping lookup. The distinct-visitor sketch here extends its best-effort click reporting as a teaching example.
Done reading? Mark it so it sticks in your dashboard.