Skip to content

Design Twitch

A system design interview walkthrough for live streaming like Twitch, covering ingest, live transcoding, low-latency HLS delivery to many viewers and failover.

Advanced22 minUpdated 2 December 2026

Before this: Content Delivery Network (CDN), Transport Protocols: TCP, UDP, QUIC, Load Balancing

"Design Twitch" is the question where time matters. With on-demand video, a segment can sit in a cache for a week. In a live stream, a segment is useful for about 10 seconds, it does not exist until the streamer's camera has produced it, and thousands of viewers are waiting for it at the same instant.

The interviewer is testing whether you can reason about latency as a budget made of stages, and whether you can choose between three delivery technologies knowing what each gives up. They also want to see the asymmetry: a few hundred thousand streamers sending video in, and millions of viewers pulling it out, with a fan-out ratio of dozens to one overall and hundreds of thousands to one for a single popular channel.

This page covers live ingest, live transcoding and delivery. The chat beside the video is a separate fan-out problem, handled in Design Live Comments. Stored video, uploads and view counting are in Design YouTube. Thinking in latency budgets is a theme of the multimodal and voice systems lesson, and we apply the same discipline here.

Clarify the requirements

Functional requirements:

  1. A streamer sends live video from software or a camera, authenticated by a stream key.
  2. Viewers watch a channel live, on web, mobile and television, with several quality choices.
  3. Viewers see the channel as live or offline, and a viewer count.
  4. Streams are recorded so they can be watched later for a limited time.
  5. A viewer can follow channels and receive a notification when one goes live.

Out of scope, said aloud: chat and emotes (see Design Live Comments), subscriptions and payments, clips editing, moderation tooling, ad insertion.

Non-functional requirements: these decide the architecture.

  • Glass-to-glass latency of a few seconds. The streamer talks to chat, so a delay of 30 seconds ruins the interaction. A delay of 3 to 6 seconds is workable.
  • Smooth playback matters more than sharpness. A stall is worse than a lower quality.
  • Ingest is resilient. A streamer's connection drops for a few seconds, and the stream should resume without ending the broadcast.
  • Massive and bursty fan-out. One channel may go from 1,000 to 300,000 viewers in minutes when a famous person joins or "raids" it.
  • Cost control. Live encoding is expensive, so the system should not spend it on streams nobody is watching.

Estimate the scale

Assume 30 million daily active viewers. At the evening peak there are 100,000 live channels and 6 million concurrent viewers. The biggest channel has 500,000 of them.

Ingest bandwidth: 100,000 streams at 6 Mbps each is 600,000 Mbps, which is 600 Gbps coming in.

Delivery bandwidth: 6 million viewers at about 4 Mbps average is 24 Tbps going out. The fan-out ratio is 24 Tbps ÷ 0.6 Tbps = 40 to 1, and for the biggest channel it is 500,000 to 1.

Transcoding: assume each live stream is re-encoded into 3 lower renditions at about one core each: 100,000 × 3 = 300,000 cores if you do it for everyone. Most channels have a handful of viewers, so a design that transcodes only watched channels is far cheaper.

Playback starts: 30M viewers × 3 starts a day = 90 million a day, about 1,000 per second on average. Joins arrive in bursts, for example a raid sending 100,000 viewers in 10 seconds, which is 10,000 per second.

Recordings: assume 1 million streams a day averaging two hours at 6 Mbps, which is 5.4 GB each: 1M × 5.4 GB = 5.4 PB a day. Kept for 14 days, that is about 76 PB.

"The unusual number is the fan-out. 600 Gbps in and 24 Tbps out means delivery is the cost, and for one channel it is 500,000 viewers asking for the same two-second file at the same moment. A live system is a CDN problem with a deadline, plus a transcoding bill to be managed."

The capacity estimator reproduces the 1,000 playback starts per second and the 14-day recording volume.

API and data model

Streamers use a media protocol and everything else is ordinary HTTP.

RTMP / SRT   rtmps://ingest.<region>/live/<streamKey>        (streamer -> ingest)
GET  /v1/channels/{id}/playback    -> { status: live|offline, manifestUrl, viewerCount }
GET  <cdn>/live/<channel>/<rendition>/playlist.m3u8          (data plane)
GET  <cdn>/live/<channel>/<rendition>/<seq>.m4s?part=n       (partial segments)
POST /v1/follows/{channelId}
channels    (channel_id PK, owner_id, stream_key_hash, status, ingest_region)
live_state  Redis: channel_id -> { startedAt, ingestNode, renditions[], lastSegmentSeq }
segments    object store: live/<channel>/<rendition>/<seq>   (short TTL), recordings/<channel>/<date>/...
follows     (user_id PK, channel_id CK)

