← Building blocks

Stream video to any connection

Networking & deliverysenior

One video file either buffers on slow links or wastes bandwidth on fast ones.

Say it like this

Connections vary by the second, so I transcode into a ladder of renditions, cut each into short segments, and let the player pick the bitrate per segment from the manifest based on the bandwidth it measures.

Basic solution

Transcode into several renditions, cut into short segments, and let the player switch quality per segment (HLS / DASH).

HLSMPEG-DASHFFmpegCDN segment caching

One manifest, many renditions, per-segment choice

The player reads the master manifest, measures throughput on each segment and picks the next segment from whichever rendition it can sustain. Every segment is an immutable file served from the CDN edge.

How it works

  1. After upload, the transcoding pipeline produces a ladder of renditions, each a copy of the whole video at a different resolution and bitrate: for example 1080p at 5 Mbps, 720p at 2.5 Mbps, 480p at 1 Mbps and 360p at 0.5 Mbps, plus a separate audio track.
  2. Each rendition is cut into segments of 2 to 6 seconds. Every segment starts on a keyframe (a frame that can be decoded without any earlier frame), so a player can begin decoding at any segment boundary. Segments are written to object storage as immutable files.
  3. A master manifest is a small text file listing the renditions with their bitrates and resolutions. Each rendition also has a media playlist, another text file listing that rendition's segments in order with their durations.
  4. The player fetches the master manifest, picks a conservative rendition to start, downloads one segment and times it: bytes received divided by seconds taken gives a throughput estimate. Before each following segment it combines that estimate with how many seconds of video it already has buffered and chooses the highest rendition it expects to sustain.
  5. Because a switch only happens at a segment boundary, a drop in bandwidth produces one lower-quality segment rather than a stall. Every fetch is a plain HTTP GET of a static file, so a CDN caches each segment and serves it to the next viewer from the edge.

Why segments and not one file

  • Range requests on one file cannot switch quality, because every byte range belongs to one encoding. Segments make quality a decision taken every few seconds, and make every piece independently cacheable.

Worked example

Video streaming platform

  • Ladder for a 10-minute video: 5 + 2.5 + 1 + 0.5 Mbps video plus 0.128 Mbps audio. Total storage per minute across renditions: about 70 MB; for 500K uploads/day at 10 minutes that is 350 TB/day of renditions on top of originals.
  • Segments of 4 s: 150 segments per rendition, 600 video segments per video. At 1080p a segment is 2.5 MB; at 360p it is 250 KB.
  • A viewer on a link that measures 3 Mbps: the player picks 720p (2.5 Mbps) and keeps a 3-segment (12 s) buffer. When throughput drops to 1.2 Mbps on a train, the next segment comes from 480p: 0.5 MB at 1.2 Mbps takes about 3 s, and the 12 s buffer absorbs it.
  • Start-up: fetch the master manifest (2 KB), one media playlist (10 KB) and the first 360p segment (250 KB) from an edge 20 ms away. On a 3 Mbps link the first segment arrives in under a second, so playback starts at roughly 1 s and the next segments upgrade quality.
  • CDN economics: 50M viewers × 1 hour at an average 2.5 Mbps ≈ 55 PB/day; the top 20% of videos hit at 95%+ on the edge, so origin egress is about 5% of that.

Variants and when each wins

HLS (HTTP Live Streaming)

  • Apple's format: m3u8 playlists with fMP4 or TS segments. It wins as the default for video on demand and most live streams, because every Apple device plays it natively and every other platform supports it through players.

MPEG-DASH

  • The open standard: an XML manifest with fMP4 segments. It wins on browsers and smart TVs that prefer it, and many services publish both HLS and DASH manifests over the same CMAF segments; it is not native on iOS Safari.

Low-latency HLS / DASH

  • Segments are published in partial chunks and transferred as they are encoded, which cuts live latency from 15 to 30 seconds down to 2 to 5 seconds. It wins for live events where delay matters, at the cost of more requests per viewer per second and tighter CDN requirements.

