CodingNodes Space play · ←→ step · R restart

The problem

YouTube hides one of the internet's hardest distribution problems behind a play button. Creators hit upload, viewers hit play — and in between, half a million new videos a day must be re-encoded into playback formats, moved onto storage that costs real money, and streamed to five million people without a stutter.

The trick to designing it is noticing the product is really two systems wearing one logo. Video bytes are few in kind but colossal in volume, so they ride content delivery networks. Everything else — search, likes, comments, channel pages — is small but chatty, so it rides ordinary API servers. We will build both pipelines: the upload path with its transcode farm, and the streaming path with its edge caches, then size the CDN bill and make the design survive the long tail.

Every number this chapter uses is stated up front in the assumptions grid, and every animation afterwards reuses those exact same numbers.

Requirements

Functional — what it must do

  • Creators upload video up to 1 GB from a phone, camera or browser, and the video is playable within minutes of the upload finishing.
  • Viewers stream on mobile apps, browsers and living-room TVs, and can raise or lower quality in the middle of playback.
  • Playback starts in about two seconds, anywhere in the world, without downloading the whole file first.
  • Titles, channels, likes and playlists are served by API servers — never on the same path as the video bytes.

Non-functional — how well it must do it

  • Smoothness beats sharpness: the player steps down the quality ladder rather than ever stalling the picture.
  • No single metadata node, queue or worker machine may take playback down — every tier either replicates or fails over.
  • Original uploads are kept even after renditions ship, so tomorrow's codec can re-encode yesterday's catalog.
  • Spending scales with watched bytes, not with catalog size — the videos nobody watches must not drive the bill.
  • Uploads authorize through signed URLs and streams serve over encrypted connections; storage never accepts anonymous writes.

Scale — how much it must take

  • 5 million daily viewers playing 25 million videos a day; 500,000 new uploads add 150 TB of raw video daily, served worldwide from CDN edges.

Assumptions — the numbers this design is sized for

A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.

Daily active viewers
5,000,000
Each watches about 5 videos a day, which is 25 million plays to serve every day.
Daily uploads
500,000
10% of viewers upload one video a day; the average file is 300 MB, capped at 1 GB.
New raw storage
150 TB / day
500,000 uploads × 300 MB of original video arriving daily, kept even after encoding.
Streaming egress
7.5 PB / day
25M plays × 0.3 GB out of the CDN — the number that decides the whole cost story.
CDN price
$0.02 / GB
Typical cloud egress rate. That is $150,000 a day just to stream — the line every optimization targets.
Transcode ladder
5 renditions
360p, 480p, 720p, 1080p and 4K, plus a thumbnail — encoded in parallel on a pool of 64 workers.
Upload chunk
5 MB
A 300 MB file ships as 60 GOP-aligned chunks, so a dropped chunk resumes alone.
Playback targets
< 2 s start · 96% edge hits
First frame in under two seconds; 96 of 100 plays are served without touching origin.

Takeaways

Part 1

One logo, two systems

Every click is either a byte-carrying video stream or a tiny API call — and the two travel completely different roads.

  1. 1
    Two roads out of the client

    Pressing play pulls video bytes from a CDN edge; searching, liking and subscribing go to the API servers. One logo, two systems: a firehose of huge objects on one road, a chatter of tiny requests on the other.

  2. 2
    Rent the commodities

    Blob storage and CDN reach are commodities: Netflix streams from compute it rents and Facebook rents Akamai's edge rather than building one. You build only the two things that are genuinely yours — the transcode farm and the metadata tier.

  3. 3
    The ratio that rules the design

    Twenty-five million plays at 0.3 GB push 7.5 petabytes a day, while every API call a viewer makes sums to a few hundred gigabytes. The bytes outweigh the chatter by nearly twenty thousand to one — which is why they get their own network.

Part 2

The upload path

A upload is a signed ticket, sixty chunks to blob storage, and a job on a queue — the API servers carry almost none of it.

  1. 1
    Ask for a signed door

    Uploading starts with a request that carries no video at all: the creator asks the API tier for a pre-signed URL — a short-lived write ticket for exactly one object in blob storage. The video itself never flows through the API servers.

  2. 2
    Chunks, not one blob

    The client slices the 300 MB file into sixty 5 MB, GOP-aligned chunks and PUTs them straight into blob storage through the signed door. If the network blinks, one chunk retries alone — a resume costs seconds, not the whole upload.

  3. 3
    Metadata rides in parallel

    While the chunks fly, a second small request records the video's title, size and format. The API tier writes it through the metadata cache into the sharded database. Files and facts travel separately and meet only when the video goes live.

  4. 4
    The farm picks up the job

    Upload complete is not 'done'. The finished object hands a transcode job to the farm's queue, and an idle worker pulls the raw file's task list. From this moment the creator sees 'processing' — a working system, never a dead spinner.

  5. 5
    Renditions ship to the edge

    Encoding ends with five renditions and a thumbnail landing in encoded storage, then a push out to the CDN POPs. A completion event flips the metadata row to 'ready', and the creator's dashboard turns green.

