KatInterview

Real-Time Top-K Noisy-Service Ranker Over a Rolling Window

System DesignModel answer
MediumModel answerSystem Designstream-processingsliding-windowtop-k

Last updated

Design an end-to-end pipeline that ingests high-volume error events and serves a live ranking of the K noisiest services over the last five minutes.

Context

A large cloud platform runs tens of thousands of independently deployed microservices. Each service emits structured error events at very high volume. During an outage, on-call engineers need a live dashboard that answers one question: which K services are throwing the most errors right now?

You are asked to design the complete system — from log ingestion through aggregation to the query API — that powers this dashboard.

Input Schema

Each error event contains:

  • timestamp — event time (when the error occurred on the service)
  • service_id — identifier for the emitting service
  • error_code — category of the error
  • trace_id — distributed trace identifier

API to Implement

GET /noisiest?k=10&at=<unix_seconds>

Returns the top-K services by error count over the 5-minute window ending at the requested time, along with each service's count, whether the result is final or provisional, and a staleness indicator.

Functional Requirements

  1. Ingest the structured events described above at peak throughput.
  2. Implement the /noisiest endpoint with correct sliding-window semantics based on event time (not ingestion time).
  3. Accept logs that arrive up to 60 seconds late. Events beyond that threshold must not be silently discarded — route them elsewhere and count them.
  4. Support replaying logs from durable storage to reconstruct window state after a failure.

Non-Functional Requirements

DimensionTarget
Query latency< 50 ms
Log-to-dashboard lag< 10 s
Ingestion throughput1,000,000 events/s peak
Approximate log size500 bytes each (~500 MB/s)
Number of active services50,000
Query volume~200 QPS
Window size5 minutes, refreshed every 10 s

Exact error counts are preferred over probabilistic approximations. The pipeline must tolerate broker failovers and aggregation worker crashes without losing data or double-counting.

What Your Answer Should Cover

  • Architecture overview — durable ingestion, stream aggregation, and serving tiers
  • Windowing model — how you divide the 5-minute window into sub-buckets and maintain a running sum efficiently
  • Late-arrival handling — watermarks, allowed lateness, provisional vs. final results
  • Hot-key / skew mitigation — how you prevent a single noisy service from overwhelming one worker
  • Top-K mechanics — how partial counts from distributed workers are merged into a correct global ranking
  • Failure handling — broker outage, worker crash, state recovery, and replay
  • Correctness verification — how you detect and alert on data discrepancies

Hints

Hint 1

Start with back-of-envelope math. 1M events/s × 500 B = 500 MB/s. How many Kafka partitions do you need? How much memory does a per-service count over 30 ten-second buckets actually require? Answering these questions will show you that the challenge is throughput and skew, not storage.

Hint 2

A pure sliding window that tracks every individual event is expensive to maintain. Think about approximating it with a series of fixed-width sub-buckets (tumbling micro-windows) and keeping a running sum — what granularity do those buckets need to be, and what accuracy trade-off does that introduce?

Hint 3

If you partition the raw event stream by service_id, a single noisy service gets all its traffic on one partition and one worker. What two-stage aggregation pattern avoids this? After the first stage fans traffic out by a random or trace-based key, what property of the second stage makes the final top-K merge exact rather than approximate?