Books Deep-Read · System Design Interview Vol 1 · Ch 14

Design YouTube

System Design Interview Vol 1 · Ch 14 · Alex Xu · 2020

中文 →

What is this chapter about?

You tap a video, it starts within a second or two, and it keeps going when you walk from wifi onto mobile data. This chapter asks: what do you actually have to build to do that for millions of people? The answer is a little surprising — the hard part is not the playing. It is getting the film ready and getting it close to you.

A strange fact first

The file you carefully uploaded is never watched by anyone. Once the platform receives it, it makes a dozen other versions from it: a small one for phones, a big one for TVs, a tiny one for when your signal is bad, plus a cover image and an audio track. Viewers always watch one of those copies. The original just sits in a warehouse as the master.

Why the old world was hard

Because viewers' devices and networks are wildly far apart. The same film — what a TV on home fibre can swallow is more than ten times what a phone on the subway can. Ship only one version and you lose either way: ship the big one and half your audience stalls; ship the small one and the other half calls it blurry. So every portion size has to be prepared in advance, and the player picks. But preparing them is brutally expensive: one hour of HD, encoded start to finish on one machine, can take hours.

The mechanism: three intuitions

① A central kitchen that plates several portion sizes. The film comes in, gets prepped, then is cooked into a whole set of differently sized outputs. That step is called transcoding.

② Cut it into pieces so a hundred cooks work at once. Video has natural cut points — every few seconds there is a "complete picture" you can start playing from. So the hour-long film is cut into hundreds of few-second pieces, hundreds of machines cook them at the same time, and the results are stitched back in order. Total time no longer depends on how long the film is; it depends on the slowest single piece.

③ Ship the popular ones to the corner shop near you. The finished versions live in a central warehouse, but you may be half a world away from it. So the platform pushes only the hottest ones out to "corner shops" a few dozen kilometres from you — that's a CDN. You are served from the corner shop, which is why it's fast.

How do you choose?

Every trade-off here is about money. Push every version of every film to every corner shop on earth and the bill explodes — the single biggest cost of a platform like this is the bandwidth of sending video out. So the real approach is tiering: popular titles live in the corner shop, obscure ones are fetched from the central warehouse, and obscure ones may not even get the full set of portion sizes made ahead of time — they get cooked when somebody finally asks.

The honest price: the video you just uploaded takes minutes, sometimes longer, before a high-definition version exists — it is queued on that pipeline.

One line to remember

The two genuinely hard things about a video platform are turning one source file into a whole set of differently sized outputs (by cutting it into pieces and working in parallel) and getting those outputs near the viewer (by shipping only the popular ones). Playback itself is the most settled part of the business.

Want the mechanisms, the numbers and the diagrams? → Switch to the deep read