Skip to content

How to design live comments (frontend)

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 under a live video or a busy post: other people's comments show up as they're written, and you can post your own. The web client turns on one idea. The server pushes comments down a one-way connection, each with an id, and the page absorbs that stream at its own pace: it keys and orders comments by id, draws them in batches, keeps in memory just what is close to the screen, and after a drop picks up from the newest id it holds. Posting stays a plain request, shown at once under a temporary id the browser makes.

Clarifying questions

AskWhy it mattersA typical answer

Is this under a live video, under an ordinary post, or both?

An ordinary thread gets a comment now and then and a timer could do. A live video can run for an hour with thousands of comments a second, which is where the design gets interesting.

Both, with one component. Design for the live video, since that is the hard case, and say where a slow thread would let you simplify.

How many comments arrive at peak, and how many are on screen at once?

The gap between those two numbers decides whether the list draws everything or has to choose.

Up to 5,000 a second on a busy stream, with about 20 visible at once.

How fast must a comment reach other viewers, and how fast must my own appear?

The first number picks how the client receives comments. The second decides whether you wait for the server before showing your own.

Within 200 milliseconds end to end for other viewers. The page should answer a click or keystroke within 200 milliseconds too.

Do I pick how comments arrive, and what does the server already offer?

If the server can push, the client gets to choose between a held-open connection and asking on a timer, and that choice drives the rest.

You own the client and can ask the server for a push connection, paged history and an endpoint for posting. How the server spreads comments to viewers is not yours today.

What do viewers do with older comments?

Scrolling back means paged history and a decision about what stays in memory. Replies and reactions would add a tree and more state.

Scroll back through them, 10 at a time. Reactions and replies to a comment are out of scope.

How long do people keep the page open, and on what kind of network?

An hour on a phone means connection drops are normal, not rare, so catching up after a drop is part of the main path.

An hour or more, often on a patchy connection that drops again and again, minutes apart.

Do I need to worry about spam, abuse or who may post?

Moderation and permissions add states and server round trips that would crowd out the core in a short round.

No. Filtering and permissions are out of scope.

The requirements menu

RequirementScopeWhy

See other people's comments appear as they are posted, without a refresh

Core

The feature itself. Without it this is a static comment thread.

Post a comment and see it at once

Core

Posting is the one thing a viewer does that must feel instant, and it decides how your own comment enters the list.

Scroll back through older comments

Core

It forces paged history and a choice about what the page keeps in memory.

Keep up when a stream gets more comments than anyone can read

Core

The busy stream is where a straightforward list falls over, so the design has to say what it drops.

Catch up on what was missed after joining a live video late

Optional

Take it if the interviewer raises it: it reuses the paged history and the resume path. Leave it otherwise, since late joiners mostly want the newest comments.

Share one connection across several open tabs

Optional

Take it if the product is a site people keep open in many tabs: over older HTTP there is a small per-site cap on open connections. Leave it for a single-tab app.

Pause the stream while the tab is hidden and resume on return

Optional

Take it when battery and data matter, as on phones. Leave it if time is short: it is a small addition on top of the resume path.

Retry or discard a comment that failed to post

Optional

Take it: a failed post that vanishes without a word is a bug people notice. It costs one state and two buttons.

Show who is typing

Out of scope

A second stream of short-lived events with its own fan-out. It distracts from the comments themselves.

Edit or delete your own comment

Out of scope

It changes a comment after it has been pushed to everyone, which is a reconciliation problem of its own.

Filtering spam and abuse

Out of scope

A server-side problem. The client shows what it is sent.

Who may post

Out of scope

Permissions belong to the server; the client shows the box or hides it.

Reactions and replies to a comment

Out of scope

Replies make the list a tree, and reactions add a second kind of update. Both are their own question.

How the server spreads comments to every viewer

Out of scope

Fan-out is the backend version of this question. The client needs a stream with ids, a history endpoint and an endpoint for posting.

Non-functional requirements

RequirementWhat it forces

A comment reaches other viewers within 200 milliseconds

Checking back on a timer is out unless the timer fires several times a second for each viewer (see the estimate), so the client holds a connection open and the server pushes. It also means the client's own handling of an arriving comment should be cheap, since the network's share of the budget is outside the client's control.

The list keeps up with thousands of new comments a second on a busy stream

Arrivals and drawing are separated: comments land in a buffer and the list commits them as a batch before the next repaint. Memory is bounded, so the oldest comments leave the page, and when the rate passes what anyone can read the list shows a chosen subset and says so.

A dropped connection catches up without gaps or repeats

Each comment carries a server id, the client stores comments by that id, and it remembers the newest id it has seen so a reconnection can ask for what came after. Storing by id is what makes a repeated comment harmless.

Reading older comments is never interrupted by new ones arriving

The list knows whether the viewer is pinned to the newest comment. When they have scrolled up, new arrivals go into the store but the viewport holds still and a small notice counts them. Older history loads by cursor, so a page request is unaffected by comments arriving at the other end.

