Suppose a service has memory for a few counters but observes millions of identities. It might need to know whether an object is possibly present, how often a known endpoint was called, or how many distinct devices appeared. These questions sound similar until you ask what a wrong answer would do.
Three questions about one event stream
For the example stream A, B, A, membership asks whether B might be present. Frequency asks how often A appears. Cardinality asks how many different IDs appear.
Why it exists
An unnecessary disk lookup costs time. Incorrectly declaring a paid invoice absent can lose money. A small statistical error in a dashboard may be acceptable. Choose the operation and error contract before choosing the data structure.
View diagram · Device cardinalityA merged sketch estimates the union without adding duplicate visits. Full size
Words to know
False positive
A summary reports possible membership for an absent item.
Cardinality
The number of distinct elements observed.
How it works
Step through the idea, or press Play.
Step 1 of 3
Membership. A Bloom filter can rule out membership when a checked bit is clear. A positive requires an exact lookup. Plan capacity and false-positive rate; clearing shared bits on deletion can create false negatives. Use a deletion-aware variant when necessary.
Frequency. A Count-Min Sketch adds counts to hashed counters in several rows. With positive updates, the minimum counter for a key can overestimate because of collisions. Candidate discovery for top endpoints needs additional bookkeeping.
Distinct values. HyperLogLog uses hashed observations and compact registers to estimate cardinality. Merge compatible sketches register by register using maxima. Summing regional distinct estimates double-counts shared devices.
Understand it
Start here
One device crosses regions
A device reports a visit in region A and another in region B. Trace this one identity through two local summaries before estimating the number of devices across both regions.
One device crosses regions
Estimate the union of observed identities.
Structure
Device cardinality
Device cardinality
A merged sketch estimates the union without adding duplicate visits.
A dashboard with error bars
A service sees the same 1,000 devices in two regions. Adding regional distinct counts would report 2,000. Merge compatible HLLs and compare the estimate to an exact sampled set. For 16,384 registers the classic asymptotic relative standard error is about 0.81%, not a hard maximum.
A positive needs verification; a negative rules it out for a correctly maintained filter
How frequent is this known key?
Count-Min Sketch
For nonnegative updates collisions can inflate its estimate
How many distinct keys appeared?
HyperLogLog
A statistical estimate that may lie above or below the exact count
Follow one hash collision
Imagine two endpoints share a counter in one Count-Min row. An update for either endpoint increments it. Independent rows give each endpoint other counters, so the minimum reduces the impact of the collision. If all of a key’s counters pick up unrelated traffic, its estimate is still high. The sketch needs a key to query; keep candidate keys separately if the dashboard must list the busiest endpoints.
Time windows need a retirement rule
Build one compatible sketch per hour if a dashboard needs daily unique devices. Merge the hours still in the window. Once a register has taken a maximum, subtracting an expired hour cannot generally recover the previous winner. Keep the component sketches for the windows you need. Validate estimates against a small exact slice under realistic traffic distributions.
Merge compatible registers
Estimate the union of observed identities.
Add local distinct estimates
A shared device contributes to both totals.
Classic HLL relative standard error
1.04/16384≈0.0081
About 0.81% typical relative standard error. Individual estimates can deviate further.
Summary
The whole topic in one minute.
Membership, frequency and cardinality need different summaries. Specify the window, merge rule and validation method alongside the memory budget.