Skip to content

How to design Facebook Live comments

The first sections are free. The deep dives, level expectations and quiz are part of Pro.

On this page
Asked at
Practice this question

The problem

Live comments put a scrolling conversation under a live video: anyone watching can post, and the rest of the audience sees it within a fraction of a second. The design turns on one asymmetry. A comment is written once and read by the whole audience of that video, so posts can travel as ordinary HTTP requests while new comments are pushed down long-lived connections. The work is in placing those connections so each server handles the videos its own viewers watch, and in deciding what happens when one video's crowd and comment rate outgrow any push at all.

Clarifying questions

AskWhy it mattersA typical answer

How big can one video's audience get, and how many comments a second does it draw?

The answer tells you whether a push to each viewer is even possible on the biggest video, and it sets the number of connection-holding servers.

Millions of live videos at once. Most are small, but the biggest draws millions of viewers and up to 5,000 comments per second. Plan on one server holding about 100,000 open connections.

How quickly must a comment show up on other viewers' screens?

A target of a few seconds allows polling; a target well under a second makes polling far more expensive than a push, as the estimate shows.

Under 200 ms for ordinary videos. For the very biggest, a slower path of 1 to 2 seconds is acceptable if it keeps the feature up.

Does a viewer who joins late see the comments posted before they arrived?

It decides whether comments are stored durably and read back by page, or merely fanned out and forgotten.

Yes. A viewer scrolls back through earlier comments, so they are stored and paged by comment id.

Do two viewers have to see the same comments in the same order?

Strict ordering across servers costs latency and coordination; a feed that tolerates small differences is much cheaper.

No. Availability matters more than consistency. Slight differences in order between viewers are fine, as long as the feed keeps moving.

What are the clients, and how often do they drop their connection?

Phones on cellular networks drop connections often, so reconnect and catch-up behavior is part of the core design rather than an edge case.

Phones and web browsers. Assume a connection can drop and come back within a minute, and design for that as the normal case rather than an exception.

Are replies, reactions, moderation and the video itself in scope?

Each is its own system. Naming them and parking them keeps the round on delivery, which is where the difficulty is.

No. Replies, reactions, spam and abuse filtering, and the video stream itself belong to other teams or a later round.

The requirements menu

RequirementScopeWhy

Post a comment on a live video

Core

The write path. It is small next to the read path, which is what lets it stay an ordinary HTTP request.

See new comments while watching

Core

The read path and the whole difficulty: one post becomes a delivery to the entire audience, within a fraction of a second.

See comments posted before joining

Core

Forces durable storage and paging by comment id, since the list grows while the viewer scrolls through it.

Catch up on comments missed while a connection was down

Optional

Take it: phones drop off constantly, and the catch-up endpoint is already in the API. Bounding how far back the catch-up reaches is the part worth discussing.

Show a viewer their own comment immediately

Optional

Take it if the mega-stream discussion comes up: on the slower snapshot path a commenter would otherwise wait a second or two to see their own words.

Sample comments on the busiest streams instead of delivering all of them

Optional

Take it when the interviewer raises the biggest videos. At 5,000 comments per second nobody can read the feed anyway, so sampling trades nothing the viewer would notice.

Keep comments available after the broadcast ends

Optional

Cheap to promise because comments are already stored by video and comment id. Leave the replay-player work to the video team.

Delete or edit a comment after posting

Out of scope

A delete has to chase a comment that may already sit on a million screens. Mention that a tombstone event could travel the same push path, then park it.

Replies to comments

Out of scope

Threads change the data model and the feed layout. Name it and move on.

Reactions

Out of scope

Reactions arrive at a far higher rate than comments and are usually aggregated into counts rather than delivered one by one, so they deserve their own design.

Spam and abuse filtering

Out of scope

Real and important, but a classifier in the write path is a separate system. Say where it would sit, before the comment is stored, and stop.

The video stream itself

Out of scope

Owned by another team. The one dependency to name is the video id, which keys everything in this design.

Non-functional requirements

RequirementWhat it forces

Millions of live videos at once, thousands of comments a second on one video

