KatInterview

Real-Time Hourly Popularity Ranking at 100K Events per Second

System DesignModel answer
MediumModel answerSystem Designsystem-designstreamingtop-k

Last updated

Design a scalable service that ingests a massive stream of view events and serves the most popular items over a rolling 60-minute window, allowing slightly approximate results.

Hourly Popularity Ranking Service

You are asked to design a backend that surfaces the items (product IDs, hashtags, articles) that received the most views during the last 60 minutes. Traffic is global and heavy: the service must sustain about 100,000 events per second, with bursts well above that.

Operations

  • record_click(item_id): register one view of an item. Item IDs are 64-bit integers.
  • get_top_k(k): return the k items with the most views inside the rolling 60-minute window, ordered by count.

Quality requirements

  • Low latency on both reads and writes.
  • High availability and tolerance of node failures.
  • Horizontal scalability to the traffic level above.
  • Slightly approximate rankings are acceptable if that buys a substantial gain in performance.

Small example

With k = 3 and the click sequence [A, B, C, A, D, B, A], the counts are A=3, B=2, C=1, D=1. A valid answer is [A, B, C] or [A, B, D], since the tie for third place is not specified.

What to cover in your answer

  1. A high-level architecture.
  2. The API design.
  3. Data model and storage choices.
  4. How you maintain the rolling window.
  5. The trade-offs between accuracy/consistency and performance.

State your assumptions about scale (distinct items, maximum k, acceptable staleness) and let them drive your decisions.

Hints

Hint 1

Do the arithmetic first: 100K events/s at roughly 50 bytes each is only about 5 MB/s. Decide where the real difficulty lies (bandwidth, or counting and ranking), and whether reads need to touch the raw events at all.

Hint 2

If every event for a given item is routed to the same shard, that shard knows the item's full count. What does that imply about how local top lists can be combined into a global top list?

Hint 3

Split the hour into small time buckets (for example, one per minute) and keep a running total. Think about what must happen when the oldest bucket falls out of the window, and how late-arriving events should be assigned.

Hint 4

If memory for exact counts of a very long tail is too large, consider bounded-memory structures such as Count-Min Sketch with a candidate heap, or Space-Saving. Be ready to state their error behavior.