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.
Agree on the scope
Ask what changes the design, then commit to a short list.
Scope
Clarify, then choose
Clarifying requirements
Interviewer
Design a trending topics system for a social network. What would you ask?
You
First, what does trending mean?
- Most mentions: the same names every day.
- Rising: far above its own usual level. I would choose this, and it needs a history for comparison.
- Window: one hour, a day and a week. The hour matters most.
Interviewer
Rising. Scale?
You
I will assume:
- 500 million posts a day, about eight topic events each: hashtags, names, links, phrases.
- That is 4 billion events a day, 46,300 a second, 139,000 at a 3x peak.
- 55 million different topics a day, 99% of them mentioned fewer than 20 times in an hour.
- 200 places, three windows, lists of 50.
Interviewer
How fresh?
You
A mention should be able to change the hourly list within 60 seconds. The daily and weekly lists may be 10 to 30 minutes old. Reads answer in 50 ms at p99. Two readers may see number eight and nine swapped for a moment.
Interviewer
People will try to game it.
You
Then I count each account once per topic per hour, weigh accounts by trust, look for groups that act together, and let people review sensitive topics before they are promoted.
Features
- Accept topic events
- Top K for a place and a window
- Rising score against a baseline
- Filter by category
- History of one topic
- Protection and review
Out of scope
- Finding phrases in text
- Personal trends for each reader
- Ranking by watch time or percentiles
- Paid placement
- The posts shown behind a topic
Quality targets
| Quality | Target | Why |
|---|---|---|
| Accept an event batch | p99 under 100 ms | The caller should not wait for counting |
| Read a list | p99 under 50 ms | Shown on a home page |
| Freshness, hour | under 60 s | Breaking news |
| Freshness, day and week | 10 to 30 minutes | They move slowly |
| Reads available | 99.9%, old lists allowed | A list that is five minutes old is fine |
| Events durable | Not lost once accepted | Lists can be rebuilt |
| Accuracy | Top 10 right, counts within 1% | People see order, not counts |
Each answer changes the design
| You ask | If the answer is | Then |
|---|---|---|
| Known items only? | Yes, 10,000 songs | A table of counters, no sketch |
| Exact counts for money? | Yes | Different system: see ad clicks |
| Any window, such as 37 minutes? | Yes | Keep minute buckets for longer, or query an OLAP store |
| Daily list only? | Yes | A nightly job |
| Posts or people? | Posts | Simpler, and easier to fake |