Design Twitch
hard 45 minDesign a live streaming service like Twitch: one person broadcasts, many watch a few seconds later, and they all chat.
See it run
Follow a broadcast from a camera to a screen, and a message from one viewer to a hundred thousand.
Operations
Go live
The broadcaster's program connects, video flows, and followers hear about it once there is a picture.
Step 1 of 10
Connect
Connection
opening
The program opens one lasting connection to a nearby ingest site and sends the stream key. Near matters: upload is the one part nothing can cache.
Step 1: Connect
def on_connect(key, site): channel = keys.find(hash(key)) # unknown: refuse session = sessions.open(channel, site) # lease, epoch + 1 return session.settingsdef on_first_key_frame(session): session.state = "live" log.send("live", session.id) # notices, searchdef on_part(session, epoch, part): if epoch == session.epoch: # drop late files origin.store(session.id, part)- Delay
- about 5 s
- Load
- 300,000 live at peak
- Upload
- 6 Mbps
- Renditions
- source and 4
Say the first picture comes before the first notice.
In the real world
The same live streaming doing real jobs. Pick one and step through it.
Twenty seconds without internet. How little can viewers notice?
Step 1 of 4
The upload stops
State
reconnecting
Left
60 s
No data for two seconds. The session changes to reconnecting, and its lease stays.
Step 1: The upload stops
Common mistakes
The bugs interviewers watch for, with the fix beside each.
Counting viewers with plus one and minus one
majorA lost leave is never subtracted. After a day the number is fiction.
Wrongon_join: viewers += 1on_leave: viewers -= 1Rightseen[session] = now # heartbeatviewers = count(seen within 2 minutes)Delivering every chat message to every viewer
critical100,000 viewers times 5,000 messages is 500 million writes a second, and nobody can read them.
Wrongfor v in room.viewers: v.send(message)Rightframe = always_show + sample(rest, 20)send(frame) # every 250 msTranscoding every channel
majorMost channels have almost no viewers. The cards work for nobody.
Wrongfor c in live: transcode(c, full_ladder)Rightfor c in by_benefit(live): if c.viewers >= 5: transcode(c)Accepting files from an old connection
majorAfter a reconnect, late parts of the first connection overwrite the new ones.
Wrongorigin.store(session, part)Rightif part.epoch == session.epoch: origin.store(session, part)Caching the playlist like a segment
criticalA segment never changes. A playlist changes every part, so a stale one freezes the picture.
Wrongcache(playlist, seconds=60)Rightcache(segment, seconds=3600)cache(playlist, seconds=1) # or hold the request
Interview tips
Open with two systems
- Video grows with viewers.
- Chat grows with messages times viewers.
- Either may fail alone.
Show the budget
- 9.3 seconds in normal mode.
- 3.1 seconds in low latency.
- The buffer is the largest line.
Spend compute where people are
- Source passes through.
- Thirteen channels per card.
- Convert by viewers.
Say what viewers see
- Drop: a reconnecting screen.
- Flood: a readable sample.
- Card fails: source continues.