How to design nested 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
- How the thread is shaped and drawn
- Posting a reply, and the server saying no
- A thread far longer than the page
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
- ServiceNow
The problem
Comment threads beneath an article: replies nested to any depth, more of a long thread loaded on request, and posting, editing and deleting your own comments. The answer turns on two decisions. The client holds a single copy of each comment in a map keyed by id, and for each parent the ids of its children in order plus a paging cursor, so a branch five levels down can grow without touching the rest of the tree. And a reply someone posts appears in the thread at once, marked as posting, while the server catches up.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
Does the server send the thread already nested, or as pages of comments where each one names its parent? | It decides how the client stores the thread. A nested tree is easy to draw once but hard to add to later; a flat page with parent ids needs a small amount of client work and then pages anywhere. | Flat pages. |
How deep can replies go, and is this read on phones? | Unlimited depth on a narrow screen runs out of room for indentation long before the thread runs out of replies, so the drawing needs a depth limit and somewhere for the rest to go. | Any depth, on phones and desktops. Beyond a chosen depth the client stops indenting and offers a link to the rest of the branch on a page of its own. |
How large do threads get, and how fast must the page react? | The size tells you whether the whole thread can ever be drawn at once; the response budget tells you whether a reply can wait for the server before it shows. | Threads of 10,000 comments, 20 top-level comments in each page, and a click or keystroke answered within 200 milliseconds. |
Do replies from other people need to appear while someone is reading? | Live arrivals change the data layer (a subscription) and the reading experience (content moving under the reader). It's a different design if it's in scope from the start. | Not in the first pass. Keep a place in the design for a notice that new replies are waiting, in case the question comes up later. |
What happens to the replies under a comment that is deleted? | If a deleted comment vanishes, its replies lose their place in the tree. Keeping a placeholder changes the Comment entity and the delete flow. | They stay readable. The deleted comment remains as a placeholder with its body removed. |
Who can edit or delete, and is moderation part of this? | Edit and delete for your own comments is a client flow; moderation, reporting and ranking are a different product surface. | People edit and delete their own comments. Moderation, reporting, ranking, mentions and rich text are out of scope. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
A posted reply shows within 200 milliseconds, before the server has answered | The reply is added to the local store at once with an id the client made, drawn as posting, and the request goes out after. The server's answer later confirms it or marks it failed. The 200 milliseconds is the Core Web Vitals threshold for a good Interaction to Next Paint, measured at the 75th percentile, so it covers painting the reply, not the round trip. |
A thread of 10,000 comments is never drawn all at once | Top-level comments arrive 20 a page by cursor and replies load per parent on request. The top-level list draws the comments near the screen and nothing further, and one comment redraws when its own entry in the store changes rather than when any part of the thread changes. |
Deleting a comment leaves the replies under it readable | A delete keeps the comment's node in the store with a deleted flag and an emptied body, so its child list stays attached and its replies keep their indentation and their parent. |
Showing and hiding replies works by keyboard and is announced to screen readers | The control that loads or hides replies is a real button carrying |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Pages to fetch a whole large thread | 10,000 comments / 20 top-level comments per page = 500 pages, if the thread were all top-level comments fetched through this API; a whole-thread fetch that includes replies would move that data into fewer, larger responses | About 500 requests, and the whole thread in memory, before the page is complete | Why no design here fetches and draws the whole thread. The first page is what a person reads first; the rest loads as they scroll. |
Share of a large thread the first page shows | 20 top-level comments / 10,000 comments = 0.2% | 0.2% of the thread on the first load | The page's state is a small window onto the thread, so the store must cope with most of the tree being absent and arriving in any order. |
Clicks to read a comment with 20 replies | 6 replies shown before the rest are tucked away; 20 - 6 = 14 behind the button; at 5 a click that is three clicks (5, 5 and 4) | Three clicks, each a request for one parent's next page | The button's label is a subtraction, reply count minus children loaded, and it has to stay right as both numbers change. |
Core entities
| Entity | What it holds |
|---|---|
Comment | One comment as the server returns it, stored once however many lists point at it. |
Client store | The thread as the client knows it: a map of comments and, per parent, an ordered child list with a cursor. |
Pending comment | A reply the person has posted that the server has not yet accepted. |
Thread view state | What the page is showing right now, apart from the data itself. |
The API
| Endpoint | Notes |
|---|---|
| A page of top-level comments. Each comment names its parent (null here) and carries a reply count, so the thread can show how many replies wait behind each button before loading any. |
| A page of direct replies under one comment, with its own cursor. Any branch pages on its own, which is what lets a comment deep in the tree load more without refetching above it. |
|
|
| Edits the body. The client keeps the old body until the response arrives; on refusal it restores the old text and says so. |
| Marks the comment deleted. The client keeps the node, clears its body and shows a placeholder, so replies under it stay where they are. |
The high-level design
Step 1 of 4
The whole thread, nested, drawn by recursion
Start with the shortest path to a working page: the server returns the whole thread as nested objects, and a single recursive component renders a comment and calls itself for each child. It shows the data flow end to end, and it already has the two faults the rest of the design answers: the thread is drawn in full on open however big it is, and a page of replies loaded later for one branch has to be merged by rebuilding the path from the root to its parent.
What breaks first
The deep dives
How the thread is shaped and drawn. A comment five replies deep gets a new reply while its branch is half loaded, and the reader has just asked for more replies above it. How does your client hold the thread, and how does that reply get drawn in the right place?
Posting a reply, and the server saying no. Someone writes a long reply on a train, presses post, the request times out, and a second try is refused. What do they see in the thread at each step, and what happens to their text?
A thread far longer than the page. An article goes viral and gathers ten thousand comments in an afternoon, many with long chains of replies. What does the page load and draw when someone opens it, and what happens as they read on?
Concepts this guide uses
Sources
- techinterview.org: Build a comments thread, nested replies, pagination, optimistic updates
- Frontend Digest: Design a comments thread
- Hacker News API: items, parents and kids
- Redux docs: Normalizing state shape
- CSS-Tricks: Styling comment threads
- YouTube Data API: CommentThreads list
- Disqus blog: Introducing collapsed replies
- WAI-ARIA Authoring Practices Guide: Disclosure (show and hide) pattern
- web.dev: Web Vitals
- TanStack Query docs: Optimistic updates
- Stripe blog: Designing predictable APIs with idempotency keys
- TanStack Virtual docs: Introduction
- LogRocket blog: How to speed up long lists with TanStack Virtual
- React docs: memo
- JavaScriptBit: Frontend system design, nested comments