Part 3

Transcode is a graph

One raw file in, a ladder of encodings out — modeled as a directed acyclic graph so the branches run in parallel and per-creator options are just configuration.

  1. 1
    Why not serve the original?

    A camera's original file is enormous, format-locked and tuned for nobody's network. Transcoding turns one raw upload into a ladder of encodings, so every device and every bandwidth gets a version it can actually play.

  2. 2
    One file becomes three streams

    The preprocessor splits the file at GOP boundaries into independently playable pieces, then peels it into video, audio and metadata. Each stream becomes its own branch of jobs in the graph.

  3. 3
    The quality ladder

    The video branch fans into five encodes, one per rung: a cheap low-res for weak signals up to 4K for fiber and big TVs. Plus one thumbnail, generated automatically unless the creator brings their own art.

  4. 4
    Branches run in parallel

    None of these jobs waits on its siblings: five ladder encodes, the audio pass and the thumbnail run on different workers at the same time. That parallelism is why a 300 MB upload finishes its whole graph in about twelve minutes.

  5. 5
    The graph is configuration

    The DAG is generated from per-creator config, never hard-coded: partners add a watermark node, studios that deliver their own art skip thumbnail generation. New pipeline shapes are data changes, not deployments.

Part 4

Inside the transcode farm

Every GOP of every upload becomes a task in a priority queue, drained by a pool of replaceable encoder workers.

  1. 1
    Every GOP is a task

    Each GOP of each upload becomes a task — inspect it, encode a rung, cut a thumbnail. Tasks land in the farm's priority queue and wait for a free worker, so a burst of uploads just makes the line longer, never a viewer's stream worse.

  2. 2
    Workers drain the line

    A pool of sixty-four encoder workers pulls tasks as fast as they finish them. Queue depth is the farm's health gauge: short means keeping up, growing means add workers — which on rented machines is a config change, not a project.

  3. 3
    Priority beats fairness

    The queue is priority-ordered, not first-in-first-out: a partner's 4K upload with a premiere deadline overtakes a weekend clip. A scheduler pairs the most urgent task with the freest worker, and keeps a running ledger of who holds what.

  4. 4
    A dead worker is a requeue

    Task workers are cattle, not pets. When one dies mid-encode, its task quietly returns to the queue and another worker picks it up. Because work is cut at GOP size, the retry costs one five-megabyte chunk — never the whole video.

Part 5

The streaming path

Playback is a chain of two-second chunks from the nearest edge POP — and almost nothing ever touches your origin.

  1. 1
    Stream, don't download

    A player never downloads the file. It fetches a manifest, then pulls two-to-six-second chunks continuously, keeping a small buffer ahead of the playhead — first frame arrives in well under two seconds.

  2. 2
    96% of plays never leave the edge

    Popular videos are already cached at the viewer's regional POP, so ninety-six of a hundred plays are served entirely from the edge. This is what the CDN money buys — and why it is worth paying.

  3. 3
    The other 4% walk to origin

    A miss travels once to a shield next to the encoded storage, and the answer is cached at the POP on the way back — the next viewer in that city is a hit. Cold content still streams smoothly; it just costs more per play.

  4. 4
    The player climbs down before it stalls

    Every few chunks the player measures real throughput and switches rungs: Wi-Fi to subway means 1080p drops to 360p mid-scene with no stall and no reload. The ladder built in the transcode step is what makes that switch invisible.

Part 6

The long-tail bill

Streaming dwarfs every other cost in the system — and popularity, not catalog size, decides where the money goes.

  1. 1
    What streaming costs

    Charge twenty-five million plays at 0.3 GB against two cents a gigabyte and streaming alone costs a hundred and fifty thousand dollars every day — before storage, compute or salaries. No other line item in this design is in the same weight class.

  2. 2
    One video's first week

    Views explode on day zero — homepage, subscribers, shares — then fall off a cliff. Measure any large catalog and the shape repeats: a few videos soak up nearly all the plays while most are barely watched at all. That asymmetry is the long tail.

  3. 3
    Demote the tail

    Once daily plays drop under the hot line, the renditions come off the POPs and sleep in cheap blob storage. A cold video that nobody watches costs nothing; the moment it trends again, one origin fetch re-primes the edge.

  4. 4
    Route by popularity

    A popularity table sits in front of serving: the head of the catalog goes to CDN edges, the tail to ordinary video servers pulling from blob storage. And a video that is a hit in one region stays in that region — there is no reason to pay to replicate a local hit worldwide.

