TExcelTAD
Home › System Design › Design Video Streaming

Design Video Streaming

Let's design a service like YouTube or Netflix: creators upload videos, the system processes them, and viewers around the world watch them smoothly on phones, laptops and TVs, over networks that range from fibre to a crowded train. Two very different pipelines are involved: a heavy, asynchronous upload and transcoding pipeline, and a latency-sensitive playback path served almost entirely by CDNs.

1. Requirements

Functional

  • Upload videos (up to several GB); watch them on any device; search and recommendations (out of scope here).
  • Playback adapts to the viewer's bandwidth.

Non-functional

  • Playback starts within ~2 s and rarely stalls (rebuffers).
  • Uploads may take minutes to process, but must never be lost.
  • 50 million daily viewers, 500,000 uploads per day.

2. Estimates

python · try it yourself
daily_viewers = 50_000_000
minutes_watched = 40
avg_bitrate_mbps = 3                    # a mix of 480p to 1080p
uploads_per_day = 500_000
avg_upload_minutes = 8
stored_mb_per_minute_all_renditions = 120   # sum over every resolution in the ladder

watch_seconds = daily_viewers * minutes_watched * 60
avg_egress_tbps = watch_seconds * avg_bitrate_mbps / 86_400 / 1e6
print(f"average egress: {avg_egress_tbps:.1f} Tbit/s (peak ~2-3x)")

new_storage_tb = uploads_per_day * avg_upload_minutes * stored_mb_per_minute_all_renditions / 1e6
print(f"new storage per day: {new_storage_tb:,.0f} TB, per year: {new_storage_tb * 365 / 1000:,.0f} PB")
print(f"upload ingest: {uploads_per_day * avg_upload_minutes / 86_400:,.0f} minutes of video per second")
average egress: 4.2 Tbit/s (peak ~2-3x)
new storage per day: 480 TB, per year: 175 PB
upload ingest: 46 minutes of video per second

Several terabits per second of egress: no origin can serve that. Video streaming is a CDN problem first, and an encoding problem second.

3. Upload and processing pipeline

text
creator --(resumable multipart upload, pre-signed URLs)--> [ object store: originals ]
                                                                |  "uploaded" event
                                                                v
                                                   [ queue ] -> [ split into 4 s chunks ]
                                                                |  one task per chunk x rendition
                                                                v
                                                  [ transcoding workers (GPU/CPU fleet) ]
                                                                |
                    [ object store: segments + manifests ] <----+--> thumbnails, captions
                                       |
                                    [ CDN ]  <---- viewers

Splitting the video into short chunks lets hundreds of workers transcode one video in parallel, and a failed chunk is simply retried. Each chunk is encoded into a bitrate ladder:

python · try it yourself
LADDER = [  # name, height, video bitrate (kbit/s)
    ("240p", 240, 400), ("360p", 360, 800), ("480p", 480, 1_400),
    ("720p", 720, 2_800), ("1080p", 1080, 5_000), ("2160p", 2160, 16_000),
]

def renditions_for(source_height):
    return [r for r in LADDER if r[1] <= source_height]

