Index

Free, with Google

Keep going, free.

A free account opens lessons 2 and 3 of every track, 33 more lessons, and keeps your progress and review cards on every device.

Start free with Google

A chalk plan

Open everything on chalk.

Every lesson, board, video, lab and paper, and the full review decks. Yearly is $39/year.

See plansStart free instead

Counting lab1 of 5
how many different customers today? write each one down 781 KB 63355333 a few of the tallies keep tallies 12 KB 100,000 customers

the story

Can a tiny board count a huge crowd?

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?

exact list 781 KBexactly right tally board 6335 12 KB ? % off 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

board 12 KB list 781 KB

run it

Use fewer tallies. How far off does the count get?

16,384 tallies

The crowd is 100,000 different customers. The big number is the typical miss, averaged over 16 crowds.

each tally keeps its longest streak tally 16tally 23tally 33tally 45tally 55tally 63 some are lucky. Many tallies average out.

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.

the working

16,384 tallies × 6 bits = 98,304 bits = 12 KB100,000 × 8 bytes = 800,000 = 781 KBRule: 1.04 ÷ √16,384 = 1.04 ÷ 128 = 0.81%Rule at 4,096: 1.04 ÷ 64 = 1.6%Measured over 16 crowds: 0.78%
1 KB memory you have the count may missby 3% at most which board size fits both?

your call

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.

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 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.

0123456789101112131415161718

The working

Where the memory and the miss come from, redone every time you move a control.

  1. 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
  2. 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
  3. 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×
  4. 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%

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.

Sources

On a narrow screen, the three readouts stack under the dials and the tally histogram comes last.

the lesson The slate next to the stove (caching)
The whole idea this lab came from, told start to finish.

on the path Module 1 of 32 in System Design: The stall gets famous
The lessons, boards and videos around this lab, in reading order.

Board: The Slate Next to the Stove

next lab The forking paths
A thousand labs test a pill that does nothing. How many publish that it works?