Per-title or per-scene encoding

  • The ladder is tuned for each video after an analysis pass, so a cartoon gets far fewer bits at 1080p than a sports match. It wins once the catalogue is large, saving 20 to 40 percent of bytes delivered.

Recommendation: CMAF segments with both HLS and DASH manifests, 4 s segments for VOD, per-title encoding once the catalogue is large enough for the savings to matter.

Failure modes

Rebuffering on bandwidth drops

  • The spinner appears every time the network dips. Segments are so long, or the buffer target so small, that one slow segment drains the buffer before the next one arrives. Use 2 to 4 second segments, raise the buffer target on unstable networks, and make the player switch up only when there is clear headroom.

Quality oscillation

  • The picture flips between sharp and blurry every few seconds. The bandwidth estimator reacts to every single throughput sample, so a momentary dip or spike changes the rendition each time. Smooth the estimate over several segments, require headroom before switching up, and use buffer level alongside throughput when deciding.

Segments not aligned across renditions

  • Audio glitches or decoder errors appear at the moment of a quality switch. The encoders placed keyframes at different timestamps in different renditions, so segment N of 720p does not start at the same instant as segment N of 480p. Force keyframes at fixed intervals in every encode so that every rendition's segments line up exactly.

Manifest cached too long

  • A live stream freezes on the same segment while the encoder keeps producing new ones. The media playlist, which grows as new segments appear, was cached by the CDN or browser for minutes, so the player never sees the new entries. Give playlists a cache lifetime of a few seconds and segments a long, immutable one.

Slow start because the first rendition is too high

  • Viewers stare at a blank screen for 5 seconds before playback begins. The player starts at 1080p on a link it has not measured yet, so the first 2.5 MB segment takes seconds to arrive. Start on the lowest rendition and step up after the first measured segment, and for premieres push the first segments of every rendition to the edge ahead of time.

Interviewer follow-ups

Answer out loud before you open each one.

How does the player decide which bitrate to request next?

It estimates available bandwidth from the last few segment downloads, smoothed to avoid reacting to noise, and looks at how many seconds are buffered. With a full buffer it can risk a higher rendition; with a draining buffer it steps down even if the last sample looked fine. The rule is usually: pick the highest bitrate that is some fraction, say 80%, of the estimate.

Why cut segments at keyframes, and what is a GOP?

A group of pictures starts with a keyframe that is decodable alone, followed by frames that reference it. A segment must start with a keyframe so the player can decode it without the previous segment, which is what makes switching renditions possible. Encoders are told to place keyframes every 2 to 4 s to align with segment length.

How do you pre-warm the CDN for a scheduled premiere?

Push the manifests and the first few minutes of every rendition to every PoP ahead of time, or run a job that fetches them through each region so the shield and edges fill. Later segments can stay pull-based because viewers reach them at different moments and the first viewer in each region warms the rest.

What changes for live streaming?

Segments are produced continuously and the playlist grows; the player polls it every segment duration. Latency is roughly three segments behind real time, so live tends to use shorter segments or low-latency extensions with partial segments. Caching rules flip: playlists need very short lifetimes while segments stay immutable.

Connections

In study paths
Video from upload to play
Pairs with
Serve static bytes from the edge · Process uploads in parallel · Let browsers and proxies cache · Keep large blobs out of the database · Move slow work off the request
Used in
Video Streaming Platform (YouTube / Netflix)

The same parts on other clouds

The diagrams above name AWS services as one concrete example. Every part has a close equivalent elsewhere. In an interview, name the role first, such as “a managed Redis cache”, then the product you would pick.

AWSUsed here asGoogle CloudMicrosoft AzureOpen source and others
Amazon CloudFrontCDN edgeCloud CDN, Media CDNAzure Front DoorCloudflare, Fastly, Akamai