Skip to content
Codeloom
System Design

System Design: YouTube Architecture Deep Dive

How YouTube serves 1B+ hours of video per day. Covers the video upload pipeline, adaptive bitrate streaming, recommendation engine, and live streaming architecture.

·12 min read · By Codeloom
Advanced 18 min read

What you'll learn

  • How the video upload pipeline transcodes into 100+ formats
  • How adaptive bitrate streaming (DASH/HLS) works
  • How the recommendation engine drives 70% of watch time
  • How YouTube handles live streaming at global scale
  • How Vitess scales MySQL to YouTube-level traffic

Prerequisites

  • Understanding of distributed systems. See [CAP Theorem](/blog/system-design-cap-theorem).
  • Basic knowledge of media formats and streaming concepts.

YouTube serves over 1 billion hours of video every day. Over 500 hours of new video are uploaded every minute. The platform hosts over 800 million videos and serves them to over 2.5 billion monthly active users across every device from smart TVs to feature phones on 2G networks. Building the system that ingests, processes, stores, and delivers this volume of video is one of the largest engineering challenges in computing.

The Video Upload Pipeline

When a creator uploads a video to YouTube, it triggers a processing pipeline that can take minutes to hours depending on the video length and resolution. The original file is just the starting point.

Why Transcoding Matters

A creator might upload a 4K ProRes file from a professional camera or a 720p clip from a phone. Viewers watch on 4K TVs, laptops, phones, and low-end devices on slow connections. YouTube cannot serve the original file to everyone — it must create multiple versions optimized for different devices and bandwidth conditions.

Think of it like a publishing house receiving a manuscript. They do not just print one edition — they create hardcover, paperback, large print, audiobook, and e-book versions. Each format serves a different reader. YouTube does the same with video.

Pipeline Architecture

Video Upload Pipeline:

1. Upload Reception
   -> Chunked upload via resumable HTTP protocol
   -> Chunks stored in Google's distributed storage (Colossus)
   -> Client can resume from last successful chunk on failure

2. Pre-Processing
   -> File format detection (container, codec, resolution)
   -> Audio extraction
   -> Metadata extraction (duration, frame rate, aspect ratio)
   -> Generate initial thumbnail set (ML picks best frames)

3. Transcoding (Borg-scheduled workers)
   -> Encode into multiple resolution/bitrate combinations:
      - 4K (2160p): 20+ Mbps (VP9/AV1)
      - 1440p: 10+ Mbps
      - 1080p: 5-8 Mbps
      - 720p: 2.5-5 Mbps
      - 480p: 1-2.5 Mbps
      - 360p: 0.5-1 Mbps
      - 240p: 0.3-0.5 Mbps
      - 144p: 0.1-0.3 Mbps
   -> Each resolution has multiple bitrate tiers
   -> Total: 100+ encoded versions per video

4. Post-Processing
   -> Content ID matching (copyright detection)
   -> Content moderation (ML classifiers)
   -> Subtitle/caption extraction (speech-to-text)
   -> Chapter detection (scene boundary analysis)

5. Publication
   -> Encoded files distributed to CDN edge locations
   -> Metadata written to Vitess (MySQL cluster)
   -> Video appears in creator's channel and search index

Codec Selection: VP9 and AV1

YouTube has been a driving force behind open video codecs. They deploy VP9 as the primary codec and are increasingly using AV1 for newer content.

AV1 achieves roughly 30 percent better compression than VP9, which means the same visual quality at lower bitrate. For YouTube’s scale, a 30 percent reduction in bandwidth translates to enormous infrastructure savings. However, AV1 encoding is 10-50x more computationally expensive than VP9, so YouTube uses it selectively — primarily for popular videos where the CDN bandwidth savings justify the encoding cost.

Codec Tradeoff:

          Compression    Encode Speed    Decode Support