The non-obvious choice: live_state is soft state in a fast store, not a database row. It is rebuilt by the pipeline within seconds if it is lost, and the playlist is generated from it. Treat "is this channel live?" as a lease that the ingest node renews, so a crashed node cannot leave a channel stuck live.

High-level design

We simulate the delivery slice, since that is where the scale is: viewers fetch playlists and segments from CDN edges, which go to an origin that serves the live window from storage and checks live state.

Viewers pull live segments from CDN edges, which fall back to an origin that reads the live window from S3 and channel state from Redis.viewersedge cacheslive originlive segmentslive state
Viewers pull live segments from CDN edges, which fall back to an origin that reads the live window from S3 and channel state from Redis.
  • Edges serve the playlist and recent segments, and collapse concurrent requests for the newest one.
  • The origin reads the live window from object storage and the channel's live state from Redis.
Describe this diagram

Components

  • viewers (Client)
  • edge caches (Content delivery network)
  • live origin (Service)
  • live segments (Object storage (S3))
  • live state (Redis)

Connections

  • viewers sends to edge caches
  • edge caches sends to live origin
  • live origin sends to live segments
  • live origin sends to live state

Run it, then try cutting the edge's TTL or lowering the number of edge locations. Short TTLs are what live demands, and they put pressure on the origin.

The ingest path (not simulated): the streamer's software sends RTMP or SRT to the nearest ingest point, which checks the stream key and relays the stream over a private backbone to a transcoder.

The processing path: the transcoder produces the quality ladder, cuts each rendition into short segments and parts, writes them to storage and updates live_state. A recorder writes the same segments to long-term storage.

The delivery path: the player fetches the playlist, which is regenerated every fraction of a second, then fetches the newest segments in order from the edge.

The interesting problem: low latency at huge fan-out

The choice of delivery technology sets the latency floor and the cost curve.

Approach A: classic HLS with large segments

Cut video into 6-second segments, publish a playlist, and let the CDN serve plain files. Players usually start three segments back.

Good: the cheapest and most scalable approach. It is ordinary HTTP caching, and every viewer of a channel shares the same objects.

Bad: latency is the segment length times the player's buffer plus processing, here 6 s × 3 = 18 s, and in practice 20 to 30 seconds. That is too slow for an interactive audience.

Approach B: WebRTC to every viewer

Push video to each viewer over a real-time protocol, with sub-second latency, as in Design Zoom.

Good: the lowest latency, under a second.

Bad: every viewer holds a stateful connection to a media server that sends their own copy, so a shared CDN cache cannot help. For 500,000 viewers on one channel you need a tree of media servers and a large bill. See TCP, UDP and QUIC for why real-time media accepts loss while cached HTTP does not.

Approach C: low-latency HLS with chunked, partial segments

Keep HTTP and the CDN, but shrink the unit. Cut each 2-second segment into parts of about 250 to 500 ms and publish each part as soon as it is encoded, before the whole segment is finished. The player requests the next part with a blocking playlist reload: it asks for "the playlist including part N", and the server holds the request open until part N exists, then answers immediately. Chunked transfer lets the player start downloading a part while it is still being written.

Good: typical latency of 3 to 6 seconds, still plain HTTP, still cacheable and shared by all viewers of the channel. The CDN collapses the thousands of simultaneous requests for the same new part into one request to the origin.

Bad: more requests (several per second per viewer), shorter useful life for every object, a tighter origin and CDN relationship, and players that must support the extension.

The recommended design is C for the mass audience, with the option of an interactive WebRTC path for a small number of participants (a guest on stream) whose output is itself fed back into the ladder. The latency budget is the justification, and it is worth writing down:

  • Capture and encode on the streamer's machine: 0.5 s
  • Ingest transit: 0.3 s
  • Live transcode and packaging: 1.5 s
  • CDN and origin propagation: 0.5 s
  • Player buffer: 2 s
  • Total: about 4.8 s

The second decision: ingest and failover

The streamer is the single point of failure for the content, and their home network is the weakest link in the chain.

Protocol. RTMP over TCP is the common standard for streaming software, and is simple but reacts poorly to loss. SRT (a UDP-based protocol with retransmission) tolerates a bad uplink better. Accept both, and say why: be liberal at the edge of the system, standard inside it.

Grace period. When the source disappears, do not mark the channel offline for 20 to 30 seconds. The player keeps showing the last frame or a "reconnecting" slate. Only after the grace period do you end the session, and a quick return continues the same recording.

Bottlenecks and failures

A raid. A huge streamer sends 100,000 viewers to a small channel in ten seconds. The channel's segments are suddenly the hottest objects in the system. The CDN's request collapsing and an origin shield turn 100,000 simultaneous requests into one per edge, and then one per shield. Start the full ladder as soon as the first viewers arrive, and accept that the first minute is at source quality.

