Skip to content

Video Streaming

intermediate · 35 min · Last reviewed 31 Oct 2026 · by TheLearningLoop Team

Key takeaways

  • Transcode each video into several qualities and cut it into short segments, so a player can switch quality mid-way.
  • Segments are small static files, which a CDN can cache near viewers so most requests never reach your origin.
  • Do transcoding in the background from a queue. A video becomes available only when the pipeline finishes.

Why video streaming is hard, and what it teaches

Video is large, viewers have very different networks, and everyone wants it to start at once and never pause. A video streaming service stores a video once, converts it into many versions, spreads copies close to viewers, and lets each player pick the best version for its current connection.

It gives you two things interviewers always ask about:

  • Smooth playback (latency and quality): quick start, no buffering spinner, and the best picture the network allows.
  • Scalability and cost: serving millions of viewers must not need millions of requests to the origin, because bandwidth is the main cost.

Requirements to confirm first:

  • Functional: upload a video, watch it on many devices, search and resume, optionally live streaming.
  • Non-functional: start in a second or two, few stalls, huge read traffic, and durable storage of the originals.

Terms used below: the origin is your own storage or servers that hold the real files, as opposed to cached copies. A bit rate is how much data one second of video needs, measured in Mbps (megabits per second). A higher bit rate gives a better picture but needs a faster network.

Skip the design, or simplify it, when:

  • You have a few short clips: serve the file from object storage with a CDN in front.
  • Audience is small and on good networks: one quality is enough.

Try it

A viewer watches 14 seconds of video. Change the player's behaviour and the network.

Video Streaming

Step 1 of 16 · Press Space to play, arrow keys to step

Playback starts

Network8 MbpsChosen quality5 Mbps

never changes

Buffer2.0 s

healthy

Screenplaying

spinner shown when stalled

Initial state. The player has 2 seconds of video buffered. Each tick it plays one second and downloads whatever the network allows.

tick:
0
stalls:
0
seconds stalled:
0
average quality (Mbps):
-

1 tick = one step of the simulation.

Things to try:

  • Run Fixed quality with the bandwidth drops network. From tick 6 the buffer falls every second, and the video stalls once, for one second, on the last tick. It stalls late because the buffer had built up before the drop. An earlier or deeper drop would stall sooner.
  • Switch to Adaptive bitrate. The player lowers the quality and keeps playing.
  • Try Bandwidth flaps with both players and compare stalls with average quality. Neither player stalls here, because a buffer of a few seconds absorbs a short dip. The adaptive player still lowers its quality (about 3.3 Mbps on average, against 5.0 for the fixed player), so on brief dips adaptive buys nothing and costs picture quality. Its benefit shows on longer drops.

What to watch: the Buffer box is the player's safety margin, and it is what decides whether a stall happens. This simulation uses a rate-based rule (pick the highest quality that fits within 80 percent of the measured speed). Real players also use buffer-based rules (move down when the buffer is low), and many combine both. The simulation is simplified to one second per tick, not whole segments.

Streaming approaches

ApproachHow it worksGood whenWeakness
Download the whole fileThe player gets one big file before playingSmall clipsSlow start, wasted data if the user leaves
Progressive downloadA single file is played while it downloadsSimple sitesCannot change quality mid-way
Adaptive bitrate (HLS, DASH)The video is cut into short segments in several qualities. The player picks per segmentMost streamingMore storage and a transcoding pipeline
Live streamingAn encoder sends segments continuously with a short delay, set by segment length and the player's bufferEvents and sportsDelay and sudden audience spikes

Origin only vs CDN

  • Origin only: every viewer fetches segments from your storage. It is simple, but your servers and bandwidth bill grow with the audience, and far-away viewers are slow.
  • CDN (content delivery network): copies of popular segments are cached on servers near viewers. A cache hit never reaches your origin. Segments are small, static files, which suits caching very well. The price is cache invalidation and CDN cost.

How adaptive streaming works

  1. After upload, a transcoding pipeline converts the video into several resolutions and bit rates (a ladder), each cut into segments of a few seconds.
  2. A manifest file lists the versions and their segments. The player downloads it first.
  3. The player measures how fast segments arrive and keeps a buffer of a few seconds.
  4. For each next segment it picks the highest quality that fits the measured speed. When the buffer falls, it drops to a lower rung. When the network recovers, it goes up.
  5. Segments are plain files over HTTP, so any web server or CDN can serve them.

Streaming design vs the alternatives