H.264     Baseline       Fast            Universal
VP9       30% better     Moderate        Most modern devices
AV1       50% better     Very slow       Newer devices only

YouTube's strategy:
- Encode everything in VP9 (good balance)
- Re-encode popular videos in AV1 (savings justify cost)
- Keep H.264 for legacy device fallback

Adaptive Bitrate Streaming

When you watch a YouTube video, your player does not download a single file. It requests the video in small segments (typically 2-5 seconds each), and it can switch quality levels between segments based on your current bandwidth.

How DASH Works

YouTube uses DASH (Dynamic Adaptive Streaming over HTTP), a protocol where the video is split into segments at multiple quality levels.

DASH Streaming Flow:

1. Player requests MPD (Media Presentation Description)
   -> XML manifest listing all available quality levels
   -> URL patterns for each segment at each quality

2. Player estimates available bandwidth
   -> Measures download speed of initial segments

3. Segment-by-segment adaptation:
   Second 0-5:   720p (estimating bandwidth)
   Second 5-10:  1080p (bandwidth is good)
   Second 10-15: 1080p (stable)
   Second 15-20: 480p (bandwidth dropped, congestion)
   Second 20-25: 720p (recovering)

4. Buffer management:
   -> Player maintains 20-40 seconds of buffer
   -> If buffer drops below threshold, immediately
      switch to lower quality to refill
   -> If buffer is healthy, try higher quality

The adaptive bitrate algorithm is critical to user experience. Too aggressive in upgrading quality and you risk re-buffering (the video pauses to load). Too conservative and you serve lower quality than the connection supports. YouTube’s algorithm uses both bandwidth estimation and buffer occupancy to make switching decisions.

Edge Caching

YouTube uses Google’s global network of edge caches (part of Google’s CDN infrastructure). Popular videos are cached at edge locations close to viewers. The cache hierarchy works like this:

Cache Hierarchy:

Viewer's Device
    |
    v
ISP-Level Cache (Google Global Cache)
    |  Hit rate: ~70-80% for popular content
    v
Metro-Level Edge (Google PoP)
    |  Hit rate: ~90%+ cumulative
    v
Regional Data Center
    |  Serves long-tail content
    v
