Skip to content
learn
  1. Learn
  2. High Level System Design
  3. Design Case Studies

System Design HLD-19.11

Design YouTube / Netflix (video streaming)

Design video streaming: resumable uploads, a parallel transcoding DAG, adaptive bitrate with HLS and DASH, CDN placement, metadata and view counts.

Advanced 25 min Last reviewed

The problem

A creator uploads a 2 GB video file from a phone. Minutes later, viewers on fibre, on a train and on a cheap phone in another country all press play and get a picture within a second or two, which sharpens or softens as their connection changes but rarely freezes. The platform has to accept huge uploads over flaky networks, turn each video into many versions, and then push an enormous amount of bytes to viewers all over the world, cheaply.

Two facts shape the design. First, video is big and bandwidth is the cost: serving bytes dominates everything else, so the bytes must come from caches close to viewers, not from your data center. Second, networks vary, so you never send "the video": you send small chunks, each available at several qualities, and let the player choose.

Requirements

Functional

  • Upload videos of up to a few GB, resumable after a dropped connection.
  • Process each upload into several resolutions and bitrates, with thumbnails.
  • Play videos on web, mobile and TV, adapting quality to the viewer's bandwidth.
  • Show title, description and a view count; search and recommendations are separate services.

Non-functional

  • Scale: 100 million daily viewers watching 30 minutes each; 100,000 uploads a day averaging 10 minutes.
  • Playback: start in about 2 seconds, with rare rebuffering.
  • Processing: a new video playable within minutes of upload.
  • Durability: never lose an original upload.

Out of scope

  • Live streaming (an extension), recommendations, comments, ads, and digital rights management beyond a mention.

Estimates

  • Watch time: 100M x 30 minutes = 3 billion minutes a day. Divided by 1,440 minutes in a day, that is about 2.1 million people watching at any moment on average.
  • Egress: at an average of 3 Mbit/s per stream, 2.1 million streams is about 6.25 Tbit/s on average, and more at the evening peak. No single data center serves this; a CDN with servers inside or near viewers' networks must.
  • Uploads: 100,000 x 10 minutes = 1 million minutes of new video a day. At 10 Mbit/s for originals, a minute is 75 MB, so 75 TB of originals a day.
  • Renditions: the ladder below (0.4 + 0.8 + 1.4 + 2.8 + 5.0 = 10.4 Mbit/s for all five qualities together) adds 78 MB per minute, so another 78 TB a day, before extra codecs.
  • Transcoding compute: assume encoding one rendition takes 2 CPU-minutes per minute of video. Five renditions x 2 x 1 million minutes = 10 million CPU-minutes a day, about 7,000 cores busy around the clock. Uploads are bursty, so this is an elastic worker pool fed by a queue.

API

POST /v1/uploads                      -> { upload_id, part_urls[] }   # presigned URLs, one per part
PUT  <part_url>                       # client uploads parts directly to object storage
POST /v1/uploads/{upload_id}/complete -> { video_id, status: "processing" }
GET  /v1/videos/{video_id}            -> { title, status, manifest_url, thumbnails, views }
GET  <cdn>/v/{video_id}/master.m3u8   # manifest, then segments from the CDN
POST /v1/videos/{video_id}/views      # playback events, batched by the player

Uploads go straight to object storage with presigned URLs, in parts, so the API servers never carry video bytes and a dropped part is retried alone ( Object storage, block storage and file systems ).

Data model

  • Videos (relational or document store): video_id, owner_id, title, description, status (uploading, processing, ready, failed), duration, created_at.
  • Renditions: video_id, codec, resolution, bitrate, manifest_path.
  • Objects in object storage: originals at raw/{video_id}, and processed segments at v/{video_id}/{rendition}/{n}.m4s with manifests beside them. Segments are immutable, which makes them perfect for caching.
  • View counts: a counter per video, updated in batches from a stream of playback events, not one database write per view.

High-level design

Video platform: upload, transcode and playback paths Upload path: the creator gets presigned URLs from the upload service, sends parts directly to raw object storage, and a video row is created in the metadata database. Transcode path: a splitter cuts the original into chunks and fans them out to encoders for 1080p, 720p and 360p in parallel; a packager writes segments and HLS or DASH manifests to the origin store and marks the video ready. Playback path: the player fetches video metadata and the manifest URL, then requests segments from a nearby CDN edge, which fills misses from the origin; the player switches renditions as bandwidth changes. parts video row segments ready manifest URL segments on miss Creator Upload service presigned URLs Raw videos object storage Metadata DB status, renditions Splitter chunks + queue 1080p 5 Mbps 720p 2.8 Mbps 360p 0.8 Mbps Packager HLS / DASH Origin segments CDN edge near the viewer Player picks a bitrate Figure 1. Upload, transcode, play. Encoders fan out per rendition and per chunk, so a long video is processed in parallel. On playback the CDN serves the bytes; the brightest rendition is the highest bitrate.
  1. Upload path The upload service creates a video row and returns presigned URLs. The client sends the file in parts directly to object storage, retrying only failed parts, then calls complete.
  2. Transcode path A splitter cuts the original into chunks of a few seconds and queues one encoding task per chunk and rendition. Encoders run in parallel; a packager assembles segments and HLS or DASH manifests in the origin store, and the video is marked ready.
  3. Playback path The player gets the manifest URL from the metadata service, then requests segments from a nearby CDN edge, which fetches misses from the origin. After each segment the player picks the rendition its bandwidth can sustain.