def plan(duration_s, source_height, chunk_s=4):
    chunks = -(-duration_s // chunk_s)
    rungs = renditions_for(source_height)
    tasks = chunks * len(rungs)
    total_mb = sum(kbps for _, _, kbps in rungs) * duration_s / 8 / 1000
    return chunks, rungs, tasks, total_mb

chunks, rungs, tasks, mb = plan(duration_s=600, source_height=1080)
print("renditions:", [name for name, _, _ in rungs])
print(f"{chunks} chunks x {len(rungs)} renditions = {tasks} transcoding tasks")
print(f"stored size for a 10-minute video: {mb:,.0f} MB")
renditions: ['240p', '360p', '480p', '720p', '1080p']
150 chunks x 5 renditions = 750 transcoding tasks
stored size for a 10-minute video: 780 MB

A 1080p upload doesn't get a 4K rendition (no upscaling). Very popular videos can later be re-encoded with slower, more efficient codecs (AV1) to save CDN bandwidth, while rarely watched ones keep only a few renditions.

4. Adaptive bitrate (ABR) playback

The player first downloads a manifest (HLS .m3u8 or DASH .mpd) listing the renditions and their segment URLs. It then fetches segments one by one, and before each segment picks a rendition based on the measured throughput and how much video is already buffered. Here's a simple throughput-and-buffer rule on a seeded, fluctuating network:

python · try it yourself
import random

LADDER_KBPS = [400, 800, 1_400, 2_800, 5_000]
SEGMENT_S = 4

def choose(throughput_kbps, buffer_s):
    safety = 0.6 if buffer_s < 8 else 0.85        # be careful when the buffer is low
    usable = throughput_kbps * safety
    fitting = [b for b in LADDER_KBPS if b <= usable]
    return fitting[-1] if fitting else LADDER_KBPS[0]

rng = random.Random(42)
bandwidth = [3_500] * 5 + [900] * 5 + [6_000] * 5        # the train enters a tunnel, then leaves
bandwidth = [max(300, int(b * rng.uniform(0.8, 1.2))) for b in bandwidth]

buffer_s, estimate, stalls = 0.0, 1_000.0, 0.0
for i, bw in enumerate(bandwidth):
    bitrate = choose(estimate, buffer_s)
    download_s = bitrate * SEGMENT_S / bw
    if download_s > buffer_s:                        # buffer runs dry before the segment arrives
        stalls += download_s - buffer_s
        buffer_s = 0.0
    else:
        buffer_s -= download_s
    buffer_s += SEGMENT_S
    estimate = 0.7 * estimate + 0.3 * bw             # smoothed throughput estimate
    print(f"seg {i:2}: network {bw:5} kbps  chose {bitrate:5} kbps  buffer {buffer_s:5.1f} s")
print(f"total stall time: {stalls:.1f} s")
seg  0: network  3695 kbps  chose   400 kbps  buffer   4.0 s
seg  1: network  2835 kbps  chose   800 kbps  buffer   6.9 s
seg  2: network  3185 kbps  chose   800 kbps  buffer   9.9 s
seg  3: network  3112 kbps  chose  1400 kbps  buffer  12.1 s
seg  4: network  3831 kbps  chose  1400 kbps  buffer  14.6 s
seg  5: network   963 kbps  chose  1400 kbps  buffer  12.8 s
seg  6: network  1041 kbps  chose  1400 kbps  buffer  11.4 s
seg  7: network   751 kbps  chose  1400 kbps  buffer   8.0 s
seg  8: network   871 kbps  chose   800 kbps  buffer   8.3 s
seg  9: network   730 kbps  chose   800 kbps  buffer   7.9 s
seg 10: network  5324 kbps  chose   400 kbps  buffer  11.6 s
seg 11: network  6012 kbps  chose  1400 kbps  buffer  14.7 s
seg 12: network  4863 kbps  chose  2800 kbps  buffer  16.4 s
seg 13: network  5277 kbps  chose  2800 kbps  buffer  18.2 s
seg 14: network  6359 kbps  chose  2800 kbps  buffer  20.5 s
total stall time: 0.4 s

The player starts low (fast start), climbs as the buffer fills, steps down when the network collapses, and climbs again afterwards. The only "stall" is the 0.4 s start-up wait for the first segment: the buffer built up earlier carried playback through the tunnel. Note how it lags behind reality, staying at 1,400 kbps for a few segments after the drop and starting cautiously at 400 kbps after the recovery, because it reacts to a smoothed estimate. Production algorithms (BOLA, MPC, Netflix's buffer-based approaches) are more refined, but follow the same idea: trade quality for continuity, because viewers hate stalls far more than a temporarily blurry picture.

5. Serving at scale

  • Segments are static files: perfect for CDN caching. Popular videos are served almost entirely from edges.
  • Open Connect-style appliances: Netflix places its own cache servers inside ISPs and pre-fills them overnight with what each region is expected to watch.
  • Long tail: most videos are rarely watched; they're served from regional caches or origin, with only a few renditions kept hot.
  • Metadata (titles, view counts, watch history) lives in ordinary databases and caches; it's tiny compared with the video bytes.
python · try it yourself
from itertools import accumulate

# Popularity of 100,000 videos follows a power law; how much traffic do the top videos carry?
weights = [1 / (rank ** 1.0) for rank in range(1, 100_001)]
cumulative = list(accumulate(weights))
total = cumulative[-1]
for top in [100, 1_000, 10_000]:
    print(f"top {top:>6,} videos ({top / 1000:.1f}% of catalogue): {cumulative[top - 1] / total:5.1%} of views")
top    100 videos (0.1% of catalogue): 42.9% of views
top  1,000 videos (1.0% of catalogue): 61.9% of views
top 10,000 videos (10.0% of catalogue): 81.0% of views

Caching a small fraction of the catalogue at the edge serves the majority of views, which is what makes the egress numbers from the estimate affordable.

Trade-offs

Decision Choice Why
Upload Resumable, direct to object storage Large files, flaky networks, no app servers in the data path
Processing Chunked, parallel, queue-driven Fast turnaround, cheap retries, elastic workers
Formats HLS/DASH segments + bitrate ladder Works with CDNs, adapts to bandwidth
Delivery Multi-tier CDN, ISP caches Egress at Tbit/s scale
Encoding effort Extra-efficient codecs only for popular titles Encode cost pays off only with many views

Test yourself

Why are videos split into short chunks before transcoding?

  1. Chunks compress better
  2. Many workers can transcode one video in parallel and failed chunks are retried individually
  3. Browsers can't play long files
  4. To add advertisements

What does an adaptive bitrate player do when measured throughput drops?

  1. Stops playback
  2. Switches to a lower-bitrate rendition for the next segments
  3. Downloads the whole video first
  4. Increases the bitrate

Why is video streaming mainly a CDN problem?

  1. Videos are encrypted
  2. Egress is measured in terabits per second, far beyond what origin servers could serve
  3. CDNs transcode the videos
  4. Metadata is very large