ConcernAdaptive bitrate with a CDNSingle file from storageStreaming through your app servers
Handles different networksYesNoPossible
Load on your serversLowMediumHigh
Fast startYesSlowMedium
Storage usedHigh (many versions)LowLow
Extra moving partsTranscoding, CDNNoneCustom servers
Best forReal video productsSmall sitesAlmost never

Use adaptive bitrate and a CDN for anything beyond a few clips.

High-level design

  • Write path (upload and processing): the client uploads in chunks to object storage. An event goes on a queue, transcoding workers produce the ladder, the segments, a manifest and thumbnails, and then mark the video ready in the metadata database.
  • Read path (playback): the player asks an API for the video's metadata and manifest address, downloads the manifest from the CDN, and then fetches segments from the CDN. The CDN fetches from the origin only on a miss.
  • Metadata, search and resume: a database holds title, owner, status, manifest location and per-user resume position. Search runs on a separate index fed from that database. These are small, ordinary data and are not on the segment path.
  • Live streaming: an encoder sends segments continuously and the manifest keeps changing. Delay depends on segment length and how much the player buffers, and shorter segments lower it at the cost of more requests.

Capacity estimation

These numbers are illustrative assumptions, used to show the method. Use the interviewer's numbers when they give them.

  • Storage per hour of video: the ladder in this lesson is 1, 3 and 5 Mbps, which is 9 Mbps in total. 9 Mbps for 3,600 seconds is 32,400 megabits, or 4,050 MB, so about 4 GB per hour of video, before the original file. Real ladders have more rungs.
  • Storage growth: if 10,000 hours are uploaded each day, that is 10,000 x 4.05 GB = 40,500 GB, so about 40 TB a day or about 14.8 PB a year.
  • Files: with 2-second segments, one hour is 1,800 segments per rung, so 5,400 segment files for three rungs. A 2-second segment at 3 Mbps is 6 megabits, so 0.75 MB.
  • Playback egress: 1 million viewers at an average of 3 Mbps is 3,000,000 Mbps, which is 3 Tbps. No single origin can serve that, so the CDN must.
  • Origin load: with a 95 percent CDN hit rate, the origin sees 5 percent, which is 150 Gbps. At a 99 percent hit rate it is 30 Gbps. Each percentage point of hit rate matters.
  • Request rate: 1 million viewers each fetching one 2-second segment every 2 seconds is 500,000 segment requests a second. At 95 percent hits, 25,000 a second reach the origin.

Avoiding a new single point of failure

  • Upload and transcoding: use a queue with retries so a failed job is retried. Videos are not available until the pipeline finishes.
  • Origin storage: replicate across locations. The CDN can keep serving popular videos even if the origin is briefly down.
  • CDN: use more than one provider or region for important events.
  • Metadata database: replicate it. The video file and the information about it live apart.

You accept extra storage for many versions, and a delay between upload and availability.

Bottlenecks and how to relieve them

  • Transcoding: it is the heaviest compute in the system and bursts after uploads. Run workers from a queue and scale them with queue depth. Spend effort on popular videos first, and consider producing rarely watched rungs only when first requested.
  • CDN egress and cost: bandwidth is the main bill. A higher hit rate, a mid-tier cache between edge and origin, and a sensible ladder (not too many rungs) all reduce it.
  • Cache-miss storm: when a new or evicted segment is requested by many viewers at once, all the misses can reach the origin together. Request collapsing (shown in the NGINX example as proxy_cache_lock) and pre-warming prevent it.
  • Hot titles: one popular video concentrates load on the same few segments. They stay in cache, so the risk is at the first moment of a release, not afterwards.
  • Live delay: a shorter segment lowers delay but raises the request rate and the chance of stalls, so choose it from the delay target.

Common mistakes

  • Serving large files from application servers. Use object storage and a CDN.
  • Transcoding inside the upload request. Do it in the background from a queue.
  • One quality only. Viewers on slow networks stall.
  • Very long segments. Quality changes and startup become slow. Very short segments have their own cost: more requests, larger manifests and less efficient compression, so a few seconds is the usual compromise.
  • Ignoring cache keys. Different URLs for the same segment defeat the cache.
  • No plan for a viral spike. Pre-warm caches and limit origin requests.

How to talk about video streaming in an interview

  • Split the system into upload and processing (write path) and playback (read path).
  • Explain transcoding to a ladder of qualities, segments with a manifest, and adaptive bitrate in the player.
  • Put object storage at the origin and a CDN in front, and mention a queue for transcoding.
  • Name the trade-off: more storage and processing in exchange for smooth playback on any network.
  • Give rough numbers: storage per hour of video, egress for the audience, and origin load at a stated hit rate.

