AI Hints
Home›System Design Tutorial›Case Study: Video and Crawling
System Design · Lesson 104 of 120

Video Streaming: Playback

Adaptive bitrate.

⏱ 2 min read▶ Video at 2:06:29🎓 Free
Watch this lesson in the System Design courseWatch on YouTube ↗Full System Design course

A viewer on a train has a connection that goes fast, then slow, then fast again. The video should keep playing, even if the picture gets a bit softer for a moment. That's adaptive bitrate streaming.

Video Streaming: Playback
From the video: Video Streaming: Playback
  • A connection that changes
  • Keep playing
  • Adaptive bitrate

Choosing the next chunk

The player downloads one chunk at a time. Before each one, it measures the recent speed, and picks the best quality that fits, with some safety margin. Run it. When the connection drops, the player switches to a lower quality, and when it recovers, it climbs back up. The video never stops.

abr.pypython
QUALITIES = [(240, 0.4), (480, 1.0), (720, 2.5), (1080, 5.0)]  # (p, Mbps)

def pick(speed_mbps):
    best = QUALITIES[0][0]
    for p, needed in QUALITIES:
        if needed <= speed_mbps * 0.8:   # keep a safety margin
            best = p
    return best

for chunk, speed in enumerate([8, 6, 1.5, 0.7, 3.5, 9], 1):
    print(f"chunk {chunk}: speed {speed} Mbps -> {pick(speed)}p")
Output
chunk 1: speed 8 Mbps -> 1080p
chunk 2: speed 6 Mbps -> 720p
chunk 3: speed 1.5 Mbps -> 480p
chunk 4: speed 0.7 Mbps -> 240p
chunk 5: speed 3.5 Mbps -> 720p
chunk 6: speed 9 Mbps -> 1080p

Serving at scale

Almost all video traffic is served by CDNs, from servers close to viewers, often inside internet providers' own networks. The player fetches a small playlist file, listing all the chunks and qualities. Popular videos stay cached at the edge. And rarely watched videos are fetched from the origin only when needed.

keyvalue
CDNserves almost everything
Playlistlists chunks + qualities
Popular videoscached at the edge
Long tailfrom origin on demand

Summary

Adaptive bitrate picks each chunk's quality from the current speed, so playback never stops, and CDNs serve it all. Next, a web crawler.

Key takeaways
  • Measure speed
  • Pick quality per chunk
  • CDN delivers
Next up: Web Crawler — fetching the web