Designing Spotify - Music Streaming Platform
Difficulty: Intermediate Topics: Audio Streaming, CDN, Offline Sync, Play Counting Asked at: Spotify, Amazon, Google, Apple · Prerequisites: CDN and Idempotency
1. Understanding the Problem
Spotify is an audio streaming service. You search a catalog of roughly 100 million tracks, tap one, and sound comes out of the speaker almost immediately. You collect tracks into playlists and reorder them. You download an album before a flight and it plays in airplane mode. Behind all of that, every play that crosses a threshold has to be counted, because a rights holder gets paid for it.
Real examples: Spotify, Apple Music, YouTube Music, Amazon Music, SoundCloud, JioSaavn.
If you have already read the Netflix design, do not paste it here. Video and audio streaming share a delivery story and almost nothing else. The differences are not cosmetic - they move the hard problems to different places.
| Dimension | Netflix | Spotify |
|---|---|---|
| Asset size | ~3 GB per variant per title | ~3.5 MB per track at 128 kbps (kilobits per second) |
| Catalog size | ~15K titles | ~100M tracks |
| Session shape | 1-2 hours, foreground, one asset | 2-3 hours, background, 40+ assets back to back |
| Transition cost | Nobody notices a 1s gap between episodes | An audible gap between album tracks is a bug report |
| Offline | Nice-to-have | A headline feature people pay for |
| Per-asset accounting | None - nobody is paid per view | Every qualifying play is a royalty event |
Two consequences fall out of that table and they shape everything below. First, the catalog is four orders of magnitude larger than Netflix’s while each file is three orders smaller, which flips the cache problem: you cannot pre-position 100M tracks everywhere, so cache hit rate on the long tail is the thing to worry about. Second, the write path matters here in a way it does not for video. Netflix tracks a play position for convenience. We track plays because money moves.
💡 Cache hit rate is the share of requests the cache answers without going to the origin. The “long tail” is the millions of tracks almost nobody plays - too many to keep warm everywhere, and collectively a large fraction of listening. Learn more →
2. Naive First Cut
flowchart LR
PHONE["Phone App"]
API["API Server"]
DB[("Postgres<br/>tracks and play counts")]
FS[("File Server<br/>one mp3 per track")]
PHONE --> API
API --> DB
API --> FS
One file per track on a file server. The client downloads the whole file, then plays it. Play counts live in an integer column on the track row, incremented on each play. Playlists are rows with an integer position.
Why this breaks:
- Whole-file download before playback - 7 MB over a 5 Mbps (megabits per second) mobile link is 11 seconds of staring at a spinner, against a 200 ms budget. And the bytes all come from one origin: 50M concurrent streams at an average 160 kbps is 8 Tbps (terabits per second), which no single fleet serves and which would dwarf every other line item at list-price egress.
💡 The conversion is the easy place to go wrong: file sizes are quoted in bytes and links in bits, so 7 MB is 56 megabits, and 56 ÷ 5 is the 11 seconds. Egress is data leaving your network toward users, and it is usually the line item you are billed for. - One counter row per track - a track in a viral moment gets hundreds of thousands of increments a second against one row. That is a write hotspot, read-modify-write loses updates under contention, and the lost ones are royalty payments.
💡 A hot partition is one key or row taking traffic the rest of the store never sees, so adding machines does not help. “Lost update” is the classic version of it: two writers both read 100, both write 101, and one play is gone. Learn more → - Integer playlist positions - dragging a track to the top of a 10,000-track playlist rewrites 10,000 rows. Two devices doing it offline produce a playlist neither user asked for.
- Offline means handing over the file, and search does not exist - a plain mp3 in the app sandbox is a copy operation away from being everywhere, and
LIKE '%love%'against a 100M-row table is a sequential scan with none of the typo tolerance people expect from a search box.
💡 A sequential scan means reading every row because no index can help: a wildcard at the front of the pattern defeats a normal index, so the cost grows with the whole table rather than with the number of matches. Learn more →
The rest of this doc evolves that sketch into a pre-transcoded, edge-delivered catalog with an auditable play ledger.
3. Prior Art We’re Drawing From
- Spotify’s move off peer-to-peer delivery - the original desktop client was a hybrid: a server delivered the first seconds of a track so playback could start, and peers supplied the rest. Described in Kreitz and Niemelä, Spotify - Large Scale, Low Latency, P2P Music-on-Demand Streaming (IEEE P2P, 2010). The P2P network was retired in favour of server and CDN delivery as mobile became the dominant client. The pattern worth stealing survives the retirement: split the first chunk from the rest and treat them as different latency problems. (Spotify Engineering)
- Netflix Open Connect - the economic argument for owning delivery infrastructure rather than renting per-GB. Audio volumes are smaller than video but still land in exabytes per month, so the same arithmetic applies with a smaller constant. (Open Connect)
- Kafka, originally built at LinkedIn - a replayable, partitioned, ordered log. This is the piece that lets one stream of play events feed both an approximate live counter and an exact financial ledger without either consumer knowing about the other. Pair it with dependency-aware, re-runnable batch tooling in the spirit of Spotify’s Luigi and Scio, because the royalty side needs the ability to recompute last March from raw events rather than low latency. (Apache Kafka, github.com/spotify/luigi)
💡 Luigi and Scio are batch job frameworks: they let you re-run a pipeline over old data and get the same answer, which is what “recompute last March” requires. Learn more → - Spotify Annoy - an approximate nearest neighbour (ANN) index Spotify open-sourced out of its music recommendation work. The contribution here is the shape of the serving tier: embed items as vectors, then answer “what sounds like this” as a nearest-neighbour lookup instead of a join. (github.com/spotify/annoy)
💡 An embedding turns a track into a list of numbers (a vector) positioned so that similar-sounding tracks land near each other. “Approximate nearest neighbour” means finding the closest few vectors fast by accepting that you might miss a slightly closer one.
4. Functional Requirements
Core (Top 3)
- Stream a track on demand - user taps a track, audio starts near-instantly and plays through without a rebuffer
- Build and play playlists - create a playlist, add and remove tracks, reorder them, play it start to finish
- Download for offline listening - save tracks to the device and play them with no network
Discussion
Newest first