Part 7

Locking the front door

Storage must never accept writes from strangers, and the content itself needs protection on the way back out.

  1. 1
    The wide-open bucket problem

    If blob storage accepted uploads from anyone, one leaked URL would let a stranger fill your 150-terabyte-a-day budget or overwrite your catalog. Storage is a warehouse, not a reception desk — it must never be the public's front door.

  2. 2
    Mint a ticket first

    The API tier mints a pre-signed URL scoped to exactly one object with a fifteen-minute lifetime. Microsoft's blob storage calls the same idea a shared access signature — a different name for the same short-lived ticket.

  3. 3
    Upload through the ticket

    The client PUTs each chunk with the signature attached, and storage verifies it before the first byte lands. Expired ticket, edited URL, wrong object — the write is refused with a 403 without ever involving your servers.

  4. 4
    Protect the playback too

    Rented studio content ships under the three big digital-rights systems; user uploads get AES encryption and an invisible watermark, so a leaked screencap can be traced back to the account that leaked it.

Part 8

When parts break

At half a million uploads a day, failures are constant background noise — the design just has to make them cheap.

  1. 1
    Two kinds of broken

    A dropped chunk or a crashed encode deserves retries; a file that cannot be parsed never will. The DAG knows which kind of failure it is holding, so it either requeues the task or halts the video and returns a real error to its creator.

  2. 2
    Retry at GOP size

    When an encode fails, only the failed GOP returns to the queue while the rest of the video's tasks keep running elsewhere. Three strikes and the task is declared non-recoverable and surfaced to the creator with a real message.

  3. 3
    The metadata tier fails over

    Losing the metadata master is an event, not an outage: a replica is promoted within a minute and the stateless API servers simply reconnect. Cache nodes are replicated too, so losing one is a miss-rate blip rather than a blackout.

★ Finale

End to end

One video, the whole machine: a creator on the left, the farm across the top, a viewer at the bottom — and every number from the assumptions grid holding.

  1. 1
    The whole machine

    Everything on one map. A creator sits at the top left; the transcode farm runs along the top; viewers wait at the bottom right. The metadata tier hangs under the API servers, and blob storage sits between both worlds. Now run one 300 MB video through it.

  2. 2
    Upload

    The creator gets a signed URL, then sixty five-megabyte chunks fly straight into blob storage while the title row lands in the metadata database. On a decent connection the file is up in under two minutes.

  3. 3
    The DAG runs

    The farm cuts the file into GOPs and works the ladder: five renditions and a thumbnail land in encoded storage about twelve minutes later, and a completion event flips the metadata row to 'ready'.

  4. 4
    Onto the edges

    Encoded renditions push out to POPs near where viewers live. The head of the catalog rides every edge on earth; the cold tail waits at origin and travels only when someone actually presses play.

  5. 5
    A viewer presses play

    The player fetches a manifest, then streams chunks from the nearest edge: first frame inside two seconds, and for ninety-six percent of plays the origin never hears about it at all.

  6. 6
    When the edge misses

    The four percent of plays that miss walk up to encoded storage once, get cached at the POP, and the next local viewer is a hit. Upload, transcode, publish, serve — the whole design in six moves, with every number still holding.

🌐 In the wild

In the wild

None of this is interview fiction: blob storage, DAG-shaped transcode farms and rented edges are how the big streaming platforms actually run.

  1. 1
    This pipeline runs the internet

    Every piece of the map you just built has a production twin with a company name on it. Let's put the names on — blob storage, the DAG farm, the rented edge and the owned edge.

  2. 2
    The blob and the ticket

    The signed-door pattern is a product, not a pattern: S3 calls it a pre-signed upload URL and Azure blob storage calls it a shared access signature. Our 'original storage' box is that product, taking 150 terabytes a day.

  3. 3
    The farm at Facebook scale

    Facebook's streaming video engine defines encoding as a directed acyclic graph of stages that run sequentially or in parallel — the same shape as our scheduler. Weibo has published its own take on a video-processing pipeline for feed-scale traffic.

  4. 4
    Edges you rent

    Netflix streams from cloud infrastructure it rents, Facebook rents Akamai's edge rather than building one, and CloudFront publishes the per-gigabyte price our whole bill was sized from. Renting the last mile is the industry default.

  5. 5
    Owning the last mile

    At Netflix scale, renting stops being enough: Open Connect puts caching servers physically inside internet providers and fills them with the content its popularity models predict will be watched. The long-tail logic, taken all the way to your ISP's rack.