Adaptive bitrate streaming
HLS and DASH: the player picks the quality the network can carry.
01The intuition
The problem it solves
Asha starts a video on the train. For the first minute the signal is strong, then the train enters a cutting and her bandwidth drops from 8 Mbps to 1 Mbps for two minutes, then recovers. If the player had chosen one quality at the start, the choice is wrong for part of the journey either way: pick 1080p at 5 Mbps and the video freezes to buffer in the cutting; pick 360p at 0.8 Mbps and she watches a blurry picture for the whole ride even though her phone could have shown it sharp.
Downloading the whole file first is no answer either: a 90-minute film at 5 Mbps is 3.4 GB, and nobody waits for that. The video must be fetched a little at a time, and the quality must be allowed to change in the middle.
Build it one piece at a time
Step 1: cut every rendition into segments. The transcoding pipeline has already produced 360p, 720p and 1080p versions. Cut each into segments of, say, 4 seconds, all aligned on the same keyframes, so segment 17 covers seconds 68 to 72 in every rendition.
Step 2: write a manifest. A small text file lists the renditions and the URL of every segment. In HLS it is an .m3u8 playlist; in DASH it is an MPD. The player downloads the manifest first; it is a few kilobytes.
| Segment | Seconds | 360p URL | 720p URL | 1080p URL |
|---|---|---|---|---|
| 1 | 0–4 | /v/abc/360/1.ts | /v/abc/720/1.ts | /v/abc/1080/1.ts |
| 2 | 4–8 | /v/abc/360/2.ts | /v/abc/720/2.ts | /v/abc/1080/2.ts |
| … |
Step 3: the player measures and chooses. It fetches segment 1 at a safe low quality, times how long the bytes took, and estimates its bandwidth. If 1 MB arrived in 0.5 s, throughput is 16 Mbps, so it asks for segment 2 at 1080p. The server does nothing clever; the intelligence is in the client.
Step 4: keep a buffer and switch calmly. The player tries to stay a few segments ahead, for example 20 seconds of video in memory. When the measured bandwidth falls, it picks a lower rendition for the next segment before the buffer runs dry; when it rises, it steps up one level at a time rather than jumping, so the picture does not flicker between qualities.
Step 5: serve segments as plain files. Because every segment is an ordinary file with a fixed URL, a CDN caches them at the edge. The first viewer in a city pulls each segment from the origin; the next thousand get it from the nearby cache.
Why it works, with the numbers
The player's rule is a safety margin: choose the highest rendition whose bitrate is comfortably below the measured bandwidth.
choose the largest brendition such that brendition ≤ 0.8 × measured bandwidth
With a 20 s buffer and 4 s segments the player has five segments of slack, so bandwidth can collapse for 20 seconds before anyone sees a spinner, as long as it switches down in time. The guarantee is "playback continues at the best quality the last few seconds of network allowed". The costs: each rendition is stored and cached separately, and a switch is only possible at a segment boundary, so shorter segments adapt faster but create more files and more requests (a 90-minute film at 4 s segments is 1,350 segments per rendition). The failure modes: an optimistic estimate after one fast segment causes a stall, which is why players smooth the estimate; and live streams add latency of at least one segment, so live sport uses 1 to 2 s segments.
The trade-off you must say out loud
Short segments give fast adaptation and low live latency but more overhead; long segments compress better and cost fewer requests. Say the segment length and buffer size as numbers. The interviewer's follow-up is how a CDN serves a live stream where the newest segment did not exist a second ago, which is a manifest that is refreshed every few seconds and a short cache TTL on it.
Where it shows up in the 100 Google problems
- Design YouTube: the rendition ladder, 4 s segments, a manifest per video and a player that measures and switches.
- Design a video distribution CDN: segments are the cacheable unit; popular segments stay at the edge and origin traffic drops to a small fraction.
- Design TikTok uses the same ladder with the first segment prefetched for the next video so the swipe feels instant.