AlgoViz

Design YouTube

hard 45 min

Anyone may upload a video, and anyone may watch it on any screen.

Video lesson · Chapter 1 of 6

Protect the original upload

0:00 / 3:54

One film, many sizes, in 90 seconds

1:30Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

A video sharing service takes a file from any person, turns it into versions that every phone, computer and television can play, and shows it to anyone who asks.

Two very different systems share one database of titles. The upload side is a factory: slow, heavy and allowed to wait. The watch side is a shop window: quick, enormous and never allowed to wait for the factory.

Why it exists

A file from a camera is a poor thing to send. It may be too large for a phone network, in a format that a television cannot read, and impossible to start in the middle. So the service makes copies in several qualities, each cut into pieces of a few seconds.

Bytes decide everything. Our assumed 500 million hours of watching a day is about 83 Tb/s on average, and no single building sends that.

A short history

  1. 2010

    Vitess begins inside YouTube to spread MySQL over many machines, by its own project history. It is open source today.

  2. August 2017

    RFC 8216 publishes HTTP Live Streaming: short media files and text playlists that any HTTP cache can keep.

  3. April 2021

    A YouTube engineering article and an ASPLOS paper describe the VCU, a chip made for transcoding in data centers.

  4. June 2022

    RFC 9110 becomes the HTTP standard and includes range requests, which RFC 7233 described before.

Words to know

Codec
The method that compresses and opens the picture, such as H.264, VP9 or AV1.
Transcoding
Decoding a video and encoding it again in another size, bitrate or codec.
Rendition
One finished quality of a video, for example 720p at 2.5 Mb/s.
Ladder
The list of renditions from lowest to highest.
GOP
Group of pictures. It starts with a complete picture. A closed one can be decoded alone.
Segment
A few seconds of one rendition as a file.
Manifest
The text that lists renditions and segments (.m3u8 in HLS, .mpd in DASH).
ABR
Adaptive bitrate: the player picks a rendition for each segment.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

Understand it

Picture it

The flip-book workshop

One book in, three sizes out
COPIESOne thick book comes in

Copy once in the workshop, then read from the nearest shelf, one part at a time.

Scope

What are we building?

A service like YouTube: upload, processing, watching, search, likes, comments and subscriptions. This is an exercise with our own assumptions, not a description of the company's system.

  • Take it in

    Huge files over poor networks, without starting again.

  • Make copies

    600,000 busy cores at our scale, fed by a queue.

  • Send it

    250 Tb/s at peak, nearly all from caches.

  • Count it

    Two million events a second, and one video may be very hot.

Similar services

ServiceWhat differs
Netflix, Disney+, Amazon Prime VideoA small, licensed library known in advance
TwitchLive first, with chat
TikTokShort clips, the feed chooses
VimeoHosting for professionals
Dailymotion, Rumble, BilibiliOther public video sites
Facebook videoVideo inside a social network

The finished system

Where the design ends up

The whole system

Creators ask the upload service for a session and send parts directly to object storage. A job queue feeds encoders, which write segments back to storage. Viewers ask the video service, which reads Redis and MySQL shards, and fetch segments from the CDN, which fills itself from storage. Player events go to Kafka and counters.
Orange is the upload side, blue is the watch side, dashed is asynchronous.

Read next

Who owns which idea

IdeaIts own page
Resumable and direct uploadsFile upload APIs
Objects, parts, storage classesObject storage
Edges, shields, keysCDN, CDN design
Building a CDNContent delivery network
Hot countersSharded counters
Candidates and rankingRecommendation system
Suggestions while typingSearch autocomplete
Jobs, retries, duplicatesQueues, Idempotency
Telling subscribersNotification service

For the interview: Approach, then Scope, Numbers, API and data, Design and Deep dives. For revision, go to the Mock.

Summary

The whole topic in one minute.

The file goes straight from the creator to object storage in parts that can be resumed. A job queue hands it to workers, which cut it at GOP borders, encode the pieces in parallel and write segments and a manifest. The video is published when a low rung is ready, and better rungs join later.

Viewers get the title from a small sharded database with a cache, and the bytes from a CDN. The player changes quality between segments. Views are events in a log and become numbers a little later.

  • Files never pass through our API servers.
  • Split at GOP borders, encode in parallel, retry one piece and not the film.
  • Publish with the low rungs, add the rest later.
  • All renditions are cut at the same times, so the player can switch between them.
  • Bytes come from caches; the database only holds small rows.
  • Views are events first and numbers later.

Sources