← Back to System Design, page 4

system-design-0066 · Hard senior

Design a live video streaming platform like Twitch or YouTube Live where one stream can reach 1 million concurrent viewers with a few seconds of delay.

The streamer sends RTMP or SRT to an ingest server, which transcodes in real time into a ladder of bitrates and cuts the stream into short segments (1 to 2 seconds, or smaller parts for low-latency HLS). Segments and playlists are pushed to origin storage and fanned out through a CDN; players fetch the newest segments and switch bitrate as their bandwidth changes. One stream at 1 million viewers and 3 Mbps is about 3 Tbps of egress, which only a CDN can serve.

Full answer

Requirements

Functional:

  • Streamers broadcast from software or a phone; viewers watch live with adaptive quality.
  • Live chat alongside the stream; the stream is recorded for later viewing.

Non-functional:

  • Glass-to-glass delay of a few seconds (standard HLS is 15 to 30 seconds; low-latency modes reach 2 to 5).
  • Smooth playback for viewers on weak connections.
  • A popular stream must not overload origin servers.

Capacity estimates

Assumptions: one big stream with 1M concurrent viewers at an average of 3 Mbps, a ladder of 6 renditions (1080p down to 160p), 100,000 concurrent streams platform-wide, most with a few viewers.

  • Egress for the big stream: 1M x 3 Mbps = 3 Tbps, served by CDN edges.
  • Origin egress stays small: each CDN edge region fetches each segment once and serves it many times.
  • Transcoding: 100,000 streams x 6 renditions; GPU or hardware encoders handle dozens of renditions per machine, so the fleet is in the low thousands of machines. Small streams may get fewer renditions to save cost.

API

Ingest: rtmp://ingest.example.com/live/{stream_key} POST /v1/streams -> {"stream_id", "stream_key", "ingest_url"} GET /v1/streams/{id}/master.m3u8 -> playlist listing renditions GET /v1/streams/{id}/720p/index.m3u8 ; .../720p/seg_18342.m4s WS /v1/streams/{id}/chat

Data model

  • streams: stream_id, owner, status (live, ended), started_at, ingest_node, renditions.
  • Segments in object storage: live/{stream_id}/{rendition}/seg_{n}.m4s, plus rolling playlists listing the latest segments.
  • Recordings: the same segments kept and stitched into a video-on-demand asset after the stream.

High-level design

mermaid
flowchart LR
  S[Streamer] -- "RTMP or SRT" --> IN[Ingest edge]
  IN --> TR[Real-time transcoder]
  TR --> PK[Packager: segments and playlists]
  PK --> OR[(Origin storage)]
  OR --> SH[Origin shield]
  SH --> CDN[CDN edges]
  CDN --> V[Viewers]
  V --> CH[Chat service]

Latency and delivery

  • Segment length drives delay: players buffer about three segments, so 6-second segments mean roughly 20 seconds. Low-latency HLS or DASH sends partial segments (around 200 to 500 ms) and lets players request the next part before it exists, bringing delay to a few seconds.
  • An origin shield layer between CDN edges and origin collapses many edge requests for the same new segment into one.
  • Request coalescing at the edge stops a "thundering herd" when a million players ask for the next segment at the same moment.
  • Players use adaptive bitrate: they measure download speed and choose the rendition that keeps the buffer healthy.

Bottlenecks and trade-offs

  • Latency versus stability. Shorter segments and smaller buffers mean less delay but more rebuffering on poor networks; offer a low-latency mode for interactive streams.
  • Transcoding cost. Transcode all renditions only for streams with enough viewers; pass through the original for tiny streams.
  • Ingest failure. Streamers reconnect to another ingest node; the player sees a short gap. Redundant ingest from the streamer is possible for big events.
  • Chat at 1M viewers is its own fan-out problem: sample or rate-limit messages and shard rooms.
  • WebRTC gives sub-second delay but is expensive to scale to millions; it suits small interactive streams.

By level

Fresher: Describe the path from streamer to ingest, transcoding, small video files and the CDN. A fresher should explain why viewers get different qualities.

Mid: Explain segment-based delivery and latency, origin shield, adaptive bitrate and the egress math. Mention recording for later viewing.

Senior: Compare LL-HLS, DASH and WebRTC, discuss transcoding economics, request coalescing, ingest redundancy and chat at scale. Expect clear latency versus stability reasoning.

Key points

  • Ingest, real-time transcode into a bitrate ladder, then segment
  • Segment length sets latency; low-latency HLS uses partial segments
  • CDN plus origin shield serve 3 Tbps for one big stream
  • Request coalescing prevents herds on new segments
  • Adaptive bitrate on the player

Common mistakes

  • Serving viewers from origin servers
  • Claiming standard HLS gives one-second latency
  • Transcoding every rendition for streams with two viewers
  • Ignoring chat scale for big streams

Follow-ups

  • How would you get delay under one second?
  • How do you insert ads into a live stream?
  • How do you handle a streamer with an unstable upload connection?

Tips

  • Explain the segment-latency link with numbers
  • Show the egress math to justify the CDN
  • Mention origin shield and coalescing

Evaluation rubric

  • Correct ingest to CDN pipeline
  • Latency reasoning
  • Egress capacity math
  • Cost and failure trade-offs