Feed traffic
Storage:
Create Subreddit
POST /subreddits
Get subreddit feed
GET /subreddits/{id}/posts?sort=x&cursor=...
Subscribe
POST /subreddits/{id}/subscribe
Create Posts
POST /subreddits/{id}/post
Get comments
GET /posts/{id}/comments
post comments
GET /posts/{id}/comments
Vote
POST /posts/{id}/vode
req - {value}
Value -> 0,1,-1
0 = remove vote
1 = upvote
-1 = downvote
Home feed
GET /feed?cursor=...
Acts as the entry point for all client requests.
Responsibilities:
Distributes traffic across multiple instances of all services to improve scalability and availability.
User Service
Post Service
Comment service
Subreddit Service
Vote Service
Feed Service
Vote aggregator
Top posts refresher
formula:
hot_score = (upvotes - downvotes + comments * weight ) * decay recenty
Affinity Pipeline
formula:
affinity = weight * recency
each action has weight assinged so
weight = action * W
Redis
Kafka
Reddit is a massive, read-heavy system where most access patterns are predictable and can be modeled around partition keys. We prioritize horizontal scalability and high availability over relational joins and complex transactions so using NoSQL like cassandra
Users
id - partiton key
username
Subreddits
id - partiton key
name
description
createdBy
createdAt
User subscriptions
Id
userId - partiton key
subredditID
isActive
createdAt
modifiedAt
Posts
Id
Title
Description
media_url
subredditId - partition key
AuthorId- FK to user id
score
CreatedAt -clustering key
Votes
Id
UserId - clustering key
EntityId - Parition key
EntityType - Posts/Comments
value
CreatedAt
Comments
Id
UserId
PostId - partition key
comment
parentCommentId
Score
CreatedAt
We will now deep dive into the following topics:
Feed is one of most high traffic througput and most complex component of the design
How it works?
Ranking formula = post score weight * top comments score weight * decay recency
Example:
Comment A
|
+-- Reply A1
|
+-- Reply A2
The hierarchy comes from:
parent_comment_id
Why not store the entire tree?
A popular post can have millions of comments.
One giant nested document would cause:
Instead, fetch incrementally.
GET comments
|
v
Top 50 comments
|
v
Load replies as needed
Comment Cache
Comments are caches as LFU so popular comments are cached
Comments:post123
Cache:
Redis reduces repeated reads from the comment DB.
-> Top posts refersher and Afiinity PIpelien services are reponsible to keep them fresh.
How it works?
Redis failure:
For post metdata and comments:
For user affinity and hot posts:
Kafka failure
Use:
Because events are durable, consumers can resume.
Database failure
Use: