24 - System Design: Petabyte-Scale Video Streaming (YouTube / Netflix)

Executive Summary: Serving video at global scale (YouTube handles 500 hours uploaded per minute; Netflix accounts for β‰ˆ 15% of global internet downstream traffic) is fundamentally different from serving standard web APIs. Monolithic video files cannot be streamed over fluctuating mobile networks. A production video architecture relies on Distributed DAG Transcoding, slicing videos into short 2–6 second chunks encoded across multiple bitrate profiles, indexed by HLS / MPEG-DASH manifests (.m3u8), dynamically navigated via Adaptive Bitrate Streaming (ABR), and cached across global Edge CDN PoPs (e.g. Netflix Open Connect).


🏒 Real-world case studies: how Google & Netflix approach this, in REAL_WORLD_EXAMPLES.md.

⏱️ Prerequisites: What You Need to Know First

To get the most out of this module, make sure you understand:


πŸ§’ The 30-Second Version (ELI5)

Imagine you are baking a massive 50-pound wedding cake to share with people all over the city:


πŸ• Everyday Analogy

Video System Component Bakery Analogy Technical Reality in Netflix / YouTube
Raw Master Video 50-pound raw unbaked wedding cake batter Uncompressed 4K ProRes master file (50–100 GB)
Transcoding Worker Fleet of industrial ovens baking different sizes Distributed FFmpeg cluster encoding H.264, H.265, AV1
Video Chunk (.ts / .m4s) Individual 4-second bite-sized cupcake slices 2–6 second HTTP chunk segment
Manifest (.m3u8 / .mpd) The bakery menu listing all cupcake sizes and flavors Master playlist index containing URLs for every resolution
Adaptive Bitrate (ABR) Automatically eating smaller cupcakes when running out of breath Client player dynamically switching bitrates based on bandwidth
Edge CDN Appliance Local corner bakery stores holding fresh cupcakes Edge PoP cache (Netflix OCA / Cloudflare) serving chunks in 3ms

🧩 The Problem It Solves

Why Monolithic Video Fails