Work has to be split by video from top to bottom: the table keyed by video, the pub/sub channels keyed by video, and viewers of one video steered onto a few servers so no server takes in the comments of videos none of its viewers are watching.

Availability over consistency: eventual consistency is fine

The push path can be fire-and-forget: a comment that reaches one viewer a moment later than another, or in a slightly different order, is acceptable. What is not acceptable is a feed that stalls, so catch-up after a drop and a fallback path for the biggest streams matter more than ordering.

A comment reaches viewers in under 200 ms

Polling is out: a poll interval that meets the target means several requests a second per viewer, nearly all of them empty. New comments are pushed over a connection the viewer already holds, and the path from post to push has few hops: one write, one publish, one fan-out.

Estimates

QuantityWorkingResultWhat it sizes

Time each comment stays visible on a mega-stream

A phone shows about 20 comments at once. At 5,000 comments per second, the whole screen turns over 250 times a second: 20 ÷ 5,000 = 0.004 seconds

About 4 ms a comment

Whether to deliver all of them: nobody reads a comment that is gone in 4 ms, so sampling costs the viewer nothing.

Cost of polling at the latency target

To see comments within 200 ms a client would poll once each 200 ms: 1,000 ms ÷ 200 ms = 5 requests a second a viewer. For the 100,000 viewers one server could instead hold open: 100,000 × 5 = 500,000 requests a second

500,000 requests a second for 100,000 viewers, nearly all returning nothing on a quiet video

Why the design pushes instead of polls.

Size of a comment, stored and on the wire

About 4 words of text, say 25 bytes, plus a 64-bit comment id, a video id, an author id and a timestamp: about 100 bytes stored. As a JSON event with field names and a display name it roughly doubles, say 200 bytes. One mega-stream hour: 5,000 × 3,600 seconds × 100 bytes ≈ 1.8 GB

About 100 bytes a row, about 200 bytes an event; about 1.8 GB an hour for the biggest video

The storage write rate and the bytes each pushed event costs. An assumption, not a measurement.

Push work for one server holding viewers of a mega-stream

5,000 comments per second × 100,000 connections = 500 million event writes a second. At 200 bytes an event: 500 million × 200 bytes = 100 GB a second from one machine

500 million writes a second, about 100 GB a second, per server

Why a per-viewer push does not survive the biggest video, and why those streams move to sampling and CDN snapshots.

Servers to hold a mega-stream's audience

1 million viewers ÷ 100,000 a server = 10 servers; 5 million ÷ 100,000 = 50 servers

Roughly 10 to 50 connection-holding servers for a single video

How many servers subscribe to one video's channel even when viewers are co-located, and why a mega-stream's viewers do not fit on one box.

Comments missed during a drop

On the biggest video, one minute offline: 60 seconds × 5,000 = 300,000 comments; five minutes: 300 seconds × 5,000 = 1.5 million. On a quiet video posting 1 comment a second: 60 in a minute, 300 in five minutes

From a few hundred to over a million, against a catch-up page of 100

The replay window: bound it by time on ordinary videos and by count on the biggest, and show a gap marker rather than a million-comment replay.

Snapshot size and what the CDN carries

200 comments × 200 bytes = 40 KB a snapshot. 1 million viewers fetching it once a second = 40 GB a second served from CDN edges; the origin sees about one fetch an edge a second

About 40 KB a snapshot, about 40 GB a second across the edges for a million viewers

The edge load for the snapshot path and why the origin stays small: each edge pulls once and serves many.

Hysteresis band for the mode switch

Switch to snapshots above 100,000 viewers, or above 500 comments per second. Switch back below 80,000 viewers (100,000 × 0.8), or below 400 comments per second (500 × 0.8), and, in this design, after the lower figures have held for 60 seconds

Up at 100,000 or 500; down at 80,000 or 400, held for a minute

The gap between the two thresholds, so a video hovering near the line does not switch modes several times a minute.

Core entities

EntityWhat it holds

Comment

One row per post: who said what, under which video, and a time-ordered id that doubles as the cursor and the stream event id.

Live video

Owned by the video team; this design reads its id, its broadcaster and whether it is still live.

