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.
Video lesson · Chapter 1 of 5
Yesterday's biggest singer is not necessarily today's trend
Trending topics in 90 seconds
1:30Reel coming soon
Theory
Read or listen first. About 2 min.
What it is
A top K system watches a stream of events and answers one question: which K items appear most often in a period of time? K is small, such as 10 or 50. The number of different items is huge.
A trending system asks a slightly different question: which items appear much more often than they usually do? A famous singer is always popular. A new word that went from nothing to fifty thousand mentions is trending.
Why it exists
People open a trends list to learn what is happening now. The list must therefore be fresh, the same for everyone in a region, and hard to buy with a crowd of fake accounts.
The problem also teaches a habit that is useful everywhere: you rarely need to remember everything. A bounded summary with a known error can replace an unbounded table, if you know which error you can accept and prove that it stays small.
A short history
1982
Jayadev Misra and David Gries publish a way to find repeated elements with a small, fixed number of counters.
1996
Noga Alon, Yossi Matias and Mario Szegedy formalise algorithms that read a stream once in little memory.
2003 to 2005
Graham Cormode and S. Muthukrishnan invent the Count-Min Sketch and describe it in a 2005 paper.
2012
Goyal, Daumé and Cormode study sketches, including an update rule that overcounts less, for counting words in text.
Words to know
- Topic event
- One mention of one topic: a hashtag, a name or a phrase found in a post.
- Heavy hitter
- An item that makes up a large share of the stream.
- Count-Min Sketch
- A small table of shared counters. Its estimates can be too high and are never too low.
- Sliding window
- The last hour, moving forward all the time, made here from sixty one-minute buckets.
- Watermark
- The stream's statement that events older than this time have probably all arrived.
- Baseline
- The count we would expect at this hour on a normal day.
- Combiner
- A step that adds up small counts before sending them on.
- Checkpoint
- A saved copy of a worker's memory and of its place in the log.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
How it works
Step through the idea, or press Play.
Count. Events through a log, filtered and added up
Understand it
Few marks, a known mistake, and a list that is good enough to act on.
The product
What are we building?
A trends list for a large social network: the top ten or fifty topics for the world, for each country and for large cities, over the last hour, day and week. Similar lists exist for searches, videos, songs and products.
Count
Mentions of any topic, by place and by time.
Rank
Rising topics first, not only large ones.
Serve
Finished lists in milliseconds.
Protect
Fake crowds and harmful topics stay out.
Similar applications
| List | Counts |
|---|---|
| Trends on a social network | Posts and phrases |
| Search trends | Searches |
| Trending videos | Views |
| Hot posts on a forum | Votes with age |
| Music charts | Plays |
| Best sellers in a shop | Orders |
The hard part
The winner could be anything
A leaderboard knows its players. Here the next winner may be a word that did not exist an hour ago, so nothing can be registered in advance. Counts are spread over machines, and taking each machine's top ten is wrong if one topic's mentions are spread too. See multiplayer leaderboard for the known-players version.
The finished system
Where the design ends up
The whole system
Ideas owned by other topics
| Idea | Read |
|---|---|
| The log and consumer groups | Log-based queues |
| Windows, watermarks, state | Stream processing |
| One very hot counter | Sharded counters |
| Splitting keys over machines | Partitioning strategies |
| Short cache lifetimes | Cache TTL |
| Scoring suspicious accounts | Fraud detection |
| Billing-grade counting | Ad click aggregator |
Summary
The whole topic in one minute.
Mentions go into a log. A first group of workers, divided by account, removes repeats and suspicious accounts and adds up ten seconds of mentions. A second group, divided by region and topic, keeps the counts. Because a topic lives on exactly one counter, each counter's best 50 are enough to build the true list.
A sketch in front of the counters stops the millions of one-time topics from taking memory. The score compares the last hour with the same hour on earlier days. Lists are made in advance, stored whole, and served through a cache, so reading is trivial and a post reaches the list within a minute.
- If each topic lives on one counter, merging each counter's top K gives the true top K.
- Divide by account first, then by topic. Repeats and hot topics are solved on the way.
- Sixty minute buckets make a sliding hour: add one, remove one.
- A sketch overcounts and never undercounts, so it is a safe gate in front of exact counters.
- Trending means far above its own baseline, counted in people rather than in posts.