If you serve a single 2-hour 4K MP4 file (https://example.com/movie.mp4, 8 GB):

  1. Bandwidth Waste: If a user watches 30 seconds and navigates away, the browser has already pre-buffered 500 MB of data over cellular, wasting money for both the user and the platform.
  2. Buffering Hell: If the user enters an elevator and network speed drops from 20 Mbps to 1 Mbps, playback immediately freezes with a spinning loading circle because the monolithic file bitrate exceeds current network capacity.
  3. Device Incompatibility: A 4K TV needs high-bitrate HEVC/AV1, while an older Android smartphone only supports H.264 baseline profile.

How Modern Video Streaming Solves This

  1. Slices the video into discrete 4-second chunks.
  2. Encodes each chunk into multiple resolution/bitrate tiers (360p, 480p, 720p, 1080p, 4K).
  3. The client video player monitors its own download buffer in real-time. If the buffer shrinks, it requests the next 4-second chunk in 720p rather than 1080p. Buffering drops to zero.

πŸ–ΌοΈ Picture It: Architecture & Flow

High-Resolution Video Streaming Animation

Below is an animated trace of video chunking, distributed DAG transcoding, and real-time Adaptive Bitrate playback switching from 1080p β†’ 720p with zero freezing:

Video Streaming Architecture


End-to-End Transcoding & Playback Pipeline

flowchart TD
    subgraph Ingestion["1. Ingestion Tier"]
        Creator["πŸ“Ή Video Creator"] -->|Multipart Upload| S3Raw[("Raw Video Bucket<br/>(S3 / GCS Master)")]
        S3Raw --> Slicer["Video Slicer<br/>(Splits into 4s GOP chunks)"]
    end

    subgraph Transcode["2. Distributed DAG Transcoder Fleet"]
        Slicer --> Queue["πŸ“œ Kafka / SQS Task Queue"]
        Queue --> W1["Worker 1: 1080p (AV1)"]
        Queue --> W2["Worker 2: 720p (H.264)"]
        Queue --> W3["Worker 3: 360p (H.264)"]
        W1 --> Storage[("Transcoded Chunks Bucket<br/>.ts / .m4s segments + .m3u8")]
        W2 --> Storage
        W3 --> Storage
    end

    subgraph CDN["3. Global Delivery Tier"]
        Storage --> Shield["πŸ›‘οΈ Origin Shield (Regional Cache)"]
        Shield --> Edge["🌐 Edge CDN PoPs (Netflix Open Connect)"]
    end

    subgraph Playback["4. Client Playback Tier"]
        Edge -->|1. Fetch master.m3u8| Player["πŸ“± Client Player (ABR Engine)"]
        Player -->|"2. Stream 1080p chunks (Wi-Fi)"| Edge
        Player -.->|3. Auto-downgrade to 480p on 4G| Edge
    end

    style Ingestion fill:#161b22,stroke:#58a6ff,stroke-width:2px;
    style Transcode fill:#161b22,stroke:#3fb950,stroke-width:2px;
    style CDN fill:#161b22,stroke:#d29922,stroke-width:2px;
    style Playback fill:#161b22,stroke:#a371f7,stroke-width:2px;

πŸ”’ Walk-Through with Real Numbers

Let's estimate storage, compute, and bandwidth for a platform at YouTube / Netflix scale:

1. Ingestion & Storage Estimation

Raw Storage = 720,000 hrs Γ— 2 GB = 1,440 Terabytes / day (1.44 PB/day)
Total Daily Storage β‰ˆ 1.44 PB Γ— 2.5 = 3.6 Petabytes / day!

2. Egress Bandwidth & CDN Offload Math

Total Daily Egress = 1,000,000,000 hrs Γ— 3600s Γ— 375 KB/s β‰ˆ 1.35 Exabytes / day!
Bandwidth = (1 Billion hrs Γ— 3 Mbps Γ— 3600s) / (86,400s) = 125 Terabits / second (125 Tbps)!

Why Edge CDNs are Mandatory:


βš™οΈ How It Works Under the Hood

1. Modern Video Codecs & Formats

A raw uncompressed 4K video frame (3840 Γ— 2160 Γ— 3 bytes) is 24.8 MB per frame! At 60 fps, that is 1.48 GB per second. Video codecs use temporal and spatial compression to reduce file sizes by >99%:

Codec Efficiency Hardware Support Licensing Best Used For
H.264 (AVC) Baseline 99.9% of all devices on Earth Royalty fees Fallback for legacy devices & mobile
H.265 (HEVC) 50% better than H.264 Modern 4K TVs, iPhones, Androids Complex royalties 4K & HDR premium streaming
VP9 50% better than H.264 Broad browser support (Chrome, Android) Open source (Google) YouTube default stream
AV1 30% better than HEVC Modern GPUs & latest smartphones Open source / Royalty-free Next-generation YouTube & Netflix standard

2. The HLS (HTTP Live Streaming) Protocol & Manifests

HLS (RFC 8216) works entirely over standard HTTP/HTTPS ports (80/443). There are no proprietary streaming server sockets!

  1. The client player requests the Master Manifest (master.m3u8): (Refer to the companion playlist file master.m3u8 for the multi-bitrate index manifest.)
  2. The player inspects its local network connection speed (e.g. 8 Mbps) and chooses 1080p/index.m3u8: (Refer to the segment playlist file stream.m3u8 for 4-second chunk declarations.)
  3. The player issues standard HTTP GET requests: GET /video/1080p/chunk_000.ts GET /video/1080p/chunk_001.ts

3. Distributed DAG Transcoding Architecture

A 2-hour movie cannot be transcoded sequentially on a single computerβ€”it would take 4 hours to process!


βš–οΈ Trade-Offs & Decision Matrix

Dimension HLS (HTTP Live Streaming) WebRTC RTMP (Legacy)
Latency Medium (~2–6 seconds) Ultra-Low (<500 ms) Low (~1–3 seconds)
CDN Scalability Infinite (Standard HTTP Caching) Extremely Poor (No CDN caching) Poor (Requires Flash media servers)
Protocol Standard HTTP/HTTPS UDP / SRTP / ICE TCP port 1935
Best For On-Demand (Netflix, YouTube), Live TV 2-way Video Calls (Zoom, Discord) Ingestion stream from creator (OBS)

🚫 Common Mistakes & Production Pitfalls

  1. Non-Keyframe Aligned Chunking:

    • Pitfall: Slicing video chunks at arbitrary time marks (e.g. exactly every 4.000s) without aligning to I-frames (Keyframes).
    • Result: Video artifacts, green screen glitches, and player crashes during ABR resolution switching because P-frames and B-frames have missing references. Always split video strictly on Keyframe (GOP) boundaries.
  2. Serving Video Chunks Directly from S3/Origin:

    • Pitfall: Letting user video players fetch .ts chunks directly from AWS S3.
    • Result: Catastrophic cloud egress bills (<span class="math-inline">0.08 per GB). At 100 TB daily egress, S3 egress alone costs </span>8,000 per day! Always put an Edge CDN (CloudFront/Cloudflare) in front with an aggressive 30-day cache header.
  3. Ignoring Client-Side Battery & CPU Drain:

    • Pitfall: Forcing complex AV1 decoding on low-end mobile devices without dedicated AV1 hardware chips.
    • Result: Software decoding burns 100% of the mobile CPU, drains the phone's battery in 45 minutes, and causes frame drops. Always inspect client user-agent capabilities and fall back to hardware-accelerated H.264 when necessary.

πŸ’» Try It Yourself: Python Simulation

The companion simulation script demo.py implements:

  1. Video Slicing into 4-second HLS chunks across 4 resolution profiles (1080p, 720p, 480p, 360p).
  2. Generating an HLS master.m3u8 manifest index playlist.
  3. An Adaptive Bitrate (ABR) Video Player dynamically shifting stream quality as simulated network speeds drop from 12 Mbps to 0.6 Mbps.
  4. Proof of zero-rebuffering playback continuity.

Run it locally in your terminal:

python demo.py

🌍 Where You'll See It in Production


🎯 Top Interview Questions & Deep Answers

Q1: "How does Adaptive Bitrate Streaming (ABR) work, and how does the client decide when to switch quality?"

Interviewer Goal: Testing client-server video protocol mechanics and control algorithms.

Model Answer: "In Adaptive Bitrate Streaming (ABR):

  1. The server pre-encodes the video into multiple discrete bitrate streams (e.g. 1080p @ 6 Mbps, 720p @ 2.5 Mbps, 360p @ 400 kbps) sliced into uniform 2–6 second chunks, described by a master .m3u8 playlist.
  2. The client video player runs an ABR Decision Engine governed by two metrics:
    • Throughput-Based Estimation: Measuring the download duration of the preceding chunk:
Estimated Bandwidth = (Chunk Size (Bits)) / (Download Duration (Seconds))
  1. If network throughput drops or the forward buffer dips below a safety threshold (e.g. <5 seconds), the player proactively requests the next chunk in a lower resolution (e.g. 720p).
  2. Because all resolution streams are strictly aligned on identical timestamp boundaries, the player seamlessly splices the lower-resolution chunk into playback without any audio popping, frame jitter, or buffering freezes."

Q2: "Why do systems like YouTube split video files into small chunks during transcoding rather than encoding the full file?"

Interviewer Goal: Evaluating distributed processing, fault tolerance, and pipeline parallelism.

Model Answer: "Transcoding an entire 2-hour 4K video file as a single monolithic job is an anti-pattern:

  1. Pipeline Parallelism: Slicing the video into 4-second chunks allows hundreds of distributed worker containers to encode different chunks simultaneously. A 2-hour movie is processed in parallel across 500 containers in 2 minutes, rather than waiting 2 hours for a single machine.
  2. Failure Isolation: Video transcoding is CPU/GPU intensive and prone to worker crashes or spot-instance terminations. If a worker crashes on chunk 42, only that single 4-second chunk needs to be re-tried, rather than restarting a 2-hour encoding job from the beginning.
  3. Pipelined Ingestion: The transcoding pipeline can begin encoding the first 10 seconds of a video while the creator is still uploading the remaining 90% of the file."

Q3: "How does Digital Rights Management (DRM) protect video content in streaming systems?"

Interviewer Goal: Testing media security, key licensing, and content encryption.

Model Answer: "Video streaming platforms protect copyrighted content using Common Encryption (CENC - ISO/IEC 23001-7) combined with DRM systems (Google Widevine, Apple FairPlay, Microsoft PlayReady):

  1. Symmetric Chunk Encryption: Video chunks are encrypted using AES-128 in CTR or CBC mode during transcoding. The encrypted .ts chunks can be publicly cached on edge CDNs without security risk.
  2. Encrypted Key Transmission: The AES decryption key is not stored in the manifest.
  3. License Acquisition Handshake:
    • When the user hits Play, the client authenticates with the platform and receives a signed DRM token.
    • The client browser's hardware-isolated Content Decryption Module (CDM) communicates directly with the platform's DRM License Server.
    • The license server issues the decryption key, which is injected directly into the device's secure video hardware pipeline (HDCP). Application JavaScript has zero access to the raw key or decrypted video frames, preventing screen recording."

Q4: "How does live streaming (e.g. Twitch, live sports) differ from Video-on-Demand (VoD like Netflix)?"

Interviewer Goal: Assessing real-time latency trade-offs in media pipelines.

Model Answer: "Live streaming introduces strict end-to-end latency constraints that fundamentally change the architecture:

  1. Latency Requirements:
    • VoD (Netflix): Latency doesn't matter; pre-transcoding can take hours, and players buffer 30–60 seconds ahead.
    • Live Streaming: Expects sub-3-second latency so viewers can interact with live chat.
  2. Chunk Sizes:
    • VoD uses 4–6 second chunks for maximum compression efficiency.
    • Low-Latency HLS (LL-HLS) and DASH-CMAF split chunks into sub-second 'parts' (200–500 ms chunks), streaming them over HTTP/2 chunked transfer encoding as they are being encoded!
  3. Transcoding Compute:
    • VoD uses multi-pass high-compression encoding (e.g. AV1/HEVC taking 10x real-time).
    • Live streaming requires real-time single-pass hardware encoding (GPU/ASIC) outputting frames in <50 ms."

Q5: "What is Origin Shielding, and why is it crucial for high-traffic video platforms?"

Interviewer Goal: Testing multi-tier CDN caching economics and origin protection.

Model Answer: "In a global CDN with 300+ Edge PoPs:

Origin Shielding (Tiered Caching):


βœ… Check Yourself: Flash Quiz

1. Why can't you use WebRTC to stream a live football match to 50 million concurrent viewers?
Because WebSockets and WebRTC establish direct stateful peer connections that cannot be cached by standard HTTP Edge CDNs. Transmitting 50 million individual peer streams would require petabits of centralized server egress! HLS uses standard HTTP GET requests for static .ts chunks, allowing Edge CDNs to cache and fan out video to 50M viewers with 99% origin offload.
2. In HLS, what is the role of the master.m3u8 file vs an individual resolution manifest file?
The master.m3u8 manifest acts as the top-level index listing available resolution and bitrate streams (e.g. 1080p, 720p, 360p). The individual resolution manifest (e.g. 1080p/index.m3u8) lists the actual URLs of the sequential 4-second video chunk files (chunk_001.ts, chunk_002.ts).
3. Why must video chunking strictly occur on I-frame (Keyframe) boundaries?
Because I-frames are the only frames that contain complete, self-contained image data. P-frames and B-frames only contain delta differences relative to preceding frames. Slicing at a non-keyframe boundary results in missing reference frames, causing visual artifacting and player crashes during adaptive bitrate switches.