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.
API and data
The contract first, then the data that serves it.
API
One way in, two ways out
HTTP
POST /v1/events (internal){ "batch_id": "b-91", "events": [ { "id": "e1", "topic": "#superbowl", "account": "u7", "city": "c12", "country": "US", "at": "2026-02-08T19:45:32Z" } ] }-> 202 { "accepted": 497, "rejected": { "bad_format": 3 } }GET /v1/trends?place=US&window=1h&limit=10&category=sports-> 200 Cache-Control: public, max-age=0, s-maxage=30 ETag: "v8812"{ "place": "US", "window": "1h", "made_at": "...", "next_at": "...", "stale": false, "topics": [ { "rank": 1, "topic": "#superbowl", "people": 284700, "usual": 1900, "change": 2, "since": "..." } ] }GET /v1/topics/superbowl/history?from=...&to=...&step=hour&after=c1GET /v1/trends/stream?place=US server-sent events, every 30 sDecisions
| Decision | Reason |
|---|---|
| Batches of 100 to 1,000 events | One request for each event would cost more than the event |
| 202 after the log has it | The caller is safe, counting is later |
| Batch id and event id | A retry of a batch is recognised, and repeats are dropped |
| Whole list, one version | No half-updated list, easy to cache |
| 50 at most | The list is stored once and cut per request |
| 429 for noisy callers | One broken service must not fill the log |
How do open pages learn about changes?
BreaksWebSocket for every reader
- How
- Push each change on its own socket.
- Limit
- Millions of sockets for one short message every 30 seconds.
- Choose when
- The page already has a socket for something else.
WorksAsk again every 30 to 60 seconds
- How
- Plain requests, answered by the edge.
- Limit
- Some requests return nothing new. An ETag makes those tiny.
- Choose when
- Almost always. Our default.
StrongServer-sent events through the edge, with polling as a fallback
- How
- The edge holds one line to us and copies a whole list every 30 seconds. Pages animate the difference.
- Limit
- Open connections at the edge, and not every CDN supports it.
- Choose when
- A live dashboard where movement is the point.
Data
What lives where
| Data | Store | Pattern |
|---|---|---|
| Raw events | Kafka, 7 days | Append, read in order, replay |
| Seen accounts, sketches, buckets | Worker memory with checkpoints | Millions of small updates |
| Finished lists | Redis, replicated | Read whole by key |
| Topic display name, category, picture | PostgreSQL, cached | Looked up once when listed |
| Blocked topics, review decisions | PostgreSQL | Few writes, must be right |
| Hourly history | ClickHouse | Scan by topic and time |
Text
SET trends:US:1h {"version": 8812, "topics": [...]} EX 300SET trends:US:24h {"version": 391, "topics": [...]} EX 7200GET trends:US:1hSQL
CREATE TABLE trend_history ( topic String, place LowCardinality(String), hour DateTime, people UInt64, usual UInt64, rank UInt16) ENGINE = MergeTreePARTITION BY toYYYYMM(hour)ORDER BY (topic, place, hour);State held by one counter