AlgoViz

Three questions for a tiny memory budget

medium 6 min

A telemetry service needs to skip pointless disk reads, estimate traffic for a known endpoint, and count unique devices across regions.

Video lesson · Chapter 1 of 5

Three questions arrive at one operator desk

0:00 / 3:54

Three questions to ask of the same stream

1:09Reel

Theory

Read or listen first. About 1 min.

What it is

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

EventsABAB present?YesCount of A2Distinct IDs2B exists · A appears twice · 2 distinct IDs
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 cardinality
Region A → Sketch A (device hash); Region B → Sketch B (device hash); Sketch A → Merged HLL (registers); Sketch B → Merged HLL (registers)
A 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.

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
A device visits two regionsDevice 9first visitRegion AemptyRegion BemptyCount distinct devices across regions

Estimate the union of observed identities.

Structure

Device cardinality

Device cardinality

Region A → Sketch A (device hash); Region B → Sketch B (device hash); Sketch A → Merged HLL (registers); Sketch B → Merged HLL (registers)
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.

How to choose a sketch

Three summaries, three error contracts

QuestionStructureHow to interpret the result
Could this key exist?Bloom filterA positive needs verification; a negative rules it out for a correctly maintained filter
How frequent is this known key?Count-Min SketchFor nonnegative updates collisions can inflate its estimate
How many distinct keys appeared?HyperLogLogA 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.00811.04 / \sqrt{16384} \approx 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.

Sources