The transcoder falls behind. Segments are produced late and viewers' buffers drain. Monitor the encode speed as a ratio of real time (it must stay above 1.0). Drop to fewer renditions or lower resolution on overloaded nodes, since a lower-quality stream that keeps up beats a better one that stalls.

What if the origin is down? Edges can serve the segments they already hold, so viewers lose about the length of the player's buffer and the stream stalls. Run the origin active-active across two zones, with the packager writing to both.

Abuse and takedowns. Illegal live content must be stoppable quickly. Provide a kill switch that ends ingest, invalidates the stream key and reports the channel offline.

Trade-offs to name aloud

  • Low-latency HLS over classic HLS: several seconds lower latency, paid for in more requests and tighter caching.
  • LL-HLS over WebRTC for the mass audience: about 5 seconds instead of 1, in exchange for CDN economics and reach.
  • Transcode on demand: large compute savings, and the first viewers see source quality or a short delay.

What interviewers listen for

  • A working answer: RTMP ingest, a transcoder producing HLS segments, a CDN for delivery and a database for channel data.
  • A strong answer: sizes the asymmetry between ingest and delivery, builds a latency budget by stage, chooses low-latency HLS over WebRTC with reasons, and uses request collapsing at the CDN for the newest part.
  • A standout answer: also transcodes only watched channels, runs redundant ingest with a grace period, handles raids with an origin shield, monitors encode speed against real time, protects stream keys and plans a kill switch.

Check your understanding

Common mistakes

Designing it as stored-video streaming. In live, the object does not exist until a moment ago, and its useful life is seconds. Caching rules, TTLs and the playlist all change.

Choosing WebRTC for everything. It wins on latency and loses on cost and reach. Say where you would still use it: a small interactive group.

No story for the streamer's connection. The weakest link is the home uplink. Without redundancy and a grace period, every blip ends a broadcast.

Ignoring the playlist. The manifest is a hot object that changes constantly. Explain how it is cached and refreshed.

Practice

A streamer's uplink degrades and their ingest bitrate drops from 6 Mbps to 2 Mbps with occasional gaps. Viewers see constant buffering. What can the system do?

Write your own answer first, on paper or in a note. Then compare.

Show a hint

Think about what the transcoder can control, and what the viewer's player does when segments arrive late.

Show a strong answer

Detect it at ingest. Measure incoming bitrate, frame gaps and round-trip time. A bad source is visible within seconds, before viewers complain.

Reduce the ladder to match the source. If the incoming stream is 2 Mbps, there is no point offering 1080p at 6 Mbps. Cap the top rendition below the source's actual quality, so the manifest does not promise what cannot be delivered.

Let viewers adapt. The player will drop to lower renditions when parts arrive late, which is the same adaptive logic as in Design Netflix. Make sure the lowest rung is available early.

The pattern worth keeping: a live pipeline can only be as good as its worst input, so measure the input, shape the output to match and tell the producer.

The product wants 'clips': a viewer presses a button and gets the last 30 seconds of the stream as a shareable video. How would you build it?

Write your own answer first, on paper or in a note. Then compare.

Show a hint

You already write live segments somewhere for a short time. Decide what the clip is made from.

Show a strong answer

Use the live window that already exists. The origin keeps the last few minutes of segments for each rendition. A clip request names the channel and a time range, such as the 30 seconds ending now.

Cut at segment boundaries. With 2-second segments, a 30-second clip is 15 segments, plus a trim of the first and last for precision. Copy references to those segments into a new clip manifest rather than copying the bytes, so creating a clip is a metadata write.

The pattern worth keeping: build a new feature from the data the pipeline already produces, as references to immutable segments, instead of a new pipeline.

Key takeaways

  • Live is a CDN problem with a deadline. The segment is useful for seconds.
  • Build a latency budget by stage, and choose the technology that fits it: about 5 seconds with low-latency HLS.
  • Use LL-HLS for the mass audience and WebRTC only for small interactive groups.
  • Collapse requests for the newest part at the edge and at an origin shield.
  • Transcode only what is watched, and run redundant ingest with a reconnect grace period.
  • Ingest is 600 Gbps and delivery is 24 Tbps: the fan-out sets the cost.

Further reading

  • R. Pantos and W. May, RFC 8216 "HTTP Live Streaming" (2017): the segment and playlist model that low-latency variants extend.
  • H. Schulzrinne et al., RFC 3550 "RTP: A Transport Protocol for Real-Time Applications" (2003): the transport underneath real-time media.
  • W3C, "WebRTC 1.0: Real-Time Communication Between Browsers" (Recommendation, 2021): the browser API for sub-second delivery.

Check yourself

3 questions in this lesson. Answer each once to complete it.

Practice this

Lessons in this track