Adaptive bitrate for system design interviews: why your video starts blurry, then turns sharp

2026-12-05来源 dev.toEnglish
Adaptive bitrate for system design interviews: why your video starts blurry, then turns sharp

You're on the metro, watching the last over of a match. The video opens blurry, sharpens a second later, and when the train goes into a tunnel the picture gets soft again instead of freezing.

None of that is an accident. It's one of the most practical ideas in system design, and it comes up whenever an interviewer says "design YouTube" or "design a streaming service": adaptive bitrate streaming.

The obvious design, and how it breaks

The first answer most people give is simple: store the video as one 1080p file and stream it to the phone.

In our bitrate ladder (below), 1080p needs 6 Mbps. That's fine on home Wi-Fi. On a train, the connection is anything but steady.

In our simulation on Designbench, the phone's bandwidth over two minutes goes like this: around 7 to 11 Mbps at first, then a tunnel where it collapses to about 1 Mbps for half a minute, then a recovery to about 5 to 7 Mbps.

A player that always downloads 1080p does this:

Always 1080p Result
Time to first frame 2.8 s
Freezes in 2 minutes 4
Total time frozen 9.4 s

Always 1080p in a metro tunnel: four freezes, 9.4 seconds frozen

It looks perfect on good signal and falls apart the moment the signal drops. A buffered video protects you for a while, but a long tunnel drains any buffer you can reasonably hold.

The fix: cut the video into pieces, in several qualities

Adaptive bitrate (ABR) changes two things.

1. The video is cut into short segments. Our lesson uses 4-second pieces. Each piece is a small file the phone can fetch on its own over plain HTTP, which also means a CDN can cache it like any other file.

2. Every segment is encoded several times, at different qualities. This set of versions is called the bitrate ladder. In our simulation it has five rungs:

Rung Bitrate
240p 0.4 Mbps
360p 0.8 Mbps
480p 1.5 Mbps
720p 3 Mbps
1080p 6 Mbps

Each 4-second piece exists in five qualities

A small text file, the manifest (HLS and DASH are the two common formats), lists the rungs and where each segment lives. The player reads the manifest, then chooses a rung for every single segment.

That's the whole trick: the decision moves from the server to the player, and it's made again every 4 seconds.

Why it starts blurry

A new player knows nothing about your connection, so it starts at a low rung. A 240p segment is tiny, so it arrives almost immediately and the video starts. As segments keep arriving quickly, the player climbs the ladder.

In our simulation, the buffer-based player showed the first frame in 0.18 s, compared with 2.8 s for always-1080p. That brief blur at the start is the price of starting fast, and most viewers prefer it to staring at a spinner.

Two ways to pick the next rung

Throughput-based (watch the speed). Measure how fast recent segments downloaded, then pick the best rung that fits under that speed with a safety margin. Our lesson's version uses the harmonic mean of the last 3 segments and picks the best quality under 80% of it.

Buffer-based (watch the saved-up video). Look at how many seconds of video are already buffered. Plenty saved up? You can afford a higher rung. Running low? Drop down before you freeze.

Here's a throughput-based picker in Java, the same idea as the lesson's:

import java.util.List; final class ThroughputPicker { // Bitrates in Mbps, lowest rung first: 240p, 360p, 480p, 720p, 1080p private static final double[] LADDER = {0.4, 0.8, 1.5, 3.0, 6.0}; private static final double SAFETY = 0.8; /** Returns the rung index for the next segment. */ static int pick(List<Double> recentMbps) { if (recentMbps.isEmpty()) return 0; // know nothing yet: start low int n = Math.min(3, recentMbps.size()); double sumInverse = 0; for (int i = recentMbps.size() - n; i < recentMbps.size(); i++) { sumInverse += 1.0 / recentMbps.get(i); } double estimate = n / sumInverse; // harmonic mean: one slow sample pulls it down int best = 0; for (int i = 0; i < LADDER.length; i++) { if (LADDER[i] <= estimate * SAFETY) best = i; } return best; } } 

The harmonic mean is a deliberate choice: one slow segment pulls the estimate down much more than an arithmetic mean would, which is the safe direction when the alternative is a freeze.

In our simulation, both adaptive players got through the tunnel with zero freezes. They differ in how they get there:

Player Freezes First frame Average bitrate Quality switches Biggest jump
Always 1080p 4 (9.4 s) 2.8 s 6 Mbps 0 0
Throughput-based 0 0.4 s 3.7 Mbps 6 3 rungs
Buffer-based 0 0.18 s 3.8 Mbps 12 1 rung

The adaptive player steps down through the tunnel and back up: zero freezes

The catch: switching too eagerly

Adapting has its own failure mode. If the player reacts to every wobble in the signal, the picture keeps flickering between sharp and blurry, and that's almost as annoying as a freeze.

Look at the last two columns. The buffer-based player switched twice as often, but only one rung at a time, so each change is gentle. The throughput-based player switched less, but jumped three rungs at once when the tunnel hit, which viewers notice. Real players tune this trade-off, and many combine both signals.

Things candidates get wrong

  • Streaming the original file. Without segments and a ladder, there's nothing to adapt.
  • Putting the decision on the server. The phone is the only one that knows its own connection and buffer, so the player decides.
  • Forgetting the cost. Five renditions means five encodes, and the whole ladder stores about twice the bytes of the 1080p version alone (0.4 + 0.8 + 1.5 + 3 + 6 = 11.7 Mbps vs 6). Rarely watched videos may not deserve every rung.
  • Ignoring the CDN. Segments are ordinary files, so they can be cached at the edge. Video bandwidth is the biggest bill in this design, and the CDN is how you pay less of it.

Interview follow-ups

Why 4-second segments and not 1 or 20? Shorter segments let the player react faster but add more requests and per-file overhead. Longer ones are efficient but slow to adapt. A few seconds is the usual middle ground.

How does a live stream differ? The manifest keeps updating as new segments are produced, and the player stays close to the live edge, so it holds less buffer and has less room for error.

Where does the ladder come from? A transcoding pipeline: the upload is split into chunks, each chunk is encoded at each rung in parallel, then the outputs are assembled. That's the other half of the video-streaming design.

Try it

Press play on all three players and watch the tunnel hit: designbench.dev/learn/video-streaming.

The 60-second version is on YouTube: @designbenchdev.

原文:https://www.a8q9.cn/en/article/adaptive-bitrate-for-system-design-interviews-why-your-video-starts-blurry-then--1b4f59.html

相关文章