Design a music streaming service like Spotify
hard 50 minAny of 100 million songs starts within a fifth of a second, keeps playing on a bad network, follows the listener from phone to speaker, and is counted once so that artists are paid.
Video lesson · Chapter 1 of 5
The song begins before the whole track has arrived
Theory
Read or listen first. About 2 min.
What it is
A music streaming service keeps a large catalogue of recordings and plays any of them on request. A service like Spotify or Apple Music adds search, playlists, personal mixes, downloads for paying members and playback that moves between devices.
The song is cut into small pieces, each stored at several sizes. The player asks for a few pieces ahead, and chooses the size that its network can carry. Most of the work is done by the player and by caches near it, not by our services.
This page is an exercise. It is inspired by well-known products and does not claim to describe how any company built its system.
Why it exists
Owning music means choosing, buying and storing it. Access to everything means a person can try a song at once, so waiting even a second feels broken.
For the designer it joins two very different systems: one that moves terabits of audio, and one that answers small questions about taste, rights and money. Keeping them apart is most of the answer.
A short history
October 2008
Spotify launches in several European countries.
April 2014
Spotify moves away from peer-to-peer delivery from listener to listener, and relies on servers.
July 2015
Spotify introduces Discover Weekly, a personal playlist renewed on Mondays.
August 2017
RFC 8216 describes HTTP Live Streaming, where a playlist file lists short pieces at several bit rates.
Words to know
- Bit rate
- How many bits each second of sound uses. 320 kbit/s is 40 KB a second.
- Piece (segment)
- A few seconds of audio that can be fetched alone.
- Manifest
- The list of pieces and qualities of one track, with addresses the player may use.
- Buffer
- Audio already fetched and not yet played.
- Signed URL
- An address that works for a short time because it carries a signature.
- DRM licence
- Permission, with a key, that lets one device decrypt a track for some time.
- Long tail
- The very many tracks that are each played rarely.
- Embedding
- A list of numbers that places a song or a listener near similar ones.
Solve it like an interview
New to it? Start with Approach. Revising? Go straight to the mock.
- 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
- 2ScopeAsk what changes the design, then commit to a short list.
- 3NumbersWork out a number when the answer changes what you build.
- 4API and dataThe contract first, then the data that serves it.
- 5DesignStart with the smallest system that works. Change it only when something breaks.
- 6Deep divesCompare the options before you open them, then say which you would pick.
- 7Explain your choicesAnswer first. Then compare your reasoning.
- 8By levelThe same question is judged differently for mid, senior and staff.
- 9MockAnswer against the clock, then score yourself.
Understand it
The first bars come at once, the rest rolls in behind, and somebody else counts the tickets.
Scope
What are we building?
Play any track at once, search the catalogue, keep playlists, offer personal mixes, download for paying members and move the music between devices. Podcasts, social features, artist uploads and advertising are left out. The podcast platform is a different problem: there we do not own the player.
Start at once
Under 200 ms from tap to sound on a good network.
Never stop
Quality goes down before the music does.
Find the next song
Search now; weekly mixes prepared earlier.
Count fairly
Each real play once, because money follows it.
The finished system
Where the design ends up
The whole system
Ideas and where they are explained
| Idea here | Concept page |
|---|---|
| Pieces kept near listeners | CDN and content delivery network |
| Cheap, durable audio files | Object storage |
| Play events | Log-based queues and idempotency |
| Search | Full-text search and autocomplete |
| Mixes | Recommendation system |
| Devices | Real-time APIs |
| History store | LSM tree and partitioning |
Continue in the Interviews tabs: Approach, Scope, Numbers, API and data, Design, Deep dives, Explain your choices, By level and Mock.
Summary
The whole topic in one minute.
A play has two halves. A small request checks rights and returns a manifest. Then the player fetches pieces from the CDN and adapts the quality to its own network and buffer. Our services never carry audio.
Everything else is slower and separate: play events flow through a log to history, royalties and weekly mixes; search has its own index; one server-ordered state per listener decides which device plays; and offline licences open files that are the same for everybody.
- Audio goes from cache to player; services only answer small questions.
- Start with a small piece of low quality, then climb.
- One encrypted file for everybody; the licence is personal.
- A server-ordered version decides which device plays.
- Plays are events with an id, so a retry cannot pay twice.
Sources
- RFC 8216: HTTP Live Streaming
- W3C: Encrypted Media Extensions
- Apache Cassandra: CQL data types (counters)
- Spotify support: audio quality
- Spotify support: listen offline
- Spotify for Artists: how streams are counted
- Spotify for Artists: loudness normalization
- Spotify Web API: playlists and snapshots
- Spotify Engineering: Voyager nearest-neighbour search
- Wikipedia: Spotify (dates)