KatInterview

Streaming Ranking of the Busiest Users Over a Rolling Hour

System DesignModel answer
MediumModel answerSystem Designstream-processingtop-ksliding-window

Last updated

Design a fault-tolerant stream pipeline that keeps an exact, event-time ranking of the 100 users with the most events in the last 60 minutes, tolerating late data and skew.

Context

A multi-tenant cloud product needs a live "most active accounts" view. Abuse-detection and capacity-planning services poll it to see which users generated the most activity during the last hour.

Design a streaming system that consumes raw user activity records and continuously publishes the 100 users with the highest event counts over the trailing 60 minutes, measured by when events happened, not when they arrived.

Inputs and Interface

  • Each record carries (user_id, event_type, timestamp, event_id).
  • Consumers call GET /v1/top-users?window=60m and receive the current ranking.

Required Properties

  • Freshness: the ranking is refreshed every 10 to 60 seconds.
  • Correctness: events may arrive out of order, up to 5 minutes late, and must still be counted. Retransmitted duplicates must not inflate counts.
  • Durability: losing a worker must not reset or corrupt the window counts.
  • Elasticity and skew: the system must absorb sudden bursts and extremely uneven per-user traffic.

Scale

  • 100,000 events/s typical, 300,000 events/s at peak
  • About 10 million distinct users active within any one hour
  • Roughly 500 bytes per event

Your answer should address

  1. Ingestion and the choice of stream engine
  2. How the trailing-hour state is kept without recomputing from raw events on every slide
  3. How hot users are handled and how partial results combine into one global top 100
  4. Event-time handling, watermarks, deduplication and end-to-end exactly-once behavior
  5. State storage, checkpointing, state size estimates and recovery

Hints

Hint 1

Do the arithmetic first: bandwidth, events per hour, and per-user state size. You will find the live state is only a few GB, so exact answers are affordable.

Hint 2

A native 60-minute window sliding every 10 seconds assigns each event to 360 windows. Think about small fixed-size time buckets per user that you add to and expire incrementally.

Hint 3

Partition the first stage by something other than user_id so one noisy user cannot overload a single partition, pre-combine counts, and only then group by user_id so each user's total lives in exactly one place before ranking.

Hint 4

Separate lateness tolerance from dedup retention: the dedup state only needs to live as long as late arrivals and retransmissions can occur, not as long as the window.