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.
Grow the design
Start with the smallest system that works. Change it only when something breaks.
Version by version
From one table to two stages
Seven versions
Version 1: One service, one table
Forced by: The features: count and list.
- What changes
- Each batch adds to rows of topic, place and minute. A job runs the sum for the last hour every minute and stores the list.
- What it costs
- Nothing new to run.
- What breaks next
- Writes. Hundreds of events a second are fine. 46,300, each for three places, are not.
Say: At a hundredth of our scale I would stop here.
Architecture
The parts and their reasons
Two stages, two keys
Component choices
| Component | Choice | Reason and cost |
|---|---|---|
| Log | Kafka, keyed by account | Durable and replayable. Hot topics do not make hot partitions |
| Workers | Flink | Keyed state, event time and checkpoints are built in. A cluster to run |
| Gate | Count-Min Sketch | Fixed memory, one-sided error |
| Lists | Redis | Small values, fast, easily rebuilt |
| Delivery | CDN, 30 s | Same answer for everyone in a place |
| History | ClickHouse | Cheap scans by topic and time |
Flows
Three journeys
A batch of mentions
Making a list
Reading a list