call(back)

Design Top Songs by Country

Design a service that shows the ten most-played songs in each country. Every time a user plays a song the client reports a listen event. Each country's ranking is independent and refreshes about every ten minutes; a few minutes of staleness is fine, but reading a leaderboard must take well under a second.

Plan for roughly 500 million users who each open a leaderboard about ten times and play about ten songs a day: on the order of 50,000 reads and 50,000 listen events per second. Availability matters more than counting every play exactly.

GET  /songs/top?region=US        -> { region, songs: [{ songId, rank }, ...] }
POST /songs/{songId}/listen      <- { timestamp }

Derive the region from something you trust, not from a field the client sends. Explain what happens when one song suddenly becomes very popular.

Hints

Hint 1

Do the arithmetic first. The read side is a handful of tiny objects, one per country; the write side is a stream.

Hint 2

A database counter incremented on every listen is a hot key the moment a song goes viral. Count in the stream, not in the store.

Hint 3

The refresh interval is a gift: aggregate in ten-minute windows and publish a finished list, rather than maintaining a live ranking.

Asked at

Similar Problems4