Skip to content
Tutorials Center
My learning

Topics

Lesson 55 of 65 ยท Case Studies: Classic Designs

Design a Video Streaming Service

Design a video streaming platform: upload pipeline, transcoding into renditions, segments and manifests, adaptive bitrate, CDN caching and cost estimates, with Python.

Streaming services such as YouTube and Netflix deliver video to hundreds of millions of people on every kind of device and connection. The two hard parts are opposites: ingest (turning an uploaded file into many playable formats) and delivery (moving enormous bandwidth cheaply and smoothly). Videos are huge, so the design is dominated by storage, compute for transcoding and, above all, network cost.

Requirements

  • Upload videos (up to several GB); make them playable within minutes.
  • Smooth playback on phones, TVs and browsers over fast and slow networks.
  • Search, recommendations and comments exist, but this lesson focuses on upload and playback.
  • Scale: 1 billion hours watched per day, 500 hours of video uploaded per minute.

Back of the envelope:

  • Uploads: 500 hours per minute is 30,000 hours per hour, or 720,000 hours per day. At about 1 GB per hour of source video that is roughly 720 TB per day of raw uploads, before transcoded copies.
  • Viewing: 1 billion hours per day at an average of 3 Mbps is 3 billion megabits-hours; per second that is about 1 billion / 24 hours x 3 Mbps = roughly 125 Tbps of average traffic. No origin data center can serve that, so a CDN is mandatory.

The upload and processing pipeline

flowchart LR
    U["Uploader"] --> UP["Upload service (resumable)"]
    UP --> RAW[("Raw storage")]
    RAW --> SPLIT["Split into segments"]
    SPLIT --> TQ["Transcode queue"]
    TQ --> TW["Transcoding workers"]
    TW --> OUT[("Renditions + manifests in object storage")]
    TW --> TH["Thumbnails, captions, moderation"]
    OUT --> CDN["CDN"]

Work is cut into small independent tasks, so thousands of cheap workers process one video in parallel.

  1. Resumable upload: the client sends the file in chunks; if the connection drops, it continues from the last acknowledged chunk.
  2. Split the video into groups of pictures of a few seconds each.
  3. Transcode every piece into several renditions: for instance 240p, 480p, 720p, 1080p and 4K, in multiple codecs (H.264 for compatibility, VP9 or AV1 for better compression).
  4. Package into short segments (2 to 10 seconds) with a manifest file listing every rendition and segment URL. The common formats are HLS and MPEG-DASH.
  5. Side tasks: thumbnails, speech captions, copyright and content checks.
  6. Publish: update metadata (title, owner, status) and warm the CDN for expected popular titles.

Transcoding is CPU-heavy, so it is queue-based with autoscaling, priority (a creator's first upload might beat a back-catalogue re-encode) and retries for failed segments.

Adaptive bitrate streaming

The player downloads a segment, measures how fast it arrived, and chooses the rendition of the next segment to match its bandwidth and buffer. If the network slows, quality drops for a few seconds instead of the video stalling; when it recovers, quality rises.

Python An adaptive bitrate player simulation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

The quality falls through the slow stretch and recovers afterward. One short stall happens when the throughput estimate lags the real drop, which is why real players add safety margins and watch the buffer.

Delivery with a CDN

The origin stores everything; CDN edge servers near viewers cache segments. A popular video's segments are served almost entirely from edge caches. Rarely watched videos (the long tail) miss the cache and fall back to a regional cache, then the origin.

Python CDN hit ratio and origin bandwidth

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Even a cache holding only 5 percent of the titles serves well over half of the requests, and 20 percent serves about three quarters, which is why CDNs work so well for video. Large providers go further and install cache appliances inside ISPs' networks, fed overnight with the next day's likely hits.

Storage and cost

  • Object storage for renditions, in tiers: hot for new and popular, cold for old.
  • Not every video needs every rendition: transcode a basic ladder at upload and add expensive formats (AV1, 4K) only when a video becomes popular.
  • Metadata (titles, owners, view counts) lives in a database; view counters are aggregated asynchronously.
  • Cost drivers: egress bandwidth is usually the largest bill, then storage, then transcoding. Better codecs reduce bandwidth at the price of more compute.
  • Live streaming differs: ingest by RTMP or WebRTC, transcode in real time, package in tiny segments, and accept a few seconds of latency.
  • Security: signed URLs and tokens limit hotlinking; DRM protects licensed content.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Why is a video split into small independent segments for transcoding?

    Small tasks parallelize well and a failed segment is retried alone.
  2. 2. What does adaptive bitrate streaming do?

    The player adapts per segment to avoid stalls while keeping quality as high as the network allows.
  3. 3. What is the main reason a CDN is essential here?

    No origin could serve the aggregate traffic; edges absorb most of it.
  4. 4. What is a manifest file?

    HLS and DASH manifests tell the player which streams and segments exist.

Summary

  • Ingest turns one upload into many renditions through a parallel, queue-based transcoding pipeline.
  • Videos are packaged as short segments with a manifest (HLS or DASH).
  • Adaptive bitrate players pick each segment's quality to match the network.
  • CDNs and ISP caches serve most traffic because viewing is heavily skewed to popular titles.
  • Bandwidth is the biggest cost; manage it with codecs, tiering and lazy transcoding.

Next: Design a Ride-Sharing Service