Chapter 32 of 33

Design a Video Streaming Service

YouTube, Netflix and JioHotstar let millions of people watch video at the same time, on phones with patchy 4G and on 4K smart TVs. When a video starts in a second and quietly drops from 1080p to 480p as your train enters a tunnel instead of freezing, that is not luck. It is the result of a carefully designed pipeline: upload, transcode, package, distribute through a CDN, and adapt the quality on the client.

In this chapter we design a video streaming service (think "YouTube-like": users upload, everyone watches). We will lean on Object Storage & File Storage, Content Delivery Networks (CDN), Message Queues & Pub/Sub and Back-of-the-Envelope Estimation. The new ideas are transcoding, segmented streaming (HLS and DASH) and adaptive bitrate streaming.

A video streaming service stores each uploaded video as many small, pre-encoded segments at several quality levels, delivers them over HTTP through CDNs, and lets the player switch between quality levels segment by segment based on the viewer's network.


Step 1: Clarify Requirements

Functional Requirements

  • Users can upload videos (large files, possibly several GB).

  • Users can watch videos on any device, with playback starting quickly.

  • Video quality adapts to the viewer's bandwidth.

  • Basic metadata: title, description, thumbnail, view count.

Out of scope: recommendations, comments, search, live streaming (briefly discussed at the end), monetisation.

Non-Functional Requirements

  • Smooth playback: fast start (about 1 to 2 seconds) and very few rebuffering stalls. This matters more than anything else to viewers.

  • High availability for playback; uploads can tolerate some processing delay (minutes is fine).

  • Massive egress bandwidth at a reasonable cost.

  • Durability: an uploaded video must never be lost.


Step 2: Back-of-the-Envelope Estimation

Assumptions
-----------
DAU                               : 200 million
Watch time per user per day       : 30 minutes
Average streaming bitrate         : 3 Mbps
New uploads per day               : 500,000 videos
Average video length              : 10 minutes
Original upload bitrate           : ~10 Mbps
Renditions (H.264 ladder, kbps)   : 145, 365, 730, 2000, 3000, 6000  (sum = 12,240 kbps)
Watching (egress)
  200M × 30 min × 60        = 3.6 × 10^11 watch-seconds/day
  ÷ 86,400                  ≈ 4.2 million concurrent viewers on average
  × 3 Mbps                  ≈ 12.5 Tbps average egress  (peak ×2 ≈ 25 Tbps)

Uploads (ingest)
  original: 500K × 600 s × 10 Mbps / 8   = 375 TB/day
  average ingest bandwidth              ≈ 35 Gbps

Storage of encoded renditions
  per video: 12,240 kbps × 600 s / 8     ≈ 918 MB  (all six rungs, one codec)
  per day  : 500K × 918 MB               ≈ 459 TB/day
  per year                               ≈ 168 PB  (before replication)

The lessons are striking:

  • Egress is the monster. 12.5 Tbps cannot come from one data centre. Video must be served from CDN edges close to viewers.

  • Storage grows by hundreds of terabytes per day. Only object storage (S3-style) with tiered storage classes is practical. Rarely watched old videos can move to cheaper, colder tiers.

  • The request rate for metadata is tiny by comparison; it is a normal web service problem.


Step 3: Key Concepts Before the Design

Why Not Just Serve the Original MP4?

A single 1080p file at 10 Mbps would fail most viewers on mobile networks, would not play on devices that lack its codec, and would force a full download before seeking far ahead. Instead we prepare the video in advance.

Transcoding and the Bitrate Ladder

Transcoding is decoding the uploaded video and re-encoding it into different codecs, resolutions and bitrates. The set of (resolution, bitrate) versions is called the bitrate ladder, and each version is a rendition.

The ladder used in our estimate is close to the H.264 starting points in Apple's HLS Authoring Specification (which itself says these are just one possible starting set; real services tune ladders per title or even per scene).

Codec

Compression vs H.264

Notes

H.264 / AVC

Baseline

Plays almost everywhere; the safe default rendition

H.265 / HEVC

Roughly similar quality at noticeably lower bitrate

Common on TVs and Apple devices; patent licensing is complex

VP9

Similar class to HEVC

Royalty-free, from Google; widely used on the web and Android

AV1

Better still than HEVC/VP9

Royalty-free (Alliance for Open Media); encoding is slow and expensive, so it pays off on popular videos

Exact savings depend on content and encoder settings, so in interviews describe them qualitatively. A useful design idea: encode every video in H.264 for compatibility, and add newer codecs only for videos that get enough views to repay the extra compute.

Segmented Streaming: HLS and DASH

Each rendition is cut into small segments (typically a few seconds each; Apple's HLS spec recommends a 6-second target duration). A text manifest (playlist) lists the renditions and their segments. The player downloads the manifest, then fetches segments one by one with ordinary HTTP GET requests.

Protocol

Standard

Manifest

Where it dominates

HLS (HTTP Live Streaming)

Apple; published as RFC 8216

.m3u8 text playlists

Apple devices, very widely supported elsewhere

MPEG-DASH

ISO/IEC 23009-1

.mpd XML file

Android, smart TVs, web players

Today both can carry the same fragmented MP4 (CMAF) segments, so a service can encode once and generate both an HLS playlist and a DASH manifest pointing to the same files. That halves storage compared with keeping separate MPEG-TS files for HLS.

Why is this design so powerful? Because segments are plain static files over HTTP. Every CDN, proxy and browser cache in the world already knows how to cache them. No special streaming servers are needed.

Adaptive Bitrate Streaming (ABR)

Since every rendition is cut at the same time points, the player can switch rendition at any segment boundary. Before each segment it asks: "Given my measured throughput and how many seconds of video I have buffered, which rung can I safely download?" This logic runs entirely in the client; the server just serves files.

  • Throughput-based: pick the highest bitrate below a safety fraction (say 80%) of measured download speed.

  • Buffer-based: if the buffer is large, you can afford to be bold; if it is nearly empty, choose lower quality to avoid a stall.

  • Real players (hls.js, dash.js, ExoPlayer, AVPlayer) blend both, with smoothing to avoid flickering between rungs.


Step 4: ABR in Code

The program below prints an HLS master playlist for our ladder, then simulates a player choosing a rendition for each 6-second segment as network speed changes. It uses a throughput rule with an 80% safety margin, plus a buffer rule (drop one rung while the buffer is under 10 seconds).

# Bitrate ladder: (name, width, height, bitrate in kbps)
LADDER = [("234p", 416, 234, 145), ("360p", 640, 360, 365),
          ("432p", 768, 432, 730), ("540p", 960, 540, 2000),
          ("720p", 1280, 720, 3000), ("1080p", 1920, 1080, 6000)]
SEGMENT_SECONDS = 6

def master_playlist():
    lines = ["#EXTM3U"]
    for name, w, h, kbps in LADDER:
        lines.append(f"#EXT-X-STREAM-INF:BANDWIDTH={kbps * 1000},RESOLUTION={w}x{h}")
        lines.append(f"{name}/index.m3u8")
    return "\n".join(lines)

def choose(throughput_kbps, buffer_s, safety=0.8, low_buffer=10):
    """Pick the highest rung whose bitrate fits in safety x measured throughput.
    If the buffer is nearly empty, be extra careful and drop one more rung."""
    budget = throughput_kbps * safety
    idx = 0
    for i, (_, _, _, kbps) in enumerate(LADDER):
        if kbps <= budget:
            idx = i
    if buffer_s < low_buffer and idx > 0:
        idx -= 1
    return idx

print(master_playlist())
print()
buffer = 0.0
# measured network throughput (kbps) while downloading each 6-second segment
network = [1500, 4000, 9000, 9000, 9000, 2500, 800, 800, 5000]
print("seg  net(kbps)  chosen  seg-size(KB)  download(s)  buffer(s)")
for seg, net in enumerate(network, 1):
    i = choose(net, buffer)
    name, _, _, kbps = LADDER[i]
    size_kb = kbps * SEGMENT_SECONDS / 8          # kilobits -> kilobytes
    dl = kbps * SEGMENT_SECONDS / net             # seconds to download
    buffer = max(0.0, buffer - dl) + SEGMENT_SECONDS
    print(f"{seg:>3}  {net:>9}  {name:>6}  {size_kb:>12.0f}  {dl:>11.2f}  {buffer:>9.2f}")

Output:

#EXTM3U
#EXT-X-STREAM-INF:BANDWIDTH=145000,RESOLUTION=416x234
234p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=365000,RESOLUTION=640x360
360p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=730000,RESOLUTION=768x432
432p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=2000000,RESOLUTION=960x540
540p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=3000000,RESOLUTION=1280x720
720p/index.m3u8
#EXT-X-STREAM-INF:BANDWIDTH=6000000,RESOLUTION=1920x1080
1080p/index.m3u8

seg  net(kbps)  chosen  seg-size(KB)  download(s)  buffer(s)
  1       1500    360p           274         1.46       6.00
  2       4000    540p          1500         3.00       9.00
  3       9000    720p          2250         2.00      13.00
  4       9000   1080p          4500         4.00      15.00
  5       9000   1080p          4500         4.00      17.00
  6       2500    540p          1500         4.80      18.20
  7        800    360p           274         2.74      21.46
  8        800    360p           274         2.74      24.72
  9       5000    720p          2250         3.60      27.12

Reading the simulation:

  • Segment 1: 80% of 1,500 kbps is 1,200 kbps, so 432p (730 kbps) fits, but the buffer is empty, so the player plays safe with 360p. Small first segments are also why playback starts fast.

  • Segments 3 to 5: the network improves and the buffer grows, so quality climbs to 1080p.

  • Segments 6 to 8: throughput collapses to 800 kbps. The player drops to 360p, and because each download takes less than 6 seconds, the buffer keeps growing: no stall. Dropping quality is how ABR trades sharpness for smoothness.

Note: in a real master playlist, BANDWIDTH is the peak segment bitrate (RFC 8216), an optional AVERAGE-BANDWIDTH gives the average, and each variant should also declare its CODECS. We kept the playlist minimal to focus on the idea.


Step 5: API Design

POST /v1/videos                     body: { "title": "...", "size_bytes": 2147483648 }
                                    → 201 { "video_id": "v123", "upload": { ...pre-signed
                                            multipart upload URLs... } }
PUT  <pre-signed URL for part N>    (client uploads parts directly to object storage)
POST /v1/videos/v123/complete       → 202 { "status": "PROCESSING" }

GET  /v1/videos/v123                → 200 { "title": "...", "status": "READY",
                                            "manifest_url": "https://cdn.../v123/master.m3u8",
                                            "thumbnail_url": "...", "views": 1520 }
GET  https://cdn.../v123/720p/seg_00042.m4s      (segments: plain HTTP GETs from the CDN)

Two important choices:

  • Direct-to-storage, resumable uploads. Gigabyte files must not stream through our API servers, and a dropped connection must not restart the upload. Object stores support multipart upload (in Amazon S3, parts are at least 5 MiB except the last, with up to 10,000 parts per upload); the client uploads parts in parallel and retries only failed parts.

  • Asynchronous processing. The complete call returns 202 Accepted. The video becomes watchable later, when status changes to READY.


Step 6: High-Level Architecture

UPLOAD PATH
 Creator ──(1) create video──▶ API / Video service ──▶ Metadata DB (status=UPLOADING)
    │
    └──(2) multipart upload──▶ Object storage: raw/ bucket
                                    │ (3) "upload complete" event
                                    ▼
                               ┌─────────┐      ┌──────────────────────────────────┐
                               │  Queue  │ ───▶ │ Transcoding pipeline (workers)   │
                               └─────────┘      │  split → encode rungs in parallel│
                                                │  → package HLS/DASH (CMAF)       │
                                                │  → thumbnails, DRM, checks       │
                                                └───────────────┬──────────────────┘
                                                                ▼
                                       Object storage: processed/ bucket (segments,
                                       manifests)   +  Metadata DB (status=READY)

WATCH PATH
 Viewer ──▶ API: GET /videos/v123 ──▶ metadata (from cache) + manifest URL (signed)
    │
    └──▶ CDN edge (cache hit: serve segment)
               │ miss
               ▼
          Regional / shield cache ──miss──▶ Origin = object storage processed/

The Transcoding Pipeline

Encoding a 10-minute video into six renditions on one machine could take longer than the video itself. The trick is parallelism:

  1. Validate and inspect the upload (is it really a video? duration, resolution, corrupt frames?).

  2. Split the source into chunks at keyframes (for example, 30 to 60 seconds each).

  3. Encode every (chunk, rendition) pair as an independent task. A 10-minute video with 20 chunks and 6 rungs becomes 120 tasks spread across a worker fleet.

  4. Assemble and package the encoded chunks into segments and write HLS and DASH manifests.

  5. Extras: thumbnails, captions, content moderation, and DRM encryption (Widevine, FairPlay, PlayReady) for premium content.

  6. Mark the video READY. Publish the lower rungs first if you want the video watchable sooner, and add the high rungs as they finish.

This is a directed acyclic graph (DAG) of tasks. Workers pull tasks from queues, so the system absorbs upload spikes by letting the queue grow, and retries a failed task without redoing the whole video. Every task writes its output to object storage under a deterministic name, so retrying is idempotent.

Storage Layout

raw/v123/original.mp4                     (kept for re-encoding with future codecs)
processed/v123/master.m3u8
processed/v123/manifest.mpd
processed/v123/720p/index.m3u8
processed/v123/720p/init.mp4
processed/v123/720p/seg_00001.m4s ... seg_00100.m4s
processed/v123/thumbs/t_0001.jpg

Metadata (title, owner, status, durations, available renditions, view counts) goes into a regular database, sharded by video_id, with a cache in front because popular videos' metadata is read millions of times.


Step 7: Deep Dives

Video popularity is extremely skewed: a small fraction of videos gets most of the views. Those are almost always in the CDN edge caches, so the origin rarely sees them. The long tail (millions of rarely watched videos) causes cache misses. Common techniques:

  • Tiered caching / origin shield: edges miss to a regional cache instead of the origin, so one fetch fills many edges.

  • Proactive placement: push predicted-popular content (a new episode of a big show) to edges during off-peak hours. Netflix's Open Connect does this with its own cache appliances, many of them installed inside ISP networks, filled overnight.

  • Signed URLs or tokens on manifests and segments so only authorised viewers can fetch premium content.

Counting Views

Never do UPDATE videos SET views = views + 1 on every play; a viral video would create a hot row. Instead the player sends play events to a queue or log, a stream processor aggregates counts per video per minute, and the result is written in batches. The view count you see is eventually consistent, which is fine.

Fast Start and Seeking

  • Start at a low or medium rung, then climb (as our simulation did).

  • Pre-fetch the manifest and the first segment when the user hovers or the video scrolls into view.

  • Seeking to minute 7 is easy: the manifest tells the player exactly which segment covers that time, and it fetches only that segment.

A Note on Live Streaming

Live video uses the same building blocks with tighter timing: the encoder produces segments in real time and the playlist is updated continually. Ordinary HLS/DASH live streams often run 10 to 30 seconds behind real time because players buffer several segments. Low-Latency HLS (which publishes small partial segments as soon as they are encoded) and low-latency DASH (which streams each segment in small CMAF chunks while it is still being produced) cut this to a few seconds; real-time protocols such as WebRTC go below one second at higher cost. For a live cricket match with tens of millions of viewers, CDN scale matters more than the last second of latency.


Comparison: Design Choices at a Glance

Decision

Choice

Why

Video bytes

Object storage + CDN

Petabytes of immutable files; Tbps of egress

Delivery protocol

HLS and DASH over HTTP, CMAF segments

Cacheable static files, device coverage

Quality control

Client-side ABR

Only the client knows its current network

Processing

Async DAG on a worker fleet via queues

Parallel, retryable, absorbs spikes

Uploads

Pre-signed multipart upload direct to storage

Resumable, keeps large bytes off API servers

Metadata

Sharded DB + cache

Small, read-heavy, needs queries

View counts

Event stream + batch aggregation

Avoids hot-row writes


Common Mistakes

  • Serving video from application servers. Egress is measured in terabits per second; it belongs on the CDN.

  • Streaming one big file. Without segments and a ladder there is no adaptation and no efficient caching or seeking.

  • Putting ABR on the server. The client sees its own throughput and buffer; the server only serves files.

  • Synchronous transcoding. Upload requests must return quickly; processing is asynchronous and parallel.

  • Deleting the original. Keep it (in a cold tier) so you can re-encode when better codecs arrive.

  • Forgetting bits vs bytes. 3 Mbps is 375 KB/s. Mixing them makes bandwidth estimates 8 times wrong.


Interview Questions

1. What is adaptive bitrate streaming and why is it needed?

The video is encoded at several bitrates/resolutions and cut into aligned segments. The player measures its throughput and buffer level and chooses a rendition for each next segment. It is needed because viewers' bandwidth varies widely and changes during playback; ABR avoids stalls by lowering quality instead of freezing.

2. Why are HLS and DASH built on plain HTTP instead of a custom streaming protocol?

Segments and manifests are ordinary static files, so existing CDNs, HTTP caches and load balancers handle them with no special servers. HTTP also passes through firewalls easily and scales horizontally like any web content.

3. How would you transcode a 2-hour 4K movie quickly?

Split it at keyframes into many chunks and encode each (chunk, rendition) pair in parallel on a worker fleet, coordinated by a DAG scheduler and queues. Then concatenate and package. Total time is roughly the time of the slowest task plus overhead, not the length of the movie.

4. 2 million people watch concurrently at an average of 4 Mbps. What is the egress, and how do you serve it?

2 × 106 × 4 Mbps = 8 × 106 Mbps = 8 Tbps. That has to come from a CDN with many edge locations (and ideally caches inside ISPs), with tiered caching to protect the origin.

5. Why use CMAF segments?

CMAF (fragmented MP4) segments can be referenced by both HLS playlists and DASH manifests, so the service encodes and stores each rendition once instead of keeping separate formats, roughly halving storage and improving CDN cache efficiency.

6. How do you count views for a video that goes viral?

Send play events to a log or queue, aggregate them in a stream processor (per video per time window), and write totals in batches. Avoid incrementing a single database row per view, which creates a write hot spot. Display counts are eventually consistent.


Summary

  • Video streaming is dominated by egress bandwidth (terabits per second) and storage (hundreds of TB per day), so object storage + CDN is mandatory.

  • Uploads go directly to object storage using resumable multipart uploads; processing is asynchronous.

  • A parallel transcoding DAG produces a bitrate ladder of renditions, cut into segments of a few seconds.

  • HLS (RFC 8216) and MPEG-DASH deliver segments over plain HTTP; CMAF lets both share one set of files.

  • ABR runs in the player, using throughput and buffer level to pick a rendition per segment.

  • Popular content stays hot in edge caches; the long tail is handled with tiered caching and proactive placement.

Prepare everything in advance, deliver it as small cacheable files, and let the player decide quality one segment at a time.