Design a Top Songs by Country Leaderboard
Design a near-real-time service that aggregates music activity into cached country leaderboards every ten minutes.
Problem statement
For this exercise, assume a music service receives validated listening and click events and shows the top 10 songs for each country.
The source requires near-real-time results refreshed every ten minutes, eventual consistency, streaming aggregation, and a cached read path at roughly 50,000 event writes and 50,000 leaderboard reads per second.
To make ranking deterministic for this exercise, use a rolling 24-hour event-time window divided into ten-minute buckets. One completed listen contributes one point; click events are ingested and retained for analysis but do not affect the first-version rank.
Order songs by descending listen count, then ascending song ID. A late listen may update any still-open bucket, and published leaderboards may lag by one refresh interval.
Design event ingestion, validation and deduplication, country attribution, partitioning, windowed aggregation, top-10 computation, cache publication, reads, late-event handling, hot-country mitigation, rebuilds, overload, observability, and correctness reconciliation.
Recommendation ranking, personalized charts, royalties, fraud detection, and global charts are outside scope.
What to build
- The service ingests validated listen and click events with stable event identity, country, song, and event time.
- The service maintains rolling 24-hour completed-listen counts and deterministically computes the top 10 songs per country.
- Readers can fetch a country leaderboard and its window and freshness metadata from a low-latency cached path.
- Late, duplicate, or replayed events update open windows without double counting and support bounded rebuilds after recovery.
At scale
- The source reports roughly 50,000 event writes and 50,000 leaderboard reads per second.
- The source requires a near-real-time cached leaderboard refreshed every ten minutes with eventual consistency.
- For this exercise, rank completed listens in a rolling 24-hour event-time window built from ten-minute buckets.
- For this exercise, accept events up to one hour late, retain replayable event logs for seven days, and expose leaderboard window and freshness metadata.