Design a Top K / Trending Topics System
hard 45 minCount 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
Exact memory
How wide must a sketch be?
All the numbers
| Quantity | Working | Result | Decision |
|---|---|---|---|
| Raw log | 4B × 100 B | 400 GB/day | Seven days, three copies: 8.4 TB |
| Input bandwidth | 139,000 × 100 B | 14 MB/s, 111 Mbps | Not a problem |
| One worker by account | 139,000 / 20 | 7,000/s | Twenty workers are plenty |
| After adding up | 20 workers, every 10 s | 2 records/s per busy topic and place | Hot topics are harmless |
| Memory for repeats | 167M × 16 B / 20 | 133 MB each | Account and topic seen this hour |
| Sketches | 20 × 168 MB | 3.4 GB | One per counter |
| Lists | 200 × 3 windows | 600 | Written whole |
| List storage | 600 × 50 × 100 B | 3 MB | One small Redis with a copy |
| Reads | 1B / 86,400, 3x | 11,600 and 34,700/s | Cache at the edge |
| Read bandwidth | 34,700 × 2 KB | 70 MB/s | Under 1 Gbps |
| Origin at worst | 600 lists × 100 sites / 30 s | 2,000/s | A few API servers |
| History | 200 × 24 × 100 rows × 200 B | 96 MB/day | 35 GB a year |
Freshness budget for the hour list
| Step | Seconds at worst |
|---|---|
| Post to log | 1 |
| Filter adds up | 10 |
| Counter reports | 15 |
| Ranker merges and stores | 2 |
| Copy at the edge expires | 30 |
| Total | 58 |
When is an exact map smaller than a sketch?
What if?
| Change | Effect |
|---|---|
| A hundred times smaller | 463 events a second: PostgreSQL and a job every minute |
| Ten times more places | Lists 30 MB. State grows with pairs that repeat, not with places |
| A burst of ten times for ten minutes | 463,000/s. If filters manage 200,000, the log grows by 158 million and drains in about 17 minutes |
| Search phrases, 100 times more distinct | Exact for all would be 20 TB. The gate becomes necessary |