System design: show the most songs in the last 7 days

Michael Tong
2 min readJun 30, 2023
Photo by Aliane Schwartzhaupt on Unsplash

Today we will tackle a system design question: Show the songs that were most popular in the last 7 days.

To understand this problem, we need to understand the non-functional requirements for this problem. Before we head into this part, let’s dive into the workflow first:

  1. User interacts with the service as a client/UI and plays a song.
  2. The UI hits the server and updates the count of the song.
  3. The server interacts with the database, updates the count of the song along with the date of the song.
  4. When the UI needs to show the list of most popular songs, the server returns this updated information.

Sounds simple enough right? Here is the problem: we can expect plenty of people playing songs so we have to understand how to handle a huge volume of api calls.

Because of the nature of this problem, we need to ask ourselves this question: is it important for users to be able to see the most updated songs at all time, or can we tolerate a bit of delay in that and allow system to update the list of most updated songs periodically?

Let’s ask ourselves this question: do people really care about seeing the most popular songs every single second? Let’s say for today Beyonce’s songs are the most popular and perhaps there are over 10 million people checking her songs and perhaps other people’s songs. Unless I am using an application that requires constant updates, such as video streaming, otherwise we can tolerate a slight bit of delay.

Would the list of most popular songs change over seconds or even minutes? Not really.

Let’s set things up this way:

  • we can maintain a cache to maintain the list of top 10 songs. If the user accesses a song, it will update the database instead. We can run an hourly pipeline to update the cache with the list of most popular songs. That way users can still see the most popular songs quickly, even if there is going to be a bit of consistency.

--

--