Deep dives

Deep dive 1: The transcoding DAG

Encoding a 10-minute video into five renditions on one machine takes a long time. The trick is that video is made of independent pieces: encoders insert a keyframeA frame that can be decoded on its own, without earlier frames. Segments start on keyframes so each can be played independently. at regular intervals, and each stretch between keyframes can be encoded separately.

The pipeline is a directed acyclic graph of tasks, run by a workflow engine ( Workflow engines ):

  1. Validate the upload (is it a real video, what codec, how long) and extract metadata.
  2. Split it into chunks of a few seconds on keyframe boundaries.
  3. Encode each chunk into each rendition: with 4-second chunks, a 10-minute video is 150 chunks, and 5 renditions x 150 chunks = 750 independent tasks, spread over a worker pool through a queue ( Why async? Queues explained ).
  4. Package the encoded chunks into segments and write the manifests.
  5. Side tasks in parallel: thumbnails, captions, content checks.

Because tasks are independent and idempotent (same input, same output file), a failed task is simply retried, and a video becomes playable at low quality first if the 360p tasks are prioritized. Expensive renditions (such as 4K or a newer, more efficient codec) can be produced only for videos that become popular.

Deep dive 2: Adaptive bitrate streaming

With HLS or DASHHTTP Live Streaming and Dynamic Adaptive Streaming over HTTP: formats where a manifest lists the renditions, and each rendition is a list of short segment files fetched over plain HTTP., the player first downloads a manifest that lists the renditions, then fetches the video as a sequence of segments of a few seconds each. Before each request it chooses a rendition. Plain HTTP for every segment is what lets ordinary CDNs and browser caches carry video.

The player's job is to keep its buffer (seconds of video downloaded but not yet shown) from running dry. Here is a 16-segment session where bandwidth dips from 8 to 1.2 Mbit/s and recovers:

abr.pyruns with python3 (standard library)
# Adaptive bitrate: a player downloads 4-second segments while bandwidth
# dips. Compare a fixed 1080p stream with a throughput-based player.
LADDER = [("240p", 0.4), ("360p", 0.8), ("480p", 1.4),  # (name, Mbps)
          ("720p", 2.8), ("1080p", 5.0)]
SEG = 4  # seconds of video per segment
# bandwidth (Mbps) while each segment downloads: good, a dip, recovery
BW = [8, 8, 8, 7, 6, 3, 1.5, 1.2, 1.2, 1.5, 2, 4, 6, 8, 8, 8]

def play(policy):
    buffer, stall, est, picks = 0.0, 0.0, None, []
    for i, bw in enumerate(BW):
        name, rate = policy(est, buffer)
        download = rate * SEG / bw     # seconds to fetch this segment
        if i > 0:                      # playback drains the buffer
            stall += max(0.0, download - buffer)
            buffer = max(0.0, buffer - download)
        buffer += SEG
        est = bw if est is None else 0.7 * est + 0.3 * bw  # smoothed
        picks.append(name)
    avg = sum(dict(LADDER)[p] for p in picks) / len(picks)
    return picks, stall, avg

def fixed(est, buffer):
    return LADDER[-1]

def adaptive(est, buffer):
    if est is None:
        return LADDER[1]               # start low: fast first frame
    safe = 0.8 * est if buffer > 8 else 0.5 * est  # careful if low
    fits = [r for r in LADDER if r[1] <= safe]
    return fits[-1] if fits else LADDER[0]

print("bandwidth Mbps:", " ".join(f"{b:g}" for b in BW))
for label, policy in (("fixed 1080p", fixed), ("adaptive", adaptive)):
    picks, stall, avg = play(policy)
    print(f"{label}: stalled {stall:.1f} s, average {avg:.2f} Mbps")
    if len(set(picks)) > 1:
        print("  " + " ".join(p.rstrip("p") for p in picks))

Output

bandwidth Mbps: 8 8 8 7 6 3 1.5 1.2 1.2 1.5 2 4 6 8 8 8
fixed 1080p: stalled 48.9 s, average 5.00 Mbps
adaptive: stalled 0.5 s, average 2.49 Mbps
  360 720 720 1080 1080 1080 720 480 480 480 360 360 480 720 720 720

The fixed 1080p stream needed 13 to 17 seconds to download each 4-second segment during the dip, so the viewer stared at a spinner for 48.9 seconds in total. The adaptive player dropped to 480p and 360p, stalled for half a second at the bottom of the dip, and climbed back as bandwidth returned. Notice its weaknesses too: it chose 1080p for one segment after the dip began, because its smoothed estimate still remembered the fast network, and it was slow to climb back. Real players combine throughput estimates with buffer level for exactly these reasons. Playback also starts on a low rendition so the first frame appears quickly.

Deep dive 3: Getting bytes close to viewers

