Library
34 min read

Design a video streaming service

A phone on a train plays a video at 1080p, goes through a tunnel, drops to 480p for eight seconds, and comes back up. Nobody notices. That is the product.

Below, one video makes the whole trip: uploaded once across the top, then watched along the bottom.

UploaderObject storageSplitterTranscode queueTranscode workersPackagerVideo metadataPlayback APIViewerCDN edgeOrigin shield

Upload. The uploader sends the file in parts straight to object storage, using pre signed URLs. Our own servers never touch the bytes.

The interesting thing about this question is that almost none of the hard work happens while somebody is watching. Playback is a static file download from a cache near the user, and it is meant to be boring. Everything expensive happens once, when the video is uploaded, and everything clever happens on the client. Candidates who spend the interview designing the streaming path are designing the easy half.

Think of a bakery. The baking happens once, early, in one kitchen, and it can take hours. The selling happens all day, in hundreds of corner shops close to where people live. Nobody bakes a loaf when a customer walks in. A video service works the same way: transcode once, then hand out finished files from shelves near the viewer.

Words this page uses a lot:

  • Transcoding means re-encoding a video into a different size or format. One upload becomes several versions.
  • A rendition is one of those versions: the same video at one resolution and quality, like 240p, 480p, 1080p or 4K.
  • Bitrate is how many bits a second of video takes, written in Mbps (megabits per second). Higher quality means a higher bitrate and a bigger download.
  • A segment is a few seconds of video saved as its own small file. Here, six seconds.
  • A manifest is a small text file that lists the renditions and the URL of every segment. The player reads it first, then fetches segments.
  • A CDN (content delivery network) is a set of servers spread around the world that keep copies of popular files close to viewers. Each location is an edge. The origin is where the master copy lives, here object storage.

Step 1: Understand the problem#

The first question separates this into two completely different systems, so ask it first.

You askThey sayWhat it settles
On demand video, or live?On demand.Everything. On demand means transcode once and serve a static file forever. Live means transcoding while people wait, inside a tight time budget, which is a different page and you should say so.
How many hours are uploaded, and how many watched?Thousands of hours uploaded a day. Hundreds of millions of hours watched.At the default numbers in the napkin below, about fifty thousand hours are watched for every hour uploaded. Upload is a batch problem you can queue and watching is a bandwidth problem you cannot, so they get completely separate infrastructure.
What devices and what networks?Everything from a 3G phone to a 4K television.Several renditions of every video, and switching between them while the video plays. It is also why a single file per video is not an option.
How soon after upload must a video be watchable?Minutes, not hours.Transcoding is split into chunks and done in parallel, not as one long job per file. A two hour film transcoded one frame after another is a very long wait.
How exact does the view count have to be?Roughly right, quickly. Exact eventually, for payouts.A streaming counter for the number on screen and a batch recount for the number creators are paid on. The same shape as the ad click aggregator, and worth saying so.
Do we need DRM and geo restrictions?Signed URLs and region blocking, yes. Full DRM, assume not.DRM (digital rights management) is copy protection built into the player. Without it, short lived signed URLs checked at the CDN edge do the job, instead of an authorisation call per segment. An auth check on every six second chunk would be the busiest endpoint you own.

That view count answer is exactly the "two pipelines, one number" idea in the ad click aggregator, and the YouTube page builds the counter in full. This page stays on the video itself.

What you are building, and what you cut#

Functional requirements
What the system does.
  • Upload a video and make it watchable in minutes. Upload in parts that can resume, then transcode in parallel.
  • Transcode into several renditions. From 240p to 4K, in more than one compression format.
  • Stream adaptively to any device. The player picks the rendition, segment by segment.
  • Count views. Approximately now, exactly later.
Non-functional requirements
How well it has to do it. The numbers you commit to.
  • Playback starts in under 2 seconds at p95 (95 out of 100 plays start at least this fast).
  • Rebuffering under 0.5% of watch time.
  • A video is watchable within 5 minutes of upload finishing.
  • Around a hundred terabits a second of egress at peak, almost all of it from cache.
Out of scope
What you cut, and say so out loud.
  • Recommendations and the home page. It is a ranking system that happens to point at videos.
  • Live streaming, which puts transcoding on the latency path and needs its own design.
  • Comments, subscriptions and the social layer. A feed problem, covered on the YouTube page.
  • Content moderation and copyright matching. Enormous, and it runs beside the pipeline rather than inside it.

Back of the envelope#

Egress is data sent out to viewers, and cloud providers and networks charge by it. A quick way to feel the size: 500 million hours watched a day, spread over 24 hours, is about 21 million people watching at any moment. At 2.5 Mbps each, that is about 52 terabits a second, on average. The napkin does the same sum with sliders. (The method is in back of the envelope.)

Where the money goes
10 thousand
500 million
7
Source video in per day10k hours x 3.6 GB = 36 TB
Stored after transcodingsource x 4.9 = 175 TB a day
Average egress500M hours at 2.5 Mbps = 52,083 Gbps
Peak egressaverage x 2.2 = 114,583 Gbps
Transcode CPU per day10k x 7 renditions = 28,000 core hours
114.6 Tbps
at peak, which is the number that decides whether you have a CDN strategy or a hobby

Storage grows by 175TB a day and never shrinks, but it is cheap and boring. The number that dominates the bill is egress. Everything in this design that looks like an optimisation is really an attempt to serve that 114.6 Tbps from a cache close to the user rather than from your own machines.

The number people get wrong

Candidates compute storage, find a large number, and design around it. Storage is the cheapest part of this system by an order of magnitude. Egress is the bill, and the cache hit ratio at the edge (the share of requests the edge answers from its own copy) is the single metric that moves it. Out of a million segment requests, a 99% hit ratio sends 10,000 to the origin and a 95% hit ratio sends 50,000. That is five times the origin traffic, the difference between a normal origin fleet and a heroic one.