Ten stalls want one number: how many different people drank chai this week? Writing every customer down takes 8 bytes each. For 100,000 customers that is 781 KB.
A board of 16,384 small tallies takes only 12 KB, 65 times less. It counts by estimate. How far off is its count?
guess first
100,000 customers, a 12 KB board. How far off?
0.78%typical miss. Board: 12 KB
typical miss as the board shrinks
board 12 KBlist 781 KB
run it
Use fewer tallies. How far off does the count get?
16642561,0244,09616,384
The crowd is 100,000 different customers. The big number is the typical miss, averaged over 16 crowds.
why it works
One tally is luck. Many tallies average the luck out.
Each tally keeps the longest streak of lucky coin flips it has seen. One tally can be very lucky and be badly wrong. With many tallies, the lucky and unlucky ones cancel out.
You have 1 KB of memory. The miss must stay under 3%. Which board?
System DesignCounting a crowd: HyperLogLog
Counting the crowd
A hundred thousand different customers and a 12 KB tally board. How far off is the count?
Ten stalls want one number a week: how many different people drank the chai. Writing every phone number down needs a book that grows with the crowd. The slate keeps tallies instead, each holding the longest lucky streak it has seen, and the lab counts a real crowd that way.
Preparing for interviews? The ten-week plan puts this in order with DSA and the design rounds.
try this Step Tallies from 16 up to 16,384, then drag Different customers, and watch Typical miss against Memory.
16,384
100,000
1 of 16
Breaking point: 16 tallies for 1,000,000 customers count anywhere from 690,000 to 1,624,000 across 16 crowds of the same size, a typical miss of 31.9%, because too few streaks are left to average the luck out.
The tally board
a tower per tally, as tall as its longest run of heads
a customer dropping onto the tally its hash picks
too few tallies, or the exact list that needs far more memory
On the right, the true crowd stands beside the count the tallies give, and the exact list beside the tally board. Add tallies and the two count columns meet. Break it and the count column jumps from crowd to crowd.
The tallies say
99,740
true count 100,000, off by −0.26%
Typical miss
0.78%across 16 crowds this size. Expected: 0.81%
Memory
12 KB
the exact list: 781 KB
16,384 tallies miss by about 0.8%, in 12 KB. The exact list would need 781 KB, 65 times as much, and would be exactly right.
What the tallies hold
Each bar counts the tallies holding that longest run of heads (leading zeros in a customer's hash). Each doubling of the crowd moves the hump one step right.
The working
Where the memory and the miss come from, redone every time you move a control.
The tally boardtallies1 dot = 256 tallies16,384 tallies, each a small number that fits in 6 bits. Eight bits make a byte, so that is 12,288 bytes, 12 KB, however big the crowd. 16,384 × 6= 98,304 bits
The exact listexact list781 KBtally board12 KBWriting every customer down takes at least 8 bytes each, for 100,000 customers. 100,000 × 8= 800,000 bytes
How much smallerThe exact list's bytes over the board's bytes. The price of the saving is the miss below. 800,000 ÷ 12,288= 65×
The miss to expectexpected0.81%measured0.78%Each tally is one lucky or unlucky streak, and the board averages them. The miss shrinks with the square root of the tallies: 16,384 of them gives 128, and the paper's rule is 1.04 over that. This crowd's tries missed by 0.78%. 1.04 ÷ 128= 0.81%
A hundred thousand different customers and a 12 KB tally board. How far off is the count?
Luck, averaged out
One tally is one streak of luck. A single customer whose hash opens with twelve heads makes a crowd of one look like four thousand. Many tallies, each keeping its own longest run, outvote the lucky ones. The typical miss is 1.04 divided by the square root of the number of tallies, so four times the tallies halves the miss. Raise the tallies and the typical miss falls while memory climbs.
Memory stays fixed when you change the crowd. The tally board is the same size for a thousand customers or a million, while the exact list grows with every new face. Below about 1,500 customers the list is smaller, so store the names instead.
Limits of this model
The crowd is synthetic. Each customer is a number run through a fixed 32-bit mixing recipe, and "which crowd" changes a seed. A real HyperLogLog hashes real ids. The maths and the error are the same.
The exact list is a floor. It is counted at 8 bytes per customer, one 64-bit id each. A real exact set also spends memory on the table around the ids. Python's set for a million ids took 32 MB, four times the floor.
Memory is counted as Redis packs it, six bits per tally, so 16,384 tallies take 12 KB. The typical miss is the root-mean-square of the misses across eight crowds. It moves a little between dial settings, even where the formula says it should stay put.
This is the 2007 estimator, including the small-crowd fix. At two or three customers per tally it runs a little high: 4,096 tallies and 10,000 customers miss by about twice the expected error. Google's 2013 paper, HyperLogLog in Practice, corrects that range.