KatInterview

Design a Real-Time Sliding Leaderboard for Popular Merchants

System DesignModel answer
MediumModel answerSystem Designsystem-designstream-processingleaderboard

Last updated

Architect a high-throughput stream processing pipeline to track top-10 global and per-city merchants over a 60-minute trailing window with low latency.

Design an analytics system that tracks and serves the top 10 most popular merchants (by completed order count) across the entire platform as well as per city over a rolling 60-minute time window.

Functional Requirements

  1. Top Merchants API: Expose an endpoint getTopMerchants(scope, location_id, limit) returning the current top 10 merchants (globally or for a specific city) in the trailing hour.
  2. Real-time Updates: Process order events continuously to maintain up-to-date rankings.
  3. Late-Arriving Events: Gracefully handle out-of-order and slightly delayed event arrivals using event timestamps.

Non-Functional Requirements

  1. Scale: Ingest up to 100,000 order events per second globally across ~1,000,000 distinct merchants.
  2. Freshness & Latency: Rankings must reflect state within 5 seconds of event receipt. Read endpoints should respond within 10 milliseconds.
  3. Fault Tolerance & Resilience: The system must sustain worker node failures without losing state or outputting incorrect results over time.

Tasks

  • Detail the ingestion and processing pipeline architecture.
  • Explain the temporal aggregation and window sliding strategy (evaluating exact vs. approximate techniques).
  • Describe how late-arriving data, state persistence, and fault tolerance are handled.
  • Demonstrate how the architecture scales out to handle per-city metrics cleanly.

Hints

Hint 1

Consider total key space size before defaulting to probabilistic algorithms like Count-Min Sketch. Is 1M keys manageable in memory?

Hint 2

Avoid native sliding windows in stream processing engines—sliding a 60-minute window every second multiplies state management work by 3,600x. Use fixed 1-minute buckets inside a running aggregator instead.

Hint 3

Combine local top-K extraction at partition workers before sending candidates to a global merger to prevent network bottlenecks.