At terabits per second, the CDN ( Content Delivery Networks ) is not an optimization, it is the delivery system. Two properties help:

  • Popularity is skewed: a small share of videos gets most of the views. Those segments are cached at the edges and almost never touch the origin. The long tail of rarely watched videos is served from a few regional caches or the origin.
  • Segments are immutable: a segment URL never changes content, so it can be cached for a very long time with no invalidation.

For predictable demand, such as a catalog of films and series, content can be pre-positioned: copied to edge servers overnight, before anyone asks for it, so the busy evening hours only serve cached bytes. Large platforms go further and put their own cache servers inside internet providers' networks, which shortens the path and saves the provider transit capacity.

Deep dive 4: Counting views

A popular video can get thousands of views per second, and a single counter row would become a hot key ( Hot partitions and the celebrity problem ). Players send playback events in batches; a stream processor ( Stream processing ) filters out bots and repeats, then adds the counts per video every few seconds into the store. The number shown on the page lags by a little and nobody minds; billing creators or advertisers uses the slower, reconciled batch counts.

Try it

This interactive needs JavaScript. The diagrams and text above cover the same idea.

Try this:

  1. Set the hit ratio to 90 percent, then 99 percent. How much does traffic to the origin fall? At 6 Tbit/s of egress, what does each percent mean in bandwidth?
  2. Long-tail videos have low hit ratios. Where would you serve them from, and does the extra latency matter once the buffer is full?
  3. Why can a video segment use a much longer cache lifetime than the video's metadata (title, view count)?

Bottlenecks and failure modes

FailureImpactMitigation
Upload drops at 90 percentCreator starts overMultipart resumable uploads straight to object storage
Transcode backlog after a spikeNew videos slow to appearQueue with autoscaled workers; encode low renditions first
Encoder crashes mid-taskMissing segmentIdempotent tasks retried by the workflow engine
Viral videoOrigin overwhelmed by missesCDN request coalescing, tiered caches, pre-warm popular content
Viewer bandwidth dropsRebufferingAdaptive bitrate with buffer-aware switching
Hot view counterWrite contention on one rowBatch events in a stream processor before writing

Trade-offs we made

OptionWins whenLoses when
Many renditions per video (chosen) Smooth playback on any network and device Storage and compute multiply per rendition
Short segments Faster quality switches and startup More requests and manifest entries; slightly worse compression
Chunked parallel transcoding (chosen) Minutes instead of hours for long videos Orchestration, keyframe-aligned splitting
Expensive codecs only for popular videos (chosen) Compute goes where it saves the most bandwidth Two processing paths to maintain

Extensions

  • Live streaming: ingest a live feed, transcode in real time and publish segments seconds old; latency becomes the main requirement.
  • Digital rights management: encrypted segments with license servers per platform.
  • Per-title encoding: choose the bitrate ladder per video, since a cartoon needs fewer bits than a sports match.
  • Resume where you left off and offline downloads.

In the wild

Netflix

How Netflix works with ISPs around the globe to deliver a great viewing experience (opens in a new tab)

Netflix built its own CDN, Open Connect, starting in 2011, and the 2016 post says it delivers 100 percent of Netflix video traffic. Open Connect Appliances sit in close to 1,000 locations, both at internet exchange points and embedded inside internet providers' networks, where almost all content is then served locally. Files are copied from transcoding repositories to regional locations and on to the embedded servers during off-peak hours, and connections to Open Connect are free for providers because local delivery reduces the capacity they must buy to the rest of the internet.

Check yourself

  1. Why do uploads go directly to object storage with presigned URLs?

    Show answer

    B. API servers never carry the bytes, and failed parts can be retried alone Direct multipart uploads keep large transfers off application servers and make uploads resumable.

  2. What makes transcoding a long video fast?

    Show answer

    B. Splitting on keyframes and encoding chunks and renditions in parallel Independent chunk-rendition tasks spread over a worker pool finish in a fraction of the serial time.

  3. A viewer's bandwidth drops from 8 to 1.2 Mbit/s. What does an adaptive player do?

    Show answer

    B. Switches to a lower-bitrate rendition for the next segments Each segment is available at several bitrates, so the player picks one its bandwidth can sustain.

  4. Why can video segments be cached for a very long time?

    Show answer

    B. A segment URL's content never changes Immutable segments need no invalidation; only manifests and metadata change.

  5. Why are view counts batched through a stream processor?

    Show answer

    B. A popular video's single counter row would become a hot key under per-view writes Aggregating events first turns thousands of writes per second into a few.

Recap

  • Uploads go in resumable parts directly to object storage; API servers only hand out URLs.
  • Transcoding is a DAG of independent chunk-and-rendition tasks run in parallel from a queue.
  • HLS and DASH split video into segments at several bitrates; the player picks one per segment to keep its buffer full.
  • Bandwidth is the main cost: immutable segments are cached at CDN edges close to viewers, and predictable content is pre-positioned off-peak.
  • View counts flow through a stream processor in batches, never as one write per view.

Get new lessons by email

Get new posts on backend and AI internals. No spam, unsubscribe anytime.

Your email is only used to send new posts. Privacy

Next up