AlgoViz

Design a music streaming service like Spotify

hard 50 min

Any 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

0:00 / 4:41

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

  1. October 2008

    Spotify launches in several European countries.

  2. April 2014

    Spotify moves away from peer-to-peer delivery from listener to listener, and relies on servers.

  3. July 2015

    Spotify introduces Discover Weekly, a personal playlist renewed on Mondays.

  4. 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.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

Understand it

The jukebox caravan
JUKEBOXwhole recordWait for it all

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

Listeners fetch audio from a CDN, which falls back to a regional cache and audio storage, filled by an encoder. Their API calls go through a gateway to the playback API (catalogue in PostgreSQL, manifests in Redis, offline licences), the discovery API (Elasticsearch and ready mixes) and the device hub. Play events go through Kafka to Cassandra and royalty totals, and Spark makes the weekly mixes.
Blue is the listening path, pink is publishing and writing, orange is asynchronous. Dashed means only sometimes or later.

Ideas and where they are explained

Idea hereConcept page
Pieces kept near listenersCDN and content delivery network
Cheap, durable audio filesObject storage
Play eventsLog-based queues and idempotency
SearchFull-text search and autocomplete
MixesRecommendation system
DevicesReal-time APIs
History storeLSM tree and partitioning

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