KatInterview

Real-Time Ranking Service for Delivery Couriers

System DesignModel answer
MediumModel answerSystem Designleaderboardrediskafka

Last updated

Design a high-throughput system that ingests courier ratings and serves a low-latency top-K leaderboard under eventual consistency.

Context

A logistics platform tracks courier performance through post-delivery ratings. You have been asked to design the backend service that powers a courier rankings page, updated in near real-time.

What You Need to Support

Ingestion record_rating(courier_id, rating) — accepts a numerical score (1 through 5) after a delivery is completed.

Query top_couriers(K) — returns the K highest-ranked couriers by average rating, in descending order.

Scale and Quality Targets

  • Volume: 1 million active couriers; up to 10,000 ratings per second at peak.
  • Freshness: The leaderboard does not need to reflect every rating the instant it arrives. An update delay of a few seconds is acceptable.
  • Availability: Read queries must stay up even if parts of the write pipeline are degraded.

What to Cover in Your Answer

  1. API design — endpoints, request/response shapes, and any relevant HTTP semantics.
  2. Storage choices — compare relational databases, NoSQL stores, and in-memory caches for different parts of the system; justify your picks.
  3. Data flow — walk through how a rating moves from ingestion all the way to influencing the leaderboard.
  4. Hot-key and skew handling — some couriers receive far more ratings than others; explain how your design holds up.

Example

record_rating("courier_42", 5)
record_rating("courier_7",  4)
record_rating("courier_7",  5)

top_couriers(2)
→ [
    { courier_id: "courier_7",  avg_rating: 4.5, review_count: 2 },
    { courier_id: "courier_42", avg_rating: 5.0, review_count: 1 }
  ]

(Ranking order assumes the composite scoring rule you define.)

Hints

Hint 1

Start with a back-of-envelope calculation. How large is 1 million courier aggregates in memory? The answer changes how aggressively you need to shard the leaderboard store.

Hint 2

Think about what you actually store per courier. If you keep a running sum and count rather than a pre-computed average, incremental updates are exact and O(1). What does that mean for the schema?

Hint 3

The Redis sorted set is not your source of truth — it is a derived view you can always rebuild. How does that framing change your availability and consistency strategy?