#38 WebSocket/Pub-Sub Scale
Facebook Live Comments
Show a stream of comments on a live video to millions of viewers, where one popular broadcast receives thousands of comments a second.
What is being asked
Design live comments. Viewers post comments that everyone else watching the same video sees within a second or two. Some videos have a dozen viewers, a few have millions.
Requirements
Functional
- Post a comment; receive new comments for the video being watched.
- Load recent comments when joining.
Non-functional
- Millions of concurrent connections.
- Low latency; strict ordering is not required.
- A very popular video must not overwhelm viewers or servers.
Design
- A viewer holds one long-lived connection to an edge server and tells it which video they are watching.
- Posting goes through the comment API, which stores the comment and publishes it to the video's channel.
- Each edge server subscribes to the channels of the videos its own viewers are watching, and forwards messages to those connections.
- For a hot video, a dispatcher limits what each viewer receives to a readable rate, a few comments per second.
Key decisions and trade-offs
- Fan-out through pub/sub, not per-viewer queues. A comment is published once per video; each edge server delivers it to its local subscribers. Cost grows with servers, not with viewers.
- Nobody can read 5,000 comments a second. For hot streams, sample: show each viewer a subset, preferring friends and high-quality comments. This is a product decision that also saves the system.
- Server-sent events or WebSockets. Comments flow mostly one way, so SSE over HTTP is enough and simpler to operate; WebSockets are needed only for two-way features.
- Best effort. A lost comment is acceptable. This removes the need for acknowledgements and per-viewer state.
Follow-up questions
- How do you reconnect five million viewers when an edge server tier restarts?
- How would you remove abusive comments already delivered?
- How do you show the commenter their own comment instantly?
- How would you scale the pub/sub layer itself for one enormous channel?