Origin (Google's core data centers)
    |  Stores all content

Google Global Cache (GGC) is a program where Google places caching servers inside ISP networks — similar to Netflix’s Open Connect. For popular videos, the content never leaves the ISP’s network, reducing both latency and transit costs.

Recommendation Engine: 70% of Watch Time

YouTube’s recommendation system drives over 70 percent of total watch time. The “Up Next” sidebar and the homepage recommendations are powered by deep neural networks that have been refined over a decade.

Two-Stage Architecture

YouTube published their recommendation architecture in a landmark 2016 paper. The system uses a two-stage approach: candidate generation followed by ranking.

Recommendation Pipeline:

Stage 1: Candidate Generation
  Input: User's watch history, search history, demographics
  Model: Deep neural network
  Output: ~hundreds of candidate videos from millions
  Latency budget: ~10ms

  The model learns embeddings for videos and users
  in a shared vector space. "Similar" videos and users
  are close together in this space.

Stage 2: Ranking
  Input: Candidate videos + rich features
  Model: Deep neural network (larger than Stage 1)
  Features per candidate:
    - Video features: age, length, channel, topic
    - User features: watch history, session context
    - Cross features: user-video interaction history
    - Context: time of day, device type, location
  Output: Predicted watch time for each candidate
  Final ranking: Sort by predicted watch time

  Key insight: YouTube optimizes for watch time,
  not click-through rate. This reduces clickbait
  because a video you click but immediately leave
  scores poorly.

The Watch Time Objective

YouTube deliberately chose to optimize for watch time rather than clicks. This was a pivotal decision. Optimizing for clicks led to clickbait — sensational thumbnails and titles that got clicks but left users disappointed. Optimizing for watch time rewards videos that people actually enjoy and watch to completion.

# Simplified ranking model concept
def predict_watch_time(user, video, context):
    """
    Predict how long this user will watch this video.
    Higher watch time prediction = higher ranking.
    """
    features = {
        # User features
        'watch_history_embedding': user.history_embedding,
        'avg_session_length': user.avg_session,
        'preferred_categories': user.category_prefs,

        # Video features
        'video_embedding': video.embedding,
        'video_length': video.duration,
        'channel_subscriber_count': video.channel.subscribers,
        'video_age_hours': video.age_hours,
        'historical_ctr': video.click_through_rate,
        'avg_watch_percentage': video.avg_watch_pct,

        # Context features
        'time_of_day': context.hour,
        'device_type': context.device,
        'is_weekend': context.is_weekend,
    }

    return ranking_model.predict(features)

Freshness and Exploration

The recommendation system must balance exploitation (showing videos it knows the user will like) with exploration (showing videos it is uncertain about to learn user preferences). YouTube uses a combination of techniques:

  • Freshness boost — new videos get a temporary ranking boost to collect engagement data
  • Thompson sampling — for uncertain candidates, sample from the distribution of predicted watch time rather than using the point estimate
  • Diverse slates — ensure the recommendation list covers multiple topics rather than collapsing to a single category

Comment System at Scale

YouTube comments seem simple but involve significant engineering at scale. A popular video can have millions of comments, each with likes, replies, and spam filtering.

Architecture

Comment System:

Write Path:
  User posts comment
    -> Spam/toxicity classifier (ML model, < 100ms)
    -> If passes: write to Spanner (Google's distributed DB)
    -> Update comment count (async, eventually consistent)
    -> Send notification to creator (async)
    -> Index in search (async)

Read Path:
  User opens comment section
    -> Default sort: "Top Comments"
    -> Query Spanner for top-level comments
       sorted by engagement score
    -> Paginate (load 20 at a time)
    -> Reply threads loaded on-demand (expand to view)

Engagement Score:
  score = likes - dislikes + reply_count * 0.5 + recency_boost
  (Actual formula is more complex, ML-based)

Spam is a massive challenge. YouTube processes billions of comments per month and uses ML classifiers to catch spam, scams, and abuse. The classifier runs synchronously — a comment is evaluated before it becomes visible. False positives are reviewed by human moderators.

Live Streaming Architecture

YouTube Live supports millions of concurrent live streams, from small creators to events with millions of simultaneous viewers.

How Live Streaming Differs

Live streaming inverts the normal YouTube model. Instead of pre-encoding and caching, the system must ingest, transcode, and distribute video in real-time with minimal latency.

Live Streaming Pipeline:

1. Ingest
   Creator streams via RTMP/HLS to nearest ingest server
   -> Ingest server validates stream and authenticates

2. Real-Time Transcoding
   -> Transcode into multiple quality levels simultaneously
   -> Unlike VOD, encoding must be real-time
      (1 second of video encoded in < 1 second)
   -> Fewer quality levels than VOD (typically 4-6)

3. Segmentation
   -> Split transcoded stream into 2-5 second segments
   -> Each segment immediately available via CDN

4. Distribution
   -> Segments pushed to edge caches
   -> Viewers request segments via DASH/HLS
   -> For popular streams: replicated to thousands of edge nodes

5. Latency Targets
   Normal live:     20-30 seconds glass-to-glass
   Low latency:     4-6 seconds (WebRTC-based path)
   Ultra-low:       < 2 seconds (limited availability)

The tradeoff: Lower latency means smaller segments,
less buffering tolerance, and worse ABR adaptation.

Scaling for Major Events

When a live event (like a music concert or product launch) attracts millions of simultaneous viewers, YouTube faces a thundering herd problem. Millions of clients request the same segment at the same time.

The solution is aggressive edge caching. A live segment is pushed to edge servers proactively. When millions of viewers request it, they all hit their local edge cache. The origin server sees only one request per edge location, not one per viewer.

For the largest events (50M+ concurrent viewers), YouTube pre-provisions edge capacity and uses multicast-like distribution internally to push segments to edge locations simultaneously rather than having each edge pull independently.

Vitess: Scaling MySQL

YouTube’s metadata (video info, channels, playlists, comments) is stored in MySQL, scaled through Vitess — an open-source database clustering system built by YouTube engineers.

Why Vitess Exists

YouTube’s data outgrew a single MySQL instance long ago. Vitess adds a proxy layer that makes a cluster of MySQL instances appear as a single logical database.

Vitess Architecture:

Application
    |
    v
VTGate (query router)
    |-- Parses SQL queries
    |-- Determines which shard(s) to query
    |-- Merges results from multiple shards
    |
    v
VTTablet (per-shard agent)
    |-- Connection pooling
    |-- Query rewriting
    |-- Schema management
    |
    v
MySQL (one instance per shard)

Key Features:
  - Horizontal sharding (split by key range or hash)
  - Online schema changes (no downtime ALTER TABLE)
  - Connection pooling (MySQL handles ~5000 connections;
    Vitess multiplexes millions of app connections)
  - Cross-shard queries (scatter-gather when needed)

Vitess handles online schema changes — altering a table’s schema without taking it offline. For YouTube, taking the videos table offline for an ALTER TABLE is not an option. Vitess applies the schema change to a copy of each shard, then atomically swaps the old table for the new one.

Google’s Infrastructure Advantage

YouTube benefits from Google’s internal infrastructure in ways that are difficult to replicate:

  • Borg (now Kubernetes’ ancestor) — schedules transcoding jobs across millions of machines
  • Colossus — Google’s distributed file system, successor to GFS, stores all video files
  • Spanner — globally consistent database used for some YouTube workloads
  • Bigtable — used for recommendation model serving and analytics
  • Borg-managed TPUs — train and serve recommendation models
Technology Stack:

Languages:    Python (original, some services), Java, C++ (transcoding),
              Go (newer services)
Databases:    Vitess/MySQL (metadata), Bigtable (analytics, ML),
              Spanner (globally consistent workloads)
Storage:      Colossus (video files), CDN edge caches
ML:           TensorFlow (recommendations, content moderation)
Streaming:    DASH protocol, custom ABR algorithms
Processing:   Borg (job scheduling), MapReduce/Flume (batch)
CDN:          Google Global Cache + Google Edge PoPs
Monitoring:   Borgmon (predecessor to Prometheus)

Key Takeaways

YouTube’s architecture reveals principles specific to video-heavy, content-heavy platforms:

  • Encode once, serve forever. The transcoding pipeline is expensive but runs once per video. The CDN serves the encoded files billions of times. Investing heavily in encoding quality (AV1 adoption, per-title optimization) pays for itself through bandwidth savings.
  • Adaptive streaming is non-negotiable. Users on 100 Mbps fiber and users on 2G mobile both need to watch video. DASH/HLS with multiple quality levels serves both without human intervention.
  • Recommendations are the product. When 70 percent of watch time comes from recommendations, the recommendation system is not a feature — it is the core product. Optimizing for watch time rather than clicks was a strategic decision that shaped the entire platform.
  • Separate hot from cold. A viral video from today and an obscure video from 2008 have radically different access patterns. Edge caches handle the hot content; origin storage handles the long tail.
  • Live streaming is a fundamentally different system. It shares infrastructure with video-on-demand but the constraints are inverted: latency matters more than quality, and content cannot be pre-cached.

When you open YouTube and a video starts playing within a second, you are watching a system that transcoded that video into dozens of formats, cached the popular segments at an edge server near you, selected the optimal quality for your current bandwidth, and chose to show you this video because a deep neural network predicted you would watch it longer than any alternative — all invisible behind a simple play button.