Course map

Topic 141 / Advanced

Probabilistic data structures: Bloom filter, HyperLogLog

Chapter 8.6 · State at Scale

What and why

The exact structures from topic 2 (hash maps, trees) answer membership and counting questions precisely, but precision costs memory that grows with the data — sometimes more than is worth keeping at scale. A probabilistic data structure trades a small, bounded, known error rate for dramatically less memory. A Bloom filter answers "have I seen this key before?" with a guarantee of no false negatives (a "no" is certain) but a tunable chance of false positives (a "yes" might be wrong). HyperLogLog estimates the number of distinct items in a stream using a small, fixed amount of memory regardless of stream length, at the cost of an approximate rather than exact count. Builds on: → 2 (data structures and algorithms).

Diagram

Bit array (size 8): [0 0 0 0 0 0 0 0]
insert("a"): bits 2,5 → [0 0 1 0 0 1 0 0]
insert("b"): bits 5,7 → [0 0 1 0 0 1 0 1]
check("a"):  bits 2,5 both set → "possibly present" (correct)
check("c"):  bits 2,7 both already set by a/b →
             "possibly present" — but "c" was never inserted: FALSE POSITIVE
check("d"):  bit 0 unset → "definitely absent" (correct)

How it works

  1. Bloom filter: a bit array plus k independent hash functions; inserting a key sets the k bit positions its hashes map to; checking a key reads those same k positions — if any is unset, the key was definitely never inserted; if all are set, it was probably inserted, but another combination of inserted keys could have set exactly those bits by coincidence.
  2. The false-positive rate rises as more keys go into a fixed-size filter (more bits get set, so more coincidental all-set lookups occur) and falls as the array grows or the hash-function count is tuned — a chosen, computable parameter, not a fixed property.
  3. A basic Bloom filter supports no deletion (unsetting a bit for one key could falsely clear another key sharing that bit) and never returns a false negative, which is what makes "definitely no" trustworthy enough to skip an expensive lookup (an LSM-tree read path → 64, or a seen-URL check) entirely on a miss.
  4. HyperLogLog: hashes each incoming item and tracks, across many small buckets, the longest run of leading zeros seen in any hash routed to that bucket — a longer run observed is evidence that more distinct items likely passed through; averaging this signal across many buckets controls the variance any single bucket would have alone.
  5. The result is one approximate cardinality with a known, bounded relative error, computed in memory that stays fixed whether a million or a billion items were counted — the structure never stores the items themselves, only the aggregated hash signal.
  6. Both structures are mergeable: combining two Bloom filters (bitwise OR) or two HyperLogLogs (per-bucket max) gives the correct structure for the union of their inputs, without replaying the original data.

Practical example

(illustrative). An 8-bit Bloom filter with 2 hash functions, after inserting "a" and "b" as above, reports "c" as possibly present even though it was never inserted — a false positive from its hashes landing on bits already set by "a" and "b." Checking "d" correctly reports "definitely absent" because one of its bit positions was never set by anything. The structure's whole trade-off, in miniature: the "no" is always trustworthy; the "yes" needs a size and hash count chosen for the expected key count to keep false positives rare enough to justify the memory saved.

When to use it

  • Use when: a Bloom filter to cheaply skip expensive lookups when the answer is usually "no" (checking a key against an LSM-tree's on-disk segments before reading them; flagging already-seen URLs in a crawler); HyperLogLog for approximate distinct counts at a scale where an exact count would need memory proportional to the number of distinct items (e.g., unique visitors).
  • Avoid when: an answer must be exact — a Bloom filter can never replace a real existence check when a false positive is unacceptable (deciding whether to actually charge a payment), and HyperLogLog can never substitute for an exact count where billing or compliance depends on the precise number.
  • Failure scenarios: sizing a Bloom filter for the wrong expected key count, so the false-positive rate silently grows far beyond what was planned as more keys are inserted; treating a HyperLogLog estimate as exact in a report that claims a precision it doesn't have.
  • Simpler alternative: for small datasets that fit comfortably in memory, an exact hash set or counter (→ 2) is simpler and has no error to reason about.

Practice

(Mechanism kind — build a tiny version; 20–30 min, no lab services, any language). Implement a Bloom filter with a 64-bit array and 3 hash functions (three different seeds of a standard hash function suffice). Insert 10 known keys, then check 20 keys (the 10 inserted plus 10 never inserted) and count false positives among the 10 not inserted. Repeat with a 16-bit array and the same 10 keys, and compare false-positive counts between the two sizes. Done when the smaller array shows a measurably higher false-positive rate than the larger one, on the same inserted keys.

Key takeaway

Bloom filters trade a tunable, one-directional error (false positives only, never false negatives) for sublinear membership-check memory; HyperLogLog trades exactness for fixed-size, approximate distinct counts at any scale. Done when you can: explain why a Bloom filter's "no" is always trustworthy but its "yes" isn't, and size one for a stated false-positive budget. Used next in → 142.

References

None — this topic makes no external claims.

Source directory

Search the guide