0%
System requirements
Functional
Capacity estimation
Traffic
Storage
API design
Streaming API
Database design
Schema
Storage Engine Choices
High-level design
Components
Request flows
Song Playback Flow
Detailed component design
Adaptive Bitrate Streaming: The Core Challenge
Trade-offs and tech choices
Push vs Pull for Recommendations
Failure scenarios and bottlenecks
CDN Edge Failure
Future improvements
Real-Time Lyrics Sync
Collaborative Playlists with Real-Time Sync
System requirements
Functional
Capacity estimation
Traffic
Storage
API design
Streaming API
Database design
Schema
Storage Engine Choices
High-level design
Components
Request flows
Song Playback Flow
Detailed component design
Adaptive Bitrate Streaming: The Core Challenge
Trade-offs and tech choices
Push vs Pull for Recommendations
Failure scenarios and bottlenecks
CDN Edge Failure
Future improvements
Real-Time Lyrics Sync
Collaborative Playlists with Real-Time Sync
Spotify is a music streaming platform that serves audio content on demand to hundreds of millions of users worldwide. The system must let users play any song from a catalog of 100M+ tracks with near-instant start times, create and manage personal playlists, search across songs, artists, and albums, and receive personalized recommendations based on listening history.
GET /v1/stream/songs/{songId}?quality={bitrate}
Returns a response containing a signed CDN URL for the audio file, song metadata (title, artist, album art URL, duration), and the available bitrate options. The signed URL contains an HMAC signature, expiry timestamp (15 minutes), and the user's nearest CDN region. The client uses this URL to fetch audio chunks directly from the CDN, the backend never serves audio bytes.
| Table | Key Columns | Notes |
| songs | song_id (PK), title, album_id (FK), duration_ms, audio_key, genre, play_count | audio_key = S3 object key prefix |
| artists | artist_id (PK), name, bio, image_url, monthly_listeners | |
| song_artists | song_id + artist_id (composite PK), role | Many-to-many; role: primary, featured, producer |
| albums | album_id (PK), title, artist_id (FK), cover_url, album_type | album_type: album, single, EP |
| users | user_id (PK), email (UNIQUE), display_name, plan_type, country | plan_type: free, premium, family |
| playlists | playlist_id (PK), owner_id (FK), title, is_public, version, song_count | version for optimistic concurrency |
| playlist_songs | playlist_id + song_id (composite PK), position, added_at | position enables user-defined ordering |
| play_events | event_id (PK), user_id, song_id, duration_ms, context | Append-only, partitioned by month |
| follows | follower_id + followed_id (composite PK) | Social graph |
Client apps (mobile, web, desktop): Handle audio playback, local caching for offline mode, UI rendering, and adaptive bitrate switching. The client is "smart", it manages its own playback buffer, prefetches upcoming songs, and queues play events for retry if offline. This pushes complexity to the client and keeps backend services stateless.
GET /v1/stream/songs/{songId}?quality=128 to API Gateway.SELECT audio_key, duration_ms FROM songs WHERE song_id = $1. Checks if requested quality is within user's plan limit.https://cdn.example.com/{audio_key}/128.opus?sig={hmac}&exp={timestamp}. Signs with HMAC-SHA256 using a shared secret between backend and CDN.{url: "...", duration_ms: 210000, title: "...", artist: "...", albumArt: "..."}.POST /v1/events/play with duration, context, and timestamp. Gateway publishes to Kafka topic play-events.This is the heart of the streaming experience. The system must deliver uninterrupted audio to 10M concurrent users across wildly varying network conditions, from 5G in Manhattan to 3G on a rural highway.
Pre-compute (push): Generate Discover Weekly, Daily Mix, and similar playlists in a nightly batch job on Spark. Store results in Redis/DynamoDB keyed by user_id. Serving is a simple key-value lookup, under 5ms latency. Downside: recommendations are up to 24 hours stale. A user who discovers a new genre at 9 AM won't see updated recommendations until the next morning's batch run.
On-demand (pull): Compute recommendations at request time using pre-built user and song embeddings. Run nearest-neighbor search in a vector database (Pinecone, Milvus). More responsive to recent behavior but adds 100-500ms latency per request and requires a large serving cluster.
A CDN PoP goes offline (hardware failure, network partition). Impact: users in that region experience audio buffering or playback failure. Mitigation: CDN providers automatically reroute traffic to the next-nearest PoP via anycast BGP. The failover happens at the network layer within seconds, the client's next chunk request goes to a different edge with no code changes. For extended outages, the multi-CDN strategy kicks in: DNS-level failover routes the region's traffic to the secondary CDN provider. The client continues requesting the same signed URL, the secondary CDN validates the same HMAC signature (shared secret distributed to both CDNs).
Key insight: The client's 10-second buffer buys time. Even if failover takes 5 seconds, the user hears no interruption because the buffer covers the gap. This is why the buffer exists, not just for bandwidth fluctuations, but for infrastructure failover.
Display time-synced lyrics alongside playback. Each lyric line has a start and end timestamp (millisecond precision). The client renders lyrics in sync with the audio playback position. Data source: third-party lyrics providers (Musixmatch) supply timestamped lyric data via API. Stored as a JSON array per song in a separate lyrics table. Served via CDN alongside audio metadata. Challenge: different audio encodings may have slight timing offsets, the client must calibrate lyric timing against the actual audio decode position, not wall clock time.
Reading a solution is not the same as producing one under interview pressure. Design it on the whiteboard and get scored feedback.
0%
System requirements
Functional
Capacity estimation
Traffic
Storage
API design
Streaming API
Database design
Schema
Storage Engine Choices
High-level design
Components
Request flows
Song Playback Flow
Detailed component design
Adaptive Bitrate Streaming: The Core Challenge
Trade-offs and tech choices
Push vs Pull for Recommendations
Failure scenarios and bottlenecks
CDN Edge Failure
Future improvements
Real-Time Lyrics Sync
Collaborative Playlists with Real-Time Sync
System requirements
Functional
Capacity estimation
Traffic
Storage
API design
Streaming API
Database design
Schema
Storage Engine Choices
High-level design
Components
Request flows
Song Playback Flow
Detailed component design
Adaptive Bitrate Streaming: The Core Challenge
Trade-offs and tech choices
Push vs Pull for Recommendations
Failure scenarios and bottlenecks
CDN Edge Failure
Future improvements
Real-Time Lyrics Sync
Collaborative Playlists with Real-Time Sync