Social & feeds
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 first act plays here. 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
- 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.
- Then the two write lanes along the bottom: COMMENT (58 a second) and VOTE (1,160 a second, four writes each).
- 1 · Requirements - sorts, depth, freshness of scores.
- 2 · Capacity estimation - the amplification and the origin ratio - then 3 · Storage estimation.
- 4 · Availability, 5 · How it works, 6 · Deployment plan.
- 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
Available on Tier Slate
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.
- 5written pages
- 8walkthrough steps
- 5tables28 columns
5 pages behind this one
- Capacity estimation
- Storage estimation
- Availability
- How it works
- Deployment plan