Preparing your design workspace

Loading the problem and your latest saved attempt…

Design a Sliding-Window Restaurant Leaderboard

Design a seconds-fresh top-ten restaurant leaderboard over a sliding one-hour order window.

Problem statement

Design a service that shows the top 10 restaurants by order count over a sliding one-hour window with seconds-level freshness. Queue order events, compute counts in a stream processor, and publish materialized results to a query store. Use sixty one-minute buckets instead of rescanning raw events, split hot restaurant keys and merge their partial counts, deduplicate by event ID, apply event-time watermarks and a lateness cutoff, and recover from checkpoints plus retained-log replay. Approximation is allowed, including Count-Min Sketch with a declared overestimation error. Extend the design to top 10 per city. For this exercise, assume one million order events per second globally, 20,000 cities, p95 result freshness below five seconds, a five-minute lateness allowance, and 99.99% query availability.

What to build

  1. Accept order events idempotently with event time, restaurant, and city identity.
  2. Return the materialized top ten restaurants for the preceding event-time hour with freshness and completeness metadata.
  3. Return the same sliding-window leaderboard scoped to one city.

At scale

  • Source-backed behavior: maintain sixty one-minute buckets, merge hot-key partials, and recover through checkpoints plus retained-log replay.
  • Source-backed option: an approximate heavy-hitter structure may overestimate counts when its error is explicit.
  • Practice assumption: ingest one million order events per second across 20,000 cities and publish p95 results within five seconds.
  • Practice assumption: accept events up to five minutes late and serve leaderboard reads at 99.99% availability.