Design Spotify / Music Streaming Service
High-scale music streaming architecture delivering sub-200ms audio playback start, adaptive audio bitrate chunking (Ogg Vorbis / AAC), offline caching, and personalized music discovery.
1. System Requirements
- Search, stream, and play millions of licensed audio tracks with instant playback start.
- Support adaptive audio bitrate switching (96kbps, 160kbps, 320kbps) based on network conditions.
- Provide playlist management, user library persistence, and offline track download capability.
- Sub-200ms audio playback start time (Time to First Audio Frame) to eliminate playback lag.
- High streaming availability (99.99%) with zero mid-song audio buffering.
- Scalable global audio distribution optimizing CDN bandwidth and licensing egress costs.
2. Scale & Resource Estimates
Throughput: 600 Million Monthly Active Users. 50 Million concurrent listeners during peak hours.
Storage & RAM: 100 Million tracks * 5 MB average per track = 500 Terabytes raw audio. Encoded into 3 bitrates = 1.5 Petabytes total storage.
3. System API Endpoints
GET /api/v1/tracks/{track_id}/stream?bitrate=320
Response (200 OK):
{
"stream_url": "https://cdn.spotify.com/audio/chunks/trk-9901-320k.m3u8",
"track_id": "trk-9901",
"duration_ms": 214000,
"codec": "vorbis"
}
POST /api/v1/users/me/playback/play
Request: { track_id: "trk-9901", position_ms: 0, device_id: "dev-macbook-pro" }
Response: { status: "playing" }
4. Database Schema Design
Table: tracks (PostgreSQL Metadata Store)
- track_id (UUID - Primary Key)
- title (VARCHAR(255))
- artist_id (UUID - Foreign Key)
- album_id (UUID)
- duration_ms (INTEGER)
- isrc_code (VARCHAR(12) - Unique)
Table: playback_history (Cassandra - Time-Series Partitioned by user_id)
- user_id (UUID - Partition Key)
- played_at (TimeUUID - Clustering Key DESC)
- track_id (UUID)
- completion_percentage (NUMERIC(5,2))
Table: user_playlists (PostgreSQL)
- playlist_id (UUID - Primary Key)
- user_id (UUID)
- track_ids (UUID[] - Ordered Array)
- is_collaborative (BOOLEAN)
5. Interactive High-Level Architecture
6. Scalability Deep Dive & Bottlenecks
Spotify avoids sending monolithic 5 MB audio files over the wire. During ingestion, raw audio masters are transcoded into multiple bitrates (96 kbps HE-AAC, 160 kbps Ogg Vorbis, 320 kbps Vorbis) and diced into 10-second encrypted chunks. When a user presses play, the client issues an HTTP Range request for the first 500 KB to buffer the first few seconds instantly, while asynchronously pre-fetching remaining chunks in the background. Content-addressed chunk hashes are cached across hundreds of global CDN Edge Points of Presence (PoPs).
7. Production Scaling Strategies
Initial Chunk Aggressive Prefetch: When a user starts or skips a track, the client requests chunk 0 with high priority. Once playback begins, background workers prefetch chunk 0 of the *next* queued song to guarantee zero-latency gapless playback.
CDN Byte-Range Optimization: Audio chunks are immutable and content-addressed via SHA-256. They carry `Cache-Control: public, max-age=31536000, immutable`, offloading 98% of streaming traffic to edge CDNs.
Cassandra Playback Ring: User listening histories and royalty play counts write to an append-only Cassandra cluster partitioned by `user_id`. Cassandra's LSM-tree architecture absorbs 150,000+ playback metric writes/sec without locking.
Vector Search for Discover Weekly: Songs and listener listening sessions are encoded into 256-dimensional vector embeddings using two-tower collaborative filtering models. Vector databases (Milvus/Qdrant) query similar embeddings for personalized discovery.
Offline Sync via Widevine / FairPlay DRM: Downloaded tracks are encrypted on disk with device-bound AES-128 keys. Periodic heartbeat license renewals check subscription status without re-downloading music assets.
Spotify Connect WebSocket Sync: Real-time playback synchronization across phones, laptops, and smart speakers is maintained via persistent WebSocket connections to an event bus running Erlang/Elixir.
8. Runnable Production Starter Code
Scaffold