Part VI — The Blueprint · Lesson 11.6
HLD – Music Streaming (Spotify)
In one lineThe player must survive navigation, so it lives above the router — everything else follows from that.
Where you've seen itClicking through albums, artists, and search while playback never pauses for even a frame.
Diagram
| Step 1–2 | Scope and scale |
|---|---|
| In scope | browse · search · album/playlist · player · queue · offline |
| Out of scope | podcasts · social · ads · artist tools |
| Scale | 600M users · tracks ~3MB at 160kbps · sessions measured in hours, not minutes |
Lessons in this diagram9.1 Service Workers6.5 Indexed DB
How it works
- Single audio element above the router, driven by a global player store (10.5) — routes only dispatch intents.
- Queue as explicit state: context, items, index, shuffle order, repeat mode.
- Prefetch the next track at ~80% and use a second audio element for gapless transitions.
- Media Session API wires lock-screen controls, hardware keys, and metadata.
- Offline stores downloaded audio in IndexedDB and serves it through the service worker.
Trade-offs
| Choice | Cost |
|---|---|
| Player outside router | App shell can't be fully route-code-split |
| Two audio elements | More memory; gapless transitions |
| Prefetch next track | Wasted data if the user skips |
| Offline downloads | Storage quota, licensing, eviction handling |
Interview angle
"Music stops when the user navigates. Why, and how do you fix it?"
- The
<audio>element is inside a route, so navigation unmounts and recreates it. - Hoist a single player instance above the router and drive it from a global store; routes dispatch play/pause intents only.
- Then add Media Session so hardware and lock-screen controls work, which is expected behaviour for anything audio.
Recap
- The player lives above the router — that's the design.
- Queue is explicit state: context, index, shuffle order, repeat mode.
- Prefetch at ~80% and swap audio elements for truly gapless playback.