Skip to the post

How Spotify works: playing a song and counting every play

Spotify system design explained simply: how a song starts in a blink, what counts as a play, offline plays on a plane, and counting billions of plays once.

· 8 min read · High-level design (HLD)

It's Wrapped season. Your phone tells you that you played one song 412 times this year, and that you're in the top 2% of some band's listeners. Fun to share. But behind that card is a serious engineering problem, because the same play counts decide how much money every artist is paid. Getting them right is the heart of any Spotify system design.

This post builds a music streaming service that works like Spotify, step by step, with numbers. Spotify hasn't published every detail of its own design, so we'll design one the way an interviewer would ask you to: first how a song starts playing the moment you tap it, then how every play is counted exactly once, even the ones you listened to on a plane.

How big is Spotify?

Spotify said it had 788 million monthly users in its third-quarter 2026 results, 306 million of them paying. That's a lot of people pressing play.

To reason about a design, we'll use round example numbers, the kind you'd pick in an interview:

  • 200 million people listen each day, for about 2 hours each.
  • An average song is about 3.5 minutes, so each person plays about 34 songs a day.
  • That's 200 million × 34, roughly 7 billion plays a day. Divide by 86,400 seconds and it's about 80,000 plays a second, more at the evening peak.

Hold on to that 7 billion. It comes back when we count.

How does a song start playing so fast?

You tap a song and it plays almost at once. A good target is under 300 milliseconds, about the time it takes to blink. How?

First, a question. Have a guess: how much storage does the entire catalogue need?

Say there are 100 million tracks. A 3.5-minute song at 160 kbps, the quality Spotify calls "High", is about 4 MB. Keep three quality levels of each, and it's around 15 MB a track. So the whole catalogue is about 1.5 PB.

That sounds huge, but compare it with a video site. A site like YouTube takes in hundreds of petabytes of new video every year. Music is tiny next to that. It's so small that a CDN, the network of cache servers spread around the world, can keep the whole catalogue in its larger regional caches, close to listeners.

The whole catalogue is small: 100 million tracks at about 15 MB each in three qualities is about 1.5 PB, small enough for a CDN's regional caches to hold all of it, against about 320 PB of new video a year for a video site.
The whole music catalogue is about 1.5 PB, small enough for a CDN to hold all of it

So songs are stored as plain files, one per quality level, and served from the CDN. With the files close by, two tricks make the start feel instant:

  • Prefetch the next song. While you listen, the app quietly downloads the first few seconds of the next track in your queue. When the song changes, those seconds are already on the phone.
  • Start small, then improve. For a song you tap out of nowhere, the app asks for just the first 2 seconds at the lowest quality, about 24 KB. That arrives fast even on a weak connection, so playback starts, and the rest streams in at better quality while you listen.
How a tap starts playing fast: the phone asks the nearest CDN server for the first 2 seconds at the lowest quality, about 24 KB, starts playing, then fetches the rest at higher quality while you listen.
A tap fetches 2 seconds at low quality first, then the rest at higher quality

Make sense? Good. Playing music is the easy half. Now for the half that involves money.

What counts as a play on Spotify?

Spotify's own rule for artists is simple: a stream counts when someone listens for 30 seconds or more. Skip a song after 10 seconds and it isn't a play.

So the phone is the one that knows. When a song passes the 30-second mark, the app creates a small record, a play event:

  • a play ID, a random unique number the phone makes up on the spot
  • the track, the listener's country, and the time it happened

The phone sends the event to the server, which puts it on a log, an append-only list such as Kafka. From there, the events are saved to an archive and fed to the counting jobs.

Why does the phone make the ID, and not the server? Hold that thought. The answer is on a plane.

What about plays on a plane?

You download an album before a long flight and play it all the way across the ocean. Your phone has no connection for 12 hours. Those plays still have to be paid.

So the phone keeps every event it hasn't been able to send. When it's back online, it sends them. And it keeps resending each one until the server confirms "got it". Spotify's help pages say downloaded music is counted once the listener comes back online, which they must do at least once every 30 days.

Now think about what can go wrong. The phone sends an event, the server saves it, and the confirmation gets lost on a flaky airport Wi-Fi. The phone never heard "got it", so it sends the same event again. Now the server has it twice.

A play event made on the phone after 30 seconds of listening: a play ID, the track, the country and the time. The phone keeps it and resends it until the server confirms, so a play heard on a plane arrives later, and a resend with the same ID is counted once.
A play event is made on the phone after 30 seconds and resent until confirmed

