AlgoViz

Design a Top K / Trending Topics System

hard 45 min

Count billions of mentions a day and show the ten topics that are rising right now, in each country and city.

Numbers that decide

Work out a number when the answer changes what you build.

Estimates

Only the sums that decide

Inputs are our assumptions. Peaks are three times the average.

Events

500M×886,400≈46,300/s,×3≈139,000/s\frac{500\text{M}\times 8}{86{,}400}\approx 46{,}300/\text{s},\qquad \times 3 \approx 139{,}000/\text{s}
Each counts for a city, a country and the world: about 139,000 increments a second on average and 417,000 at peak. Too many for rows in one relational table, easy for memory on several machines.

Exact memory

100M pairs×2 KB=200 GB1M pairs×2 KB=2 GB100\text{M pairs}\times 2\,\text{KB} = 200\,\text{GB} \qquad 1\text{M pairs}\times 2\,\text{KB} = 2\,\text{GB}
A pair is one topic in one place. Its state is 60 minute buckets and 168 hour buckets of four bytes, plus name and overhead. Keeping only the 1% that repeat saves a factor of one hundred.

How wide must a sketch be?

error≤e Nw,2.72×25M223≈8but2.72×167M10,000≈45,000\text{error}\le \frac{e\,N}{w},\qquad \frac{2.72\times 25\text{M}}{2^{23}}\approx 8 \qquad\text{but}\qquad \frac{2.72\times 167\text{M}}{10{,}000}\approx 45{,}000
Left: one of 20 counters, one hour, 8.4 million counters in each of five rows, 168 MB. Right: the whole hour in a sketch of 10,000 columns. Its error is larger than most counts on the list.

All the numbers

QuantityWorkingResultDecision
Raw log4B × 100 B400 GB/daySeven days, three copies: 8.4 TB
Input bandwidth139,000 × 100 B14 MB/s, 111 MbpsNot a problem
One worker by account139,000 / 207,000/sTwenty workers are plenty
After adding up20 workers, every 10 s2 records/s per busy topic and placeHot topics are harmless
Memory for repeats167M × 16 B / 20133 MB eachAccount and topic seen this hour
Sketches20 × 168 MB3.4 GBOne per counter
Lists200 × 3 windows600Written whole
List storage600 × 50 × 100 B3 MBOne small Redis with a copy
Reads1B / 86,400, 3x11,600 and 34,700/sCache at the edge
Read bandwidth34,700 × 2 KB70 MB/sUnder 1 Gbps
Origin at worst600 lists × 100 sites / 30 s2,000/sA few API servers
History200 × 24 × 100 rows × 200 B96 MB/day35 GB a year

Freshness budget for the hour list

StepSeconds at worst
Post to log1
Filter adds up10
Counter reports15
Ranker merges and stores2
Copy at the edge expires30
Total58
Waiting on clocks is 55 of the 58 seconds. A 60 second cache alone would already break the target.

When is an exact map smaller than a sketch?

10,000×5×4 B40 B per topic=5,000 topics\frac{10{,}000\times 5\times 4\,\text{B}}{40\,\text{B per topic}} = 5{,}000 \text{ topics}
With 40 bytes for each entry, a map of fewer than 5,000 topics is smaller than a 200 KB sketch. Most cities in one minute are below that line. A sketch for every place and minute would waste memory: 200 places × 60 minutes × 200 KB is 2.4 GB.

What if?

ChangeEffect
A hundred times smaller463 events a second: PostgreSQL and a job every minute
Ten times more placesLists 30 MB. State grows with pairs that repeat, not with places
A burst of ten times for ten minutes463,000/s. If filters manage 200,000, the log grows by 158 million and drains in about 17 minutes
Search phrases, 100 times more distinctExact for all would be 20 TB. The gate becomes necessary
Drain: 158M divided by the spare 154,000 a second is 1,026 seconds.