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.

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

Our systemStreamerIngest siteTranscoderOriginAPI serviceCDN edgeKafka

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

Our systemStreamerIngest siteTranscoderOriginAPI serviceCDN edgeKafka

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

    major

    A lost leave is never subtracted. After a day the number is fiction.

    Wrong
    on_join:  viewers += 1on_leave: viewers -= 1
    Right
    seen[session] = now           # heartbeatviewers = count(seen within 2 minutes)
  • Delivering every chat message to every viewer

    critical

    100,000 viewers times 5,000 messages is 500 million writes a second, and nobody can read them.

    Wrong
    for v in room.viewers:    v.send(message)
    Right
    frame = always_show + sample(rest, 20)send(frame)      # every 250 ms
  • Transcoding every channel

    major

    Most channels have almost no viewers. The cards work for nobody.

    Wrong
    for c in live: transcode(c, full_ladder)
    Right
    for c in by_benefit(live):    if c.viewers >= 5: transcode(c)
  • Accepting files from an old connection

    major

    After a reconnect, late parts of the first connection overwrite the new ones.

    Wrong
    origin.store(session, part)
    Right
    if part.epoch == session.epoch:    origin.store(session, part)
  • Caching the playlist like a segment

    critical

    A segment never changes. A playlist changes every part, so a stale one freezes the picture.

    Wrong
    cache(playlist, seconds=60)
    Right
    cache(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.