Spotify System Design
Event delivery: 8 million events a second into hourly datasets
By Ankur NayakReviewed
How Spotify moves every play, skip and click from its apps into the datasets hundreds of teams build on: events go through a receiver service into a Pub/Sub topic per event type, streaming ETL deduplicates and encrypts them into hourly buckets on Cloud Storage, and a completeness marker tells consumers when an hour is done. This page covers event delivery, the part Spotify has documented.
Open in Studio· edit a copy- Throughput
- 8M+ events/s peak (Q1 2019)
- Storage
- 350+ TB raw events/day (2019)
- Event types
- 600+
- Events per day
- 500 billion
- Fleet
- ~15 services, ~2,500 VMs
- Old system's latency
- 2–3 hours
Requirements
Functional
- Collect events (plays, skips, searches, UI actions) from every client and backend service
- Deliver each event type into its own dataset, partitioned by hour
- Deduplicate events and encrypt personal data
- Tell consumers when an hour's data is complete
Non-functional
- Never lose events once they are accepted
- Handle peaks of millions of events per second
- One broken or noisy event type must not delay the others
- Deliver important event types within a few hours
In depth
The pipeline behind every play
Every time someone plays a song on Spotify, skips a track or taps a button, the app records an event. Those events feed the datasets hundreds of teams build on. Spotify's engineering blog has documented the system that carries them, event delivery, in unusual depth: a three-part series in 2016 on moving it to Google Cloud, a 2019 retrospective, and a 2021 post on rebuilding its front end. This page covers that pipeline. Spotify hasn't published comparable detail on audio streaming or recommendation serving, so they aren't drawn here.
The scale is large. At the end of the first quarter of 2019, Spotify was producing more than 8 million events per second at peak and about 500 billion events a day, over 350 TB of raw data daily, across more than 500 event types owned by hundreds of teams.
The old system: Kafka 0.7 and HDFS
Part I of the 2016 series describes where Spotify started. Events from clients reached Spotify's gateways, which logged them via syslog. A system built on Kafka 0.7 carried them from each data centre, "Groupers" compressed and republished them, and they were written in Avro to HDFS in hourly buckets. An event counted as delivered only once written to HDFS, which made the Hadoop cluster a single point of failure. The ETL job alone added about 30 minutes of latency, and the system pushed more than 700,000 events per second.
Part II explains what went wrong. Kafka's Mirror Maker, which copied data between data centres, worked only on a best-effort basis and could drop data when the destination had problems, and the Kafka producer had stability issues. For a system whose job is not to lose events, that was a deal-breaker.
Choosing Cloud Pub/Sub
Spotify evaluated Google Cloud Pub/Sub as the transport. It offered at-least-once delivery, application-level acknowledgements, retention of undelivered data for 7 days, and no brokers for Spotify to operate. The team tested it hard: publishing 2 million events per second, well above the then-production peak of about 700,000, from 29 Event Service machines, with almost no server errors. A five-day consumer test averaging 800,000 messages per second showed a median end-to-end latency of about 20 seconds; after a deliberate backlog, consumers caught up in about 8 hours, with no lost messages.
At-least-once delivery means duplicates are possible, so deduplication became part of the pipeline.
One topic, one pipeline, per event type
The central design decision, described in Part III and reaffirmed in 2019, is isolation: every event type has its own Pub/Sub topic, its own ETL process and its own storage location. With roughly a thousand event types in 2016, that's a lot of pipelines, but it means a noisy, broken or blocked event type can't halt the rest of the system. See message queues.
The ETL is a streaming job on Google Cloud Dataflow, chosen because it offers one model for streaming and batch. Events are written into hourly buckets by event timestamp on Cloud Storage. Jobs vary enormously in size: in 2016 the smallest consumed about 30 events per second with two workers, the largest about 100,000 per second with 42.
When is an hour complete?
Consumers need to know when an hour's data is final. The ETL tracks a watermark, its estimate of how far event time has progressed, and closes an hourly window when the watermark passes the hour boundary. A completeness marker is then written, exactly once. Watermark lag was mostly under 200 seconds in 2016.
Mobile phones go offline and send events late. Rather than reopen a completed hour, Spotify writes late events into the currently open bucket by shifting their timestamp forward; events more than a day late are dropped. The 2019 post sums up this choice as prioritising liveness over lateness: consumers get each hour on time, at the cost of a few events landing in a later hour.
The result was a big improvement: the old system's end-to-end latency was two hours on a perfect day and three on an average one, and the new system's worst case was four times lower.
Life in the cloud
By 2019 the system ran as about 15 microservices on roughly 2,500 VMs. Cloud Storage held final and intermediate datasets, Cloud SQL stored metadata, Dataproc handled deduplication, Dataflow jobs encrypted sensitive data, and BigQuery served as the warehouse.
Event types are assigned priorities with delivery objectives: high (a few hours), normal (24 hours, for data needed the next day) and low (72 hours, mostly internal). And privacy is built into schemas: fields are annotated with semantic types that mark personal data, and the ETL encrypts it automatically and applies access tiers and retention policies.
Rebuilding the front door
The 2021 post describes the next step. Hundreds of services still sent events by logging to disk, mobile events were often fire-and-forget, and events travelled as TSV strings. Spotify moved its 600+ event types to new SDKs and a highly available receiver service; during a receiver outage, clients store events temporarily. Deduplication moved to Dataflow, using event message identifiers to look back across weeks rather than hours.
The trade-offs
A pipeline per event type maximises isolation but multiplies the number of jobs to deploy, monitor and scale; deployments of the whole fleet once took up to three hours. Liveness over lateness gives consumers predictable hours at the cost of misfiled stragglers. Managed Pub/Sub removed broker operations but made deduplication mandatory. And schema annotations make privacy consistent, as long as every schema is annotated correctly.
Compare with YouTube, another Google Cloud-scale pipeline, and the notification service design.
What to take into an interview
- Isolate streams by type as early as possible
- Partition by event time into hourly buckets and publish a completeness marker
- Decide your late-data policy explicitly: liveness over lateness
- With at-least-once delivery, design deduplication in from the start
- Keep enough retention in the transport to survive downstream outages
- Put privacy rules in the schema so the pipeline can enforce them
Components
Spotify Apps & Services
SDKsClient. Mobile, desktop and web clients and backend services emit events through Spotify's SDKs. During a receiver outage, clients store events temporarily.
Receiver Service
API gateway. A highly available API the SDKs send events to. It recognises each event's type and publishes it to that type's topic.
Pub/Sub Topics
Google Cloud Pub/SubPub/Sub. One topic per event type. Chosen in 2016 for at-least-once delivery, application-level acknowledgements and retention of undelivered data for 7 days.
Why: A topic per event type isolates streams as early as possible, so one bad type can't block the rest.
Building block: Message Queue & Event Log, Fan-out on Write vs. Read
ETL per Type
DataflowBatch processing. A streaming job for each event type reads its topic and writes events into hourly buckets by event time. Jobs range from two workers to dozens.
Hourly Windows
Service. Events are grouped into hourly windows by their event timestamps.
Watermark
Coordination. Tracks how far event time has progressed; a window closes when the watermark passes the hour boundary. Lag was mostly under 200 s in 2016.
Late Event Handling
Service. Late events can't be appended to a completed hour, so they are written to the currently open bucket; events more than a day late are dropped.
Completeness Marker
Object storage. Written once per hourly bucket when it is complete; consumers wait for it.
Deduplication
Batch processing. Removes duplicate events, which at-least-once delivery produces. The 2021 rebuild moved it to Dataflow using event message identifiers, looking back across weeks instead of hours.
Hourly Datasets
Cloud StorageObject storage. The main storage for final and intermediate datasets: one location per event type, partitioned into hourly buckets.
Building block: Object Storage
BigQuery
Data warehouse. The data warehouse where datasets are queried; exports are made by batch jobs from the hourly buckets.
Pipeline Metadata
Cloud SQLSQL database. Metadata about event types and pipelines.
Data Consumers
Service. Hundreds of teams own event types and build on the datasets. Schema annotations mark personal data, which is encrypted and gets its own access tiers and retention.
Request flows
A song play reaches the warehouse
- A listener finishes a song; the app's SDK sends a play event.
- The receiver recognises the event type and publishes it to that type's Pub/Sub topic.
- The receiver acknowledges; until then the client keeps the event, so nothing is lost if the receiver is down.
- The play event's own streaming ETL job reads the topic.
- The event lands in the hourly bucket for its event time, with personal fields encrypted.
- Duplicates from at-least-once delivery are removed.
- The completed hour is exported to BigQuery for analysis.
A broken event type is isolated
- A client bug suddenly floods one event type with malformed events.
- They go to that type's topic only.
- That type's ETL job falls behind; every other type's job keeps running.
- Consumers of other event types get their hours on time.
Failure scenarios
What if: the receiver service is unavailable?
Clients store events temporarily and send them when the receiver is back. Because the receiver is a highly available API in front of Pub/Sub, this should be rare.
User impact: None for listeners; data arrives later.
- Client-side buffering
- Highly available receiver
What if: downstream processing stops for hours?
Pub/Sub retains undelivered messages for 7 days, so events wait in their topics until the ETL catches up. In a 2016 test, a backlog took about 8 hours to catch up with no lost messages.
User impact: Late datasets; no lost events.
- 7-day retention in Pub/Sub
- Liveness over lateness
- Priority tiers with SLOs
Trade-offs
A topic and pipeline per event type
Isolate each event type end to end: its own topic, ETL job and storage location.
- Pros
- A noisy or broken type can't block others
- Each type scales and is monitored separately
- Cons
- Hundreds of pipelines to deploy; fleet deployments once took up to three hours
Liveness over lateness
Close hourly buckets on time and put late events in a later bucket.
- Pros
- Consumers get each hour promptly
- A completeness marker is final
- Cons
- Late events end up in the 'wrong' hour; events over a day late are dropped
Managed Pub/Sub instead of self-run Kafka
Move from Kafka 0.7 to Google Cloud Pub/Sub in 2016.
- Pros
- At-least-once delivery with 7-day retention
- Someone else runs the brokers
- Cons
- Dependence on a cloud provider
- At-least-once means deduplication is required
Schema annotations for privacy
Mark personal data in schemas and let the pipeline encrypt it automatically.
- Pros
- Consistent encryption, access tiers and retention
- Producers don't each implement privacy
- Cons
- Every schema must be annotated correctly
Frequently asked questions
How does Spotify process events?
Clients send events through SDKs to a receiver service, which publishes each to a Google Cloud Pub/Sub topic for its event type. A streaming ETL job per type writes events into hourly buckets on Cloud Storage, deduplicates them and encrypts personal data; completed hours are exported to BigQuery.
Why did Spotify move from Kafka to Cloud Pub/Sub?
Its 2016 system on Kafka 0.7 had reliability problems: Mirror Maker mirrored data only on a best-effort basis and could drop it, and the producer was unstable. Cloud Pub/Sub offered at-least-once delivery, application-level acknowledgements, 7 days of retention and no brokers to operate.
What does 'liveness over lateness' mean?
Spotify closes each hourly bucket on time rather than waiting for every straggler. Events that arrive after their hour has been marked complete are written into the currently open bucket instead, and events more than a day late are dropped.
How many events does Spotify process?
At the end of the first quarter of 2019, more than 8 million events per second at peak and about 500 billion a day, adding up to over 350 TB of raw event data daily across more than 500 event types.
Building blocks used
Sources
- Spotify's Event Delivery – Life in the Cloud · Spotify Engineering
- Spotify's Event Delivery – The Road to the Cloud (Part I) · Spotify Engineering
- Spotify's Event Delivery – The Road to the Cloud (Part II) · Spotify Engineering
- Spotify's Event Delivery – The Road to the Cloud (Part III) · Spotify Engineering
- Changing the Wheels on a Moving Bus — Spotify's Event Delivery Migration · Spotify Engineering