Video in progress
A live stream gets tens of thousands of comments and reactions per second. How do you deliver them to all viewers?
Pushing each comment to each viewer separately multiplies the load by the number of viewers. Comments are distributed per stream through Kafka/pub-sub to gateway servers, and each gateway sends to its own connections; reactions are aggregated and delivered as a count every second, and comments are sampled.
📖 Answer
The numbers: 500 thousand viewers, 5 thousand comments per second. If we send every comment to every person, that's 2.5 billion messages per second. Nobody can handle that, and there's no need to: a person can't read 5,000 comments a second.
The architecture:
client -> API -> Kafka (topic: live-comments, key = streamId)
|
fan-out service (by streamId) -> one copy to each gateway
|
gateways (WebSocket, ~50–100 thousand connections each) -> to their own viewers
The main tricks:
- Fan-out at the gateway level. The fan-out service sends a message not to every viewer but once to each gateway that has viewers of this stream. Not 500 thousand, but 10–20 copies.
- Sampling. 20–30 comments per second on screen is enough. The rest are stored, but not distributed to everyone in real time. Replies from friends and from the author get priority.
- Reactions (likes, hearts) take a separate path: each one isn't a message — the gateway or an aggregator collects them for 1 second and sends a count like "+1240 hearts".
- Batching. The gateway packs comments and sends them every 200–500 ms — this saves WebSocket frames and mobile battery.
On the write side: comments are stored by stream ID in a write-friendly database like Cassandra, and moderation (spam, abuse) is asynchronous — but a fast filter (banned words) runs before sending.
When the stream ends the load drops instantly, and the gateways shrink with autoscaling. At the start, though, there's an "everyone connects at once" wave — bring the gateways up in advance for it, autoscaling won't keep up.
Sign up to mark studied questions and save your progress.