# Comment trees - one range scan, and four writes per vote

> How a whole comment tree comes back in one range scan, and why every vote rewrites what it cached. The rendered tree is cached per (post, sort) and every vote…

Source: https://tierslate.com/templates/reddit-comments

Social & feeds · System design · deep · 8 step walkthrough · 5 tables

How a whole comment tree comes back in one range scan, and why every vote rewrites what it cached.

**Own this board: 800 credits.** The first act plays on the page. Owned once with credits, never a subscription.

## What this board gets wrong on purpose

The tension: The rendered tree is cached per (post, sort) and every vote invalidates all three entries for that post - so on the thread that is being voted on four hundred times a minute, which is precisely the thread being read by a hundred thousand people, the cache is destroyed faster than it can be filled and every request rebuilds the tree from scratch. The cache works beautifully on threads nobody is looking at. The second hole is the tail: the tree is cut at 200 nodes and 8 levels, and everything below that is behind a "continue this thread" endpoint that is uncached, unranked and unmeasured - so the deepest, most argued-over part of every thread is served by the slowest path in the system.

## Practise this as an interview

Take Comment trees - one range scan, and four writes per vote as a timed system design interview on a blank board - on your own, with the requirements to check yourself against, or with a candidate you invite.

## Start here

### Why this design

Nested comments look like a tree problem and are really a read-path and write-amplification problem. Interviewers use Reddit-style threads to see whether you can store a tree so that a whole thread comes back in one query instead of a recursion, rank siblings and cut the tree for display, and notice that a vote - not a comment - is the real write workload. Teams running discussion products meet the same collision this board leaves in: every vote invalidates the cached tree that thousands of people are reading.

### What you will learn

- How a**materialised path**turns "load the thread" into**one range scan**on (post, path), and what it costs when a subtree moves.
- Why the tree is ranked per level and**cut at 200 nodes and 8 levels**, and what "continue this thread" really is.
- Why the cache key is**(post, sort)**and why most readers never reach the origin - 90% of logged-out traffic is absorbed at the CDN.
- Why**one vote is four writes**(vote row, comment score, author karma, post hot rank): 100M votes a day become 400M writes, twenty times the comment workload.
- Why the headline 28 : 1 read : write ratio is the wrong number to size on -**2.1 : 1 at the origin**is the real one.

### How to read this board

1. Press Play and follow the walkthrough:**READ A THREAD**across the top, then**BUILD THE TREE**beneath it (what a miss costs), with the**STORES**column on the right.
2. Then the two write lanes along the bottom:**COMMENT**(58 a second) and**VOTE**(1,160 a second, four writes each).
3. **1 · Requirements**- sorts, depth, freshness of scores.
4. **2 · Capacity estimation**- the amplification and the origin ratio - then**3 · Storage estimation**.
5. **4 · Availability**,**5 · How it works**,**6 · Deployment plan**.
6. Finally**THE DATA MODEL**below the band, and then the argument this board leaves open on purpose: on the hottest thread the votes destroy the cache faster than it can be rebuilt, and the deepest replies are served by the slowest, uncached path.

## 1 · Requirements

### Clarifying questions to ask first

- How**deep**can a thread go, and may we cap it? A cap turns an unbounded recursive problem into a bounded one, and the product usually accepts it.
- Are**scores exact or approximate**? "Approximately 4.2k" is a product decision that buys an enormous amount of engineering freedom.
- Must a user see**their own vote and their own comment immediately**? Almost always yes, and it is the one consistency promise worth keeping.
- How much traffic is**logged out**? On a link aggregator it is typically 70-80%, and that traffic is identical for every viewer, which means a CDN can absorb it. This single answer changes the origin load by 4x.
- Can a moderator**move or remove a subtree**, and what happens to its children? This decides whether materialized paths are viable.
- Is**vote manipulation**in scope? Storing every individual vote is required to prevent double-voting, and it is also the largest table in the system.

### Functional

- Read a thread: a ranked, paginated, nested tree of comments.
- Sort by best, top or new; each is a different tree over the same rows.
- Post a comment as a reply to the post or to another comment.
- Vote on a comment or a post, once per user per thing, changeable.
- Collapse and expand subtrees; load more of a deep thread on demand.

### Non-functional

- **Thread read p99 under 300 ms**at the origin, and under 50 ms at the edge for logged-out readers.
- **A comment is visible to its author immediately**and to everyone else within 2 seconds.
- **Vote counts may be up to 30 seconds stale**and may be approximate. This is stated as a requirement, not conceded as a bug.
- **99.95% availability**on reads; writes may be less available.
- A user must never be able to vote twice, even across a failover. This is the one hard consistency requirement on the board.

### Explicitly out of scope

Moderation tooling, spam detection, the ranking model itself, notifications, and the subreddit feed. The ranker is one box.

## The rest of this board

This page publishes the question. The answer - 5 more written pages, an 8-step narrated walkthrough and a 5-table schema - is the board itself, and it opens in Tierslate.

- 5 written pages
- 8 walkthrough steps
- 5 tables (28 columns)

5 pages behind this one:

- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan

## Related system design templates

- [Social news feed system design](https://tierslate.com/templates/news-feed) - Fan-out on write against fan-out on read, and when each loses.
- [Media feed system design](https://tierslate.com/templates/instagram-feed) - Why the feed serves URLs and the edge serves bytes, and who actually pays for the pixels.
- [The professional graph system design](https://tierslate.com/templates/linkedin-connections) - Why a degree badge on every name is a graph held in memory and searched from both ends, not a join.
- [The social graph system design](https://tierslate.com/templates/social-graph) - Why every follow is written twice on two shards with no transaction between, and what that drift costs.
- [Home timeline system design](https://tierslate.com/templates/twitter-timeline) - Why one post is fanned out into hundreds of precomputed timelines, and why the celebrity cannot be.
- [Blog platform system design](https://tierslate.com/templates/blog-platform) - Why a blog is a read-heavy system where the cache is the product, and what publishing has to invalidate.
- [Link previews system design](https://tierslate.com/templates/link-preview) - Why unfurling a pasted URL is a server-side request forgery problem first and a caching problem second.
- [Reviews and ratings system design](https://tierslate.com/templates/reviews-ratings) - Why a star rating is an aggregate maintained on write, and how fake reviews are held back before they count.
- [Content moderation system design](https://tierslate.com/templates/content-moderation) - Why moderation is a fast automatic gate plus a slow human queue, and what is shown while it decides.
- [Stories system design](https://tierslate.com/templates/ephemeral-stories) - Why expiry is a read-time filter plus a background sweep, and why the viewer list is the expensive part.

All 100 system design templates: https://tierslate.com/templates (Markdown: https://tierslate.com/templates.md)
