# Design Spotify Mobile: Offline Music, Downloads, and Streaming

Source: https://www.techinterview.org/post/3233474988/design-spotify-mobile-offline-streaming/
Updated: 2026-07-26 · techinterview.org

"Design Spotify mobile" is a layered question. The basic streaming case is straightforward — fetch a manifest, download audio, play. The hard parts are offline downloads (with DRM), gapless playback across tracks, Spotify Connect handoff to other devices, and keeping a 100K-track library searchable on a phone.

## Functional requirements

- Stream music on Wi-Fi and cellular. Interviewers probe what happens when the network drops or switches mid-track — the player should keep playing from already-buffered chunks and re-resolve the manifest on the new connection rather than stalling. Be ready to talk about the buffering target (a few seconds ahead) and how you handle a total loss of connectivity.

- Download playlists and albums for offline listening. The probes here are storage limits, encryption at rest, and what happens when the subscription lapses. Say how you cap disk usage, evict least-recently-played downloads when space runs low, and let the user see and manage what's taking up space.

- Search across 100M+ tracks instantly. The trick is splitting the work: the user's own library is searched locally while the global catalog is searched server-side. Expect questions on your latency budget, how you merge the two result sets, and how you rank them so exact matches surface first.

- Sync playback state across devices (Spotify Connect). The hard part interviewers push on is where the source of truth lives and how you resolve conflicts when two devices try to control the session at once. Server-held state with a single authoritative position is the usual answer.

- Show recommendations, recently played, and queue. Recommendations are precomputed server-side and cached on the device so the home screen renders offline; recently played and the current queue are stored locally so they survive an app restart. Interviewers like to ask how stale cached recommendations are allowed to get.

## Architecture

Three core modules: **player**, **library**, **sync**.

## Streaming

Each track has a manifest pointing to chunks at multiple bitrates (96k, 160k, 320k Ogg Vorbis or AAC). Player fetches first ~10 seconds, starts playback, prefetches the rest while playing.

For the next track in the queue, prefetch starts ~30 seconds before the current track ends. This enables gapless playback.

## Offline downloads

The user toggles "Available offline" on a playlist. The download manager queues the tracks with priority, fetches them on Wi-Fi (or cellular if user opts in), and stores encrypted blobs locally.

DRM: tracks are downloaded encrypted with a per-device key. Subscription validation happens at playback time — the local player calls a license server to decrypt. If the user goes offline for too long (typically 30 days) without a re-validation, downloads expire.

## Library

The user's library (saved tracks, playlists, albums) is fully synced to local [SQLite](/post/3233474463/sql-interview-questions-2025-window-functions-cte-joins-subqueries-indexing-query-optimization-transactions-normalization/). This enables instant search across the user's collection. Global track search is server-side.

## Spotify Connect

Phone discovers other Spotify-enabled devices via mDNS or via the Spotify backend. State (current track, position, queue) is held server-side; any device can take control. The phone becomes a remote — it tells the server "play track X on speaker Y."

## Battery and data

- Adaptive bitrate based on network — start at 160k, upgrade to 320k if Wi-Fi

- Pause prefetch when battery is below 15%

- Background audio uses iOS/Android audio sessions; minimal CPU overhead

## Frequently Asked Questions

### How does gapless playback work?

The player crossfades or seamlessly hands off between two prefetched tracks. The decoder for the next track is warmed up before the current one finishes, and audio buffers are stitched without a gap.

### What format does Spotify use?

Ogg Vorbis historically; AAC and Opus increasingly. Premium tier offers higher-quality streams. Format selection is per-device based on hardware decoder support.

### How is search so fast?

Global search uses an [inverted index](/post/3233461821/database-indexing-interview-guide/) served from a search cluster (Elasticsearch-style). Personal library search is local SQLite FTS. Combined results are merged client-side.