This is why the phone makes the play ID. The resend carries the same ID as the first try. When it's time to count, the server counts distinct play IDs, so the duplicate simply disappears. A play is paid once, whether it arrived once or five times.

Pax asks: Couldn't the server just give each play an ID when it arrives?

It could, but then a resend would arrive looking like a brand-new play, with a brand-new ID, and nobody could tell the two apart. Only the phone knows "this is the same play I tried to send an hour ago". So the ID has to be born on the phone, before the first send.

Two counts: the live one and the one that pays

Here's the part interviewers like to dig into. You need two very different kinds of count from the same stream of events.

The live count is the one you see in the app and the one artists watch on launch day. It must be fast, updated every few seconds. But it doesn't have to be perfect. If it's off by a few plays for a minute, nobody is harmed.

The paying count decides the money. It must be exact: every play that passed 30 seconds, counted once, per track and per country. But it can be slow. Artists are paid monthly, so a count that's ready a week after the month ends is fine.

Two counts from one stream of play events: a live count that is fast and close enough for the app, and a monthly count of distinct play IDs over the archive that is exact and pays artists.
Two counts from one stream of events: a fast live count and an exact monthly count

So split them. A stream job reads the log and keeps a running count, removing the duplicates it sees within a short window. That's the live number. Separately, once a month, a batch job reads the whole month from the archive and counts distinct play IDs for each track and country. That's the number that pays.

Couldn't the stream job just be exact, and skip the batch? Let's check with our example numbers. To catch a resend that arrives up to 30 days late, plus a week before the month closes, the job would have to remember every play ID for about 37 days. That's 7 billion a day × 37, about 260 billion IDs. At 16 bytes each, it's around 4 TB of IDs to keep in fast storage and check on every single event.

The batch job, by contrast, just scans files. A month is about 210 billion events of roughly 100 bytes, around 21 TB. On a cloud data warehouse that charges about $6.25 for each tebibyte it scans, that's about $120 a month.

Item Exact in the stream job Monthly batch over the archive
How it removes duplicates Remembers every ID for ~37 days Counts distinct IDs in one scan
Cost (example numbers) ~4 TB of IDs, checked on every event ~21 TB scanned, about $120 a month
How quickly it's final At once About a week after the month ends
Choose when You must pay daily Monthly pay is fine, as it is here

And what about a play that turns up after the month has closed, from a phone that was offline a long time? The next run counts it, recounts that month, and pays only the difference. Nothing is lost, and nothing is paid twice.

Where you've met this design

  • Spotify Wrapped is the same idea at a bigger scale: one huge batch job over a year of play history. Spotify's engineers wrote in 2020 that Wrapped processed a decade of listening for over 248 million users, about five times the data of the previous campaign, on Google Cloud Dataflow.
  • Spotify's royalty rules sit on top of these counts. Since early 2024, a track needs at least 1,000 streams in the past 12 months to earn recorded-music royalties, a rule that only works if every stream is counted carefully.
  • Ad clicks and video views use the same split. A fast count for the dashboard, and a slower exact count, without duplicates, for the bill.

What interviewers ask next

Once you've drawn the two counts, expect follow-ups like these. Try each one before you look anything up:

  • Someone runs a farm of 10,000 phones that play one track all day to collect royalties. How would you spot it, and stop paying for it?
  • The log confirms a play to the phone, then a whole region goes down before the event reaches the archive. Is that play lost?
  • The music label wants the 30-second rule changed to 45 seconds from next month. What in your design has to change?

The full guide, Problem set: social and content (dating app like Tinder, voting and comments like Reddit, music streaming like Spotify, news aggregator like Google News), answers each one, with the numbers to use and what to say in two minutes.

Recap

Playing music is mostly a storage question, and the answer is pleasant: the whole catalogue is small enough for a CDN to hold, so a song starts from a nearby server, with the first 2 seconds at low quality and the next track already prefetched. Counting plays is the hard half. The phone creates each play event after 30 seconds, with its own ID, and resends it until it's confirmed, so plays on a plane arrive late but arrive. Duplicates vanish because the paying count adds up distinct IDs once a month, while a separate live count keeps the app fast.

Sources (7)
Free weekly email

One interview idea a week

A 3-minute read: one question interviewers ask, an everyday picture, the real numbers and what to say.

Check your inbox. If that address is new here, a link to confirm it is on its way. Tap it, and the first email comes within a week.

That address didn't look right. Check it and try again.

We couldn't sign you up just now. Please try again in a few minutes.

One email a week. Unsubscribe with one click from any email. Privacy

More from the blog

All posts