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.

Remember it

The few things worth knowing cold, and how to make them stick.

Memory hook

By account, then by topic; gate the tail, ring the hour, rank the surprise.

An ice lolly stand on a hot day with a small tally board. Three lines are enough for the likely winners, a newcomer takes over the smallest line, a new board hangs up each hour, and the colour that sells far more than usual gets the arrow.

Cheat sheet

Events
46,300/s, 139,000 peak
Pairs
100M, 1M after the gate
Sketch
5 rows, 8.4M columns, 168 MB
Error
eN/w, about 8
Window
60 + 168 buckets
Lists
600, 3 MB
Fresh
58 s at worst
Score
surprise over √(base+100)

Spot it when you see

  • Most frequent items in a stream.
  • Unbounded distinct keys, few winners.
  • Rising compared with usual.
  • Counts that others want to fake.

Tricks that stick

  • Strangers only push up: take the smallest.
  • One topic, one counter.
  • Add sketches, never max them.
  • Count people, not posts.

Do not forget

  • Merging top lists of random splits.
  • Listing a sketch or a HyperLogLog.
  • Tumbling hours on a live list.
  • Growth without a minimum.
  • Dropping suspicious events before the log.