AlgoViz

Design Reddit

medium 50 min

A service like Reddit: communities, posts, nested comments and votes.

Video lesson · Chapter 1 of 5

A community page and one person's vote

0:00 / 3:22

Theory

Read or listen first. About 2 min.

What it is

A community discussion site lets people create topic communities, post in them, reply in nested threads and vote things up or down. Votes and time decide what others see first.

This is an exercise inspired by Reddit. It does not describe how that company runs its site today. Where we use a published Reddit formula or API rule, we link to it.

Why it exists

No editor can read a million posts a day. Voting lets a crowd sort them, and time decay gives new posts a chance.

The same three problems appear in many products: one vote for each person, counters that many people change at once, and lists ordered by a score that should favour the new.

A short history

  1. 1927

    Edwin B. Wilson publishes the score interval later used to rank items with few votes.

  2. October 1997

    Slashdot launches, an early social news site.

  3. 2004 to 2005

    Digg is founded in November 2004. Steve Huffman and Alexis Ohanian launch Reddit in June 2005.

  4. 15 October 2009

    Reddit announces a new comment sorting system in a guest post by Randall Munroe. It uses a confidence interval, the Wilson score.

  5. 9 November 2017

    The public reddit code repository is archived. Its sorting formulas can still be read there.

Words to know

Community
A topic group. Reddit calls it a subreddit.
Vote state
What one person chose on one item: up, down or nothing.
Delta
The change a new choice makes to a score, such as minus two from up to down.
Hot score
A number from votes and creation time that orders a feed.
Wilson score
A careful estimate of the share of up votes that is low when there are few votes.
Materialised path
A stored list of ancestors, such as 1/5/23, that finds a whole branch.
Fullname
An ID with a type prefix. In Reddit's API, t1 is a comment, t3 a link and t5 a subreddit.
Karma
A person's reputation from votes received.

Solve it like an interview

New to it? Start with Approach. Revising? Go straight to the mock.

  1. 1ApproachWhat this question tests, where it gets hard, and how to spend the time.
  2. 2ScopeAsk what changes the design, then commit to a short list.
  3. 3NumbersWork out a number when the answer changes what you build.
  4. 4API and dataThe contract first, then the data that serves it.
  5. 5DesignStart with the smallest system that works. Change it only when something breaks.
  6. 6Deep divesCompare the options before you open them, then say which you would pick.
  7. 7Explain your choicesAnswer first. Then compare your reasoning.
  8. 8By levelThe same question is judged differently for mid, senior and staff.
  9. 9MockAnswer against the clock, then score yourself.

Understand it

Story

Beans at the school fair

Jars, beans and the front of the fair
Cakesopened 9:004Kitesopened 9:003Puzzlesopened 11:002

Scope

What are we building?

People browse, join communities, post text, links, images or video, reply at any depth and vote. Feeds can be Hot, New, Top, Rising or Controversial.

  • Browse

    One community, everything, or your own mix.

  • Vote

    Up, down or nothing on posts and comments.

  • Discuss

    Nested replies with a useful order.

  • Moderate

    Rules, removal, reasons and appeals.

Similar applications

NameKnown for
Hacker NewsTechnology links
Stack OverflowQuestions and answers with votes
QuoraQuestions
DiggSocial news
Product HuntVoting on products
LemmyFederated communities
Discord forumsThreads inside chat
DiscourseForum software

The hard part

One row is easy, one count is hot

Ten thousand people voting on one post write ten thousand different rows. Only the total is shared. Seeing that difference removes most of the problem.

The finished system

Where the design ends up

The whole system

Readers and writers reach a CDN edge, which keeps pages and media, and a gateway. The feed service reads lists from Redis and, on a miss, the posts database. The vote service writes one row to the votes database, partitioned by user. The comment service reads threads from Redis and writes the comments database. Row changes from the votes database go to Kafka. A counter job updates counts and hot lists in Redis, and an abuse job looks for patterns.
Blue reads, pink writes, orange follows later.

Read on

Tab or pageFor
Interviews: Scope, Numbers, API and dataTargets, sums, endpoints and tables
Interviews: Design, Deep divesSix versions, votes, ranking, threads, home feed, karma
VisualizeSix flows, a viral post and failures
Sharded counters, change data captureHot counts and streams of row changes
Idempotency, API paginationSafe repeats and cursors
Data partitioning, distributed cachingWhere rows and copies live
Rate limiting, pub/sub, stream processingAbuse limits, events and windows
Circuit breaker, retriesSlow parts

Summary

The whole topic in one minute.

Separate the vote from the count. The vote is one row for each person and item, stored before we answer and never hot. The count is built from a stream of changes, batched, and may be a second old.

Rank with a score that uses creation time, so it changes only when votes change. Keep ready lists of IDs for communities and merge them for a home page. For a thread, load a light skeleton, sort it, and fetch text for the part people see.

  • Store each person's choice as one keyed row. It makes repeats safe.
  • Build counts from changes, in windows. They may lag by about a second.
  • Hot scores use creation time, so nothing needs to be rescored as it ages.
  • Lists hold IDs. Text, counts and your own votes are added in batches.
  • Load a thread as a skeleton first, then only the visible text.
  • Shown numbers may be approximate. Vote state may not.

Sources