User

The commenter and the viewer. The design needs little more than an id, a display name and a verified flag.

Viewer connection

Not a table: an in-memory record on the realtime server that holds the open stream.

The API

EndpointNotes

POST /comments/{live_video_id} { message }, the user from the token

An ordinary request, not a message on the stream. The service writes the row, publishes the event and returns the new comment id so the client can show the comment at once and later drop its own copy when it arrives on the stream. Rate-limit per user.

GET /comments/{live_video_id}?cursor={last_comment_id}&page_size={n} → older comments

Cursor pagination on the comment id, not an offset: new comments keep arriving while the viewer scrolls, and an offset would shift under them and repeat or skip rows. Served by the comments table, keyed by video and id.

GET /comments/{live_video_id}/stream → server-sent events of new comments

A long-lived HTTP response of text/event-stream. Each event carries the comment id in its id field, so the browser's own reconnect sends it back as Last-Event-ID. The load balancer routes this call by hashing the video id.

GET /comments/{live_video_id}?since={last_comment_id}&limit=100 → catch-up after a reconnect

Returns what was posted after the id the client last saw, newest page capped at 100, from the recent-comments cache. If the gap is larger than the cache holds, the response says so and the client shows a gap marker rather than asking for history.

The high-level design

insertpostViewerComment serviceDatabase

Step 1 of 4

Post, store and page

Start with the two plain requests. A comment service takes POST /comments/{live_video_id}, assigns a time-ordered id, inserts a row keyed by video and id, and returns the id. The same service answers the history call with cursor pagination on that id. For now, viewers learn about new comments by calling the history endpoint again on a timer. That is enough to post and to read, and it is where the first failure sits: at the 200 ms target a viewer would ask 5 times a second, which is 500,000 requests a second for the 100,000 viewers one server could otherwise hold open, nearly all returning nothing.

poststreaminsertpublishdeliverappendreplaysnapshotpollViewerBalancerComment serviceRealtime serversDatabasePub/subRecent cacheCDN edgeMega viewer
The whole design

What breaks first

The deep dives

Pushing comments to viewers. A viewer posts a comment. How does it reach the other viewers of that video in under 200 ms, which channel carries it down, and what do the client and server do when a phone drops off for a minute and comes back?

A concept that helps here: WebSockets, server-sent events and pollingA concept that helps here: Reconnect and resumeA concept that helps here: Delivery semantics: at most, at least, exactly once

Routing comments to the right servers. Viewers of one video are scattered over many realtime servers, and millions of videos share those servers. How does a comment find the servers that hold its viewers without each server processing the whole platform's comments, and what happens when a server with 100,000 of them dies?

A concept that helps here: Routing messages between stateful serversA concept that helps here: Consistent hashingA concept that helps here: Load balancing at layer 4 and layer 7

Mega-streams. One video has millions of viewers and 5,000 comments per second. What does each viewer actually receive, through which path, when do they see their own comment, and how does the system move a video onto that path as it crosses 100,000 viewers and back off it later?

A concept that helps here: CDNs and edge cachingA concept that helps here: Backpressure and load sheddingA concept that helps here: Hot keys and hot partitions

Concepts this guide uses

Sources

  1. MDN: Using server-sent events
  2. WHATWG HTML Standard: Server-sent events
  3. AWS Builder Center: Resilience considerations for implementing server-sent events over HTTPS
  4. Redis documentation: Pub/sub
  5. Redis documentation: XADD
  6. NGINX documentation: ngx_http_upstream_module (hash directive)
  7. MDN: Cache-Control
  8. LinkedIn Engineering: Instant Messaging at LinkedIn (persistent SSE connections at scale)
  9. InfoQ: Streaming a Million Likes/Second: Real-Time Interactions on Live Video (LinkedIn)
  10. InfoQ podcast: Akhilesh Gupta on the architecture of LinkedIn's real-time messaging platform
  11. Wikipedia: Hysteresis
  12. Stacksync: PostgreSQL keyset pagination vs offset
  13. Copilot Interview: Design a Live Comments System (Facebook Live-style)
Sign in to report a mistake