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
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Comments arriving live
- More comments than anyone can read
- Reading back and posting your own
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
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
| Ask | Why it matters | A 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. |
Non-functional requirements
| Requirement | What 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
| Quantity | Working | Result | What 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
| Entity | What 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
| Endpoint | Notes |
|---|---|
| In this design an |
| Older history by cursor. |
|
|
| The browser's |
The high-level design
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.
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?
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?
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?
Concepts this guide uses
Sources
- MDN: Using server-sent events
- MDN: Window: requestAnimationFrame() method
- MDN: Overview of scroll anchoring
- MDN: Broadcast Channel API
- web.dev: Interaction to Next Paint (INP)
- web.dev: Virtualize large lists with react-window
- techinterview.org: Build a comments thread, nested replies, pagination, optimistic updates
- Copilot Interview: Design a live comments system (Facebook Live style)
- dev.to: Design a live video comments system, the system design interview walkthrough
- interviewing.io: Facebook system design mock, design live comments
- FrontendLead Discuss: Virtual onsite rejection from Meta frontend role