← Create New Paste

System Design Interview: A Threaded Comment System Like Reddit

3 views
"Design the comments section for a large discussion site" sounds small, but it touches tree storage, ranking, pagination, and hot-spot traffic. Here is a structure you can use in a 45-minute system design round. 1. Clarify requirements Functional: users post a comment on a post or reply to another comment, nesting can go deep, comments can be upvoted and downvoted, and readers see a sorted, collapsible tree. Edits and soft deletes ("[deleted]" placeholders) must keep the tree intact. Non-functional: reads vastly outnumber writes (often 100:1), a viral post can receive thousands of comments per minute, and the first screen of comments should load in under 200 ms. 2. Data model: how do you store a tree? Offer the options and pick one: - Adjacency list: each comment stores parent_id and post_id. Simple writes, but loading a deep thread needs recursion or many queries. - Materialized path: store a path string like "0001.0007.0003". A prefix query returns a whole subtree and sorting by path gives display order. - Closure table: a separate table with every ancestor-descendant pair. Flexible but write-heavy. A practical answer: keep parent_id plus post_id and a depth field, and load comments per post in bulk. Most posts have fewer than a few thousand comments, so you can fetch the top-level page and build the tree in the application layer. 3. Ranking Sorting by raw score buries new comments. Explain a confidence-based sort (lower bound of the Wilson score interval on upvote ratio) for "best", a time-decayed score for "hot", and plain timestamp for "new". Scores change constantly, so compute them asynchronously from vote events rather than on every read. 4. Votes at scale Votes are the heaviest write. Record each vote in a votes table keyed by (user_id, comment_id) to prevent double voting, publish an event to a queue, and let workers aggregate counts into the comment row in batches. Displayed counts can lag a few seconds; say that this trade-off is acceptable. 5. Reading and pagination Return the top N root comments by the chosen sort, each with its first few replies, and a "load more replies" cursor for deeper branches. Cache the rendered first page per post and sort mode in Redis, invalidating or refreshing it on a short TTL. For viral posts, serve slightly stale cached pages rather than hitting the database on every refresh. 6. Storage and sharding Shard comments by post_id so a whole thread lives on one shard and loads with one query. The risk is a hot post overloading one shard; mitigate with read replicas and the cache layer, since the hot traffic is mostly reads. 7. Moderation and deletes Soft delete keeps the node so children still render. Run spam and toxicity classification asynchronously and hide flagged comments pending review. Wrap up with trade-offs Close by naming what you would monitor (p99 load time, vote lag, cache hit rate) and what changes at 10x scale. Getting real-time support System design rounds reward structure more than memorized answers. If you want help staying organized in the moment, Linkjob AI (https://linkjob.ai) offers real-time help during live job interviews and technical interviews, suggesting talking points such as trade-offs you have not covered yet, so you can keep the discussion moving.
Created Nov 17, 2026 15:29 Expires Nov 17, 2027 15:29