Estimates

QuantityWorkingResultWhat it sizes

How long one comment is visible on the busiest stream

20 comments on screen / 5,000 arriving a second = 0.004 s = 4 ms per comment before it is pushed off

About 4 ms

Why the busiest streams show a subset: nobody reads a line in 4 ms, so completeness has stopped being useful and the list should pick a readable sample and say it is doing so.

Requests a second per viewer if a timer were to meet the latency target

1,000 ms in a second / a 200 ms target = 5 requests a second from each viewer, most returning nothing when little is happening

5 requests a second per viewer

Why the client holds a connection open instead of asking on a timer: a timer of a few seconds misses the 200 ms target, and a timer fast enough to meet it makes a request storm.

Comments arriving during an hour of a busy stream

5,000 a second × 3,600 seconds in an hour = 18,000,000 comments

About 18 million comments an hour

Why the page keeps a bounded window of recent comments rather than the whole stream: holding them all in memory and in the DOM is out, and older comments are a page request away anyway.

Core entities

EntityWhat it holds

Comment

One comment as the page holds it, whether it came from the server or was just typed here.

Client store

The page's single copy of comments for this video, keyed by id and kept in server order.

Live connection state

What the page knows about the stream it is listening to.

List view state

Where the viewer is in the list and what the list is holding back from them.

The API

EndpointNotes

Live connection, server to client: new comments, each with an id, for one video or post

In this design an EventSource on a stream URL for the video. Each event's data is one comment as JSON and its id field is the comment's server id, which the browser records as its last event id. The connection is one-way, so posting goes over a separate request.

GET /videos/{id}/comments?before={comment_id}&limit=10 → { comments, next_cursor }

Older history by cursor. before is the oldest id the page holds and next_cursor is where the following page starts. Because the cursor is an id and not an offset, new comments arriving at the other end don't shift the page.

POST /videos/{id}/comments { client_id, text } → comment

client_id is made in the browser before the request, so a retry of the same submission carries the same key and the server can treat it as one comment. The reply is the comment with its server id; the same comment also arrives on the live connection, where the page matches it by client_id.

Live connection resume: the last comment id seen → the comments missed since

The browser's EventSource restarts a closed connection on its own after the server's retry time; in this design the resume request carries the last id the page saw and the server replays what came after it. When the gap is longer than the server keeps, this design has it answer with a marker instead of a replay, and the page then catches up through history pages, working backward from the first live id it gets until the pages overlap what it already has.

The high-level design

pushrepliespostcommentspagesServerLive connectionPage and postrequestsComment boxComment list

Step 1 of 4

A connection, a list and a box

Start with the smallest page that works. The server pushes each new comment down a live connection, and the list appends it. A comment box sends a request, and the reply is appended too, as is the older history when the viewer scrolls up. This shows the whole flow, and it has three faults. The reply and the pushed copy of your own comment both land, so you see it twice. Each arrival is a separate append and redraw. And the list moves under anyone who is reading.

push, resumerepliesclient idburstbatch, samplepages, ownordered idsunseenjumpServerLive connectionPage and postrequestsComment boxFrame bufferComments by idComment listNew commentsnotice
The whole design

What breaks first

The deep dives

Comments arriving live. A viewer keeps a live video open for an hour on a connection that keeps dropping, minutes apart, while comments keep coming. How does each new comment get to their screen, exactly once and in the right order?

A concept that helps here: WebSockets, server-sent events and pollingA concept that helps here: Reconnect and resumeA concept that helps here: Coordinating across tabs

More comments than anyone can read. A famous person goes live and comments arrive by the thousand each second, more than anyone could read. What does your list do with them, and what does the viewer give up?

A concept that helps here: Batching UI updatesA concept that helps here: List virtualizationA concept that helps here: Backpressure and load shedding

Reading back and posting your own. Someone scrolls up to find an older comment, then posts a reply while hundreds of new comments keep landing. What do they see at each step?

A concept that helps here: Optimistic updates and rollbackA concept that helps here: Pagination and infinite scrollA concept that helps here: Sync queues and client idempotency

Concepts this guide uses

Sources

  1. MDN: Using server-sent events
  2. MDN: Window: requestAnimationFrame() method
  3. MDN: Overview of scroll anchoring
  4. MDN: Broadcast Channel API
  5. web.dev: Interaction to Next Paint (INP)
  6. web.dev: Virtualize large lists with react-window
  7. techinterview.org: Build a comments thread, nested replies, pagination, optimistic updates
  8. Copilot Interview: Design a live comments system (Facebook Live style)
  9. dev.to: Design a live video comments system, the system design interview walkthrough
  10. interviewing.io: Facebook system design mock, design live comments
  11. FrontendLead Discuss: Virtual onsite rejection from Meta frontend role
Sign in to report a mistake