Social & content · Intermediate

Live comments

Push comments and reactions to millions of live viewers with SSE and a dispatcher.

Every part, and why it is there

Viewers & commenters

Thousands to millions of people watch the same live video. A few of them comment; all of them expect to see new comments within a second or two.

Load balancer

Sends long-lived streaming connections to the realtime frontends and ordinary POSTs (new comment) to the comment service.

Realtime frontends (SSE)

Each viewer holds one open HTTP connection here and receives a stream of server-sent events. Each node keeps an in-memory map of which of its own connections are watching which video, and pushes to them.

Comment rate limiter

Caps how fast one account can comment. A single script could otherwise flood every viewer’s screen.

Comment service

Stores the comment, runs it past moderation, bumps counters and publishes it for delivery. Late joiners load recent comments from storage instead of the live stream.

Moderation

Blocks spam and abuse before it is broadcast. It sits in the write path because once a comment has been pushed to a million screens it cannot be taken back.

Comment store

Comments partitioned by video id and ordered by time, so “last 50 comments for this video” is one fast read.

Reaction counters

Likes and hearts are counted in memory and broadcast as a total every second or so, rather than sending one event per like to every viewer.

Comment bus

Carries each accepted comment to every dispatcher. Decouples the write side from the delivery side.

Dispatcher

Looks up which frontend nodes have viewers of this video and forwards the comment only to them. Its work grows with the number of frontend machines, not the number of viewers.

Subscription table

Maps video id → frontend nodes that currently have viewers of it. Kept outside the dispatchers so any dispatcher can serve any video.

Asked in interviews at

  • meta
  • nubank
  • amazon
  • bloomberg
  • oracle
  • google
  • figma

Concepts used here

  • Server-Sent Events: Build a live score page from one HTTP response that never ends: a few lines of plain text per event, a browser that reconnects by itself, and an id that lets it catch up on what it missed.
  • WebSockets: Turn one HTTP request into a pipe that stays open, so the server can push a chat message the moment it arrives. Then pay for it: memory per user, heartbeats, reconnects and pub/sub.

Learn more

All system design examples