Related topics: caching, blob and object storage, message queues and async and real-time communication.

Where you meet video streaming in real systems

Every on-demand video site, live event and video call recording works this way. Here are three drawn out, the first with configuration you can run, then more examples.

Example 1: Cutting a video into segments and caching them

A command cuts a video into short segments and writes a manifest. A web server then serves the segments with caching in front of an origin. Open the example below to see the picture and the configuration.

Config: HLS segments and a caching proxySetup 1 command and 1 configCode no code changes
Uploadoriginal fileTranscodequalities + segmentsCache / CDNnear viewersPlayerpicks quality
An uploaded video is transcoded into segments in several qualities. A cache near the viewer keeps popular segments so most requests never reach the origin.
# Cut one input into 2-second HLS segments and a playlist (the manifest).
# input.mp4 is a placeholder for your file. Run it once per quality, changing -s (size) and -b:v (bit rate).
# -g and -keyint_min 50 force a new keyframe every 2 seconds at 25 frames per second, so segments split cleanly.
# -hls_playlist_type vod means a complete video, not a live stream.
ffmpeg -i input.mp4 \
  -c:v libx264 -c:a aac \
  -g 50 -keyint_min 50 -sc_threshold 0 \
  -f hls -hls_time 2 -hls_playlist_type vod \
  -hls_segment_filename "seg_%03d.ts" index.m3u8

The ffmpeg command was run with a generated test video and produced a playlist and segments. The NGINX file passed nginx -t on the official image. Names, sizes and addresses are examples.

Example 2: From upload to playback

Uploads land in object storage, a queue triggers transcoding workers, and the results are stored for the CDN to serve.

UploadStorageTranscodersSegmentsCDN
An upload is stored, then a transcoding job turns it into segments in several qualities. Viewers fetch the segments through the CDN.

Example 3: A quality ladder

The same video exists in several versions. The player moves between them as the network changes.

Mbps135Picture360p720p1080p
Each rung has its own bit rate. The player picks the highest rung that fits its measured speed and moves down when the network slows.

More real-world scenarios

4. On-demand movies and shows

Libraries are transcoded once and cached at the edge. Popular titles are pushed to caches before a big release.

5. Live sports and events

An encoder sends short segments every few seconds. The audience can jump from thousands to millions, so caches absorb the load.

6. User-generated video

Uploads of any size and format are checked, transcoded in the background and published when ready, with a thumbnail made first.

7. Video calls and recordings

Calls use different low-delay protocols, but recordings are stored and streamed like any other video afterwards.

8. Online courses

Lessons are stored in several qualities, resume points are saved per learner, and downloads may be allowed for offline use.

9. Security and rights

Segments are encrypted and the player fetches a licence key, so only paying users can watch.

10. Thumbnails and previews

Small images and short previews are generated during transcoding so lists load fast without starting a video.

Tools and managed services

KindNameNotes
TranscodingFFmpegOpen source tool for conversion and segmenting
Object storageAmazon S3, Google Cloud Storage, Azure Blob StorageHold originals and segments
CDNAmazon CloudFront, Cloudflare, AkamaiCache segments near viewers
Managed videoAWS Elemental MediaConvert, Google Cloud Transcoder APIHosted transcoding
FormatsHLS, MPEG-DASHSegment and manifest standards for adaptive streaming

The takeaway

Whenever a design serves large media to many viewers, store it once, cut it into small cacheable segments in several qualities, and let a CDN and the player's adaptive choice do the rest.

Interview questions

Try answering out loud before you reveal hints. Interviewers care about how you reason, not just the final answer.

  1. 1.

    Viewers have very different network speeds. How does your design give each a smooth experience?

    medium
  2. 2.

    Why put a CDN in front of the video storage?

    easy
  3. 3.

    A user uploads a 2 GB video. Walk through what happens before others can watch.

    medium
  4. 4.

    A video goes viral and a million people request it at once. What protects the origin?

    hard
  5. 5.

    How is live streaming different from on-demand?

    hard
  6. 6.

    Roughly how much storage and bandwidth does a video service need?

    medium

Finished this lesson?

Progress is saved on this device. Create a free account to keep it everywhere.

Topics:Video streamingYoutubeNetflixAdaptive bitrateHlsCdnTranscodingCase study

↑ Back to top

Next lesson