AlgoViz

Design Twitch

hard 45 min

Design a live streaming service like Twitch: one person broadcasts, many watch a few seconds later, and they all chat.

Video lesson · Chapter 1 of 5

The streamer sees the goal before the audience

0:00 / 5:13

Live streaming in 90 seconds

1:30Reel coming soon

Theory

Read or listen first. About 2 min.

What it is

A live streaming service takes one video signal from a person who is broadcasting, cuts it into short files, makes copies in several sizes, and lets many people download those files while the next ones are still being made.

It is really two systems side by side. The video path moves large amounts of data to many people with a delay of a few seconds. The chat path moves tiny messages in both directions in under half a second. They share a page on the screen and almost nothing else.

Why it exists

A recorded video can be processed for an hour before anyone presses play. A live one cannot. Each second of video must be received, converted, stored and delivered within a few seconds, for hours, without a pause.

People come for the conversation. If the broadcaster answers a question thirty seconds late, the conversation is gone. That makes delay a feature of the product, not only a technical number, and it is why interviewers like the problem: every part of the design pays for delay in some way.

A short history

  1. May 2009

    Apple releases HTTP Live Streaming, which sends video as a list of small files over ordinary web servers.

  2. June 2011

    Twitch opens in public beta, split off from the general streaming site Justin.tv.

  3. December 2012

    Adobe publishes the RTMP specification, version 1.0, dated 21 December. RTMP is how most broadcasting programs upload.

  4. August 2017

    RFC 8216 writes down HTTP Live Streaming, including how far behind the live end a player should stay.

  5. 2019

    Apple announces Low-Latency HLS, which publishes a segment in small parts before the segment is complete.

Words to know

Ingest
Receiving the broadcaster's video.
Transcoding
Decoding video and encoding it again at another size or bitrate.
Rendition
One version of the stream, such as 720p at 3.5 Mbps.
Segment
A short file of video, here two seconds long.
Playlist
A text file that lists the newest segments. Players read it again and again.
Adaptive bitrate
The player picks the rendition that its connection can carry, and changes its mind as conditions change.
Glass to glass
The delay from the camera lens to the viewer's screen.
Fan-out
Turning one message into a delivery for each listener.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

How it works

Step through the idea, or press Play.

Step 1 of 3

Broadcast. One upload to a nearby ingest site

Understand it

The juggler and the mirrors
One street, one show

The show travels by mirrors. The cards travel by runner. Neither waits for the other.

The product

What are we building?

A service like Twitch, for games, sport, music and conversation. Broadcasters go live from a program on their computer. Viewers watch on phones, browsers, televisions and consoles, chat beside the video, follow channels and pay to support them.

  • Go live

    Upload one stream to a nearby site and be visible in seconds.

  • Watch

    Adaptive quality, a few seconds behind the camera.

  • Chat

    Messages in under half a second, with moderation.

  • Support

    Subscriptions, small gifts, and points for watching.

Similar applications

ServiceKnown for
YouTube LiveLive next to a huge video library
KickLive channels, with a different share for creators
Facebook LiveLive inside a social network
TikTok LiveLive inside a short video app
TrovoGames
RumbleVideo hosting with live channels

The hard part

Nothing can be prepared, and everyone answers at once

Compare with Design YouTube, where a file exists before the first viewer. Here each segment is watched by thousands within seconds of being made, then almost never again. And the audience writes back: a goal makes a whole crowd type in the same second.

The finished system

Where the design ends up

The whole system

Inside our system, video goes from the streamer to ingest sites, transcoders and the origin, then to the CDN edge and viewers; the origin also saves recordings to S3. Chat goes from viewers to chat edges and room servers, which ask a safety check and send to Kafka and Cassandra. The API service uses PostgreSQL.
Orange is video, pink is chat, blue is everything else. Dashed arrows happen later.

Ideas owned by other topics

IdeaRead
Edge caches and shieldsCDN design
WebSocket and alternativesReal-time APIs
Ordered team chat with historyGroup chat app
Followers' noticesNotification service
Money and giftsDigital wallet
Crowd on one cold keyThundering herd

Summary

The whole topic in one minute.

A live stream enters at a site near the broadcaster, is converted into several renditions, cut into two-second segments with smaller parts, and copied outward by a CDN. Most of the delay is the player's safety buffer, so low latency means parts, a small buffer and some risk of stalling.

Chat is separate because it scales with messages times listeners. A room server gives each channel one order, edge servers hold the sockets and make the last copies, and above a readable rate each viewer receives a sample. Money goes through transactions, and neither path may take the other one down.

  • Video and chat scale with different numbers, so they are two systems.
  • Most delay is the player's buffer, and the buffer is three parts or three segments long.
  • Pass the source through, and convert only channels where a smaller rendition helps someone.
  • One message to 100,000 people goes 1 to 5 to 200 to 100,000, and floods are sampled.
  • A dropped broadcaster gets a minute to come back, under the same session.

Sources