AlgoViz

Design a Top K / Trending Topics System

hard 45 min

Count 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.

Our systemPost serviceReadersTrend servicePostgreSQLcounts
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

Kafka sends events by account to filters A and B. Both filters send sums to counter 1, which owns red and orange, and to counter 2, which owns blue and green. Both counters send their best topics to the ranker.
Every filter may see every topic. Every topic belongs to one counter.

Component choices

ComponentChoiceReason and cost
LogKafka, keyed by accountDurable and replayable. Hot topics do not make hot partitions
WorkersFlinkKeyed state, event time and checkpoints are built in. A cluster to run
GateCount-Min SketchFixed memory, one-sided error
ListsRedisSmall values, fast, easily rebuilt
DeliveryCDN, 30 sSame answer for everyone in a place
HistoryClickHouseCheap scans by topic and time

Flows

Three journeys

A batch of mentions

Suspicious events are stored in the log and marked by the filter, not thrown away at the door. Rules can then be changed and the week replayed.

Making a list

Reading a list

Most requests end at the second step. Every step of the three flows has its reason in Visualize.