Case Study 06 โ€” News Feed

Design a Twitter/X-like news feed: users post short updates and see a personalized timeline of posts from people they follow.

Practice first: Cover estimates/HLD. Decide fan-out on write vs read with Pattern Choosers, then compare to First 10 Minutes โ€” Walkthrough C.

1. Problem

When a user opens the app, they expect a chronological (or ranked) list of recent posts from accounts they follow โ€” without waiting several seconds. When someone they follow publishes a new post, followers should see it in their feed soon after.

The hard part is not storing a single tweet โ€” it is delivering the right posts to millions of users quickly when some accounts have millions of followers.

2. Requirements

Functional (MVP)

  • Users can create a post (text, optional media link)
  • Users can follow / unfollow other users
  • Users can view their home feed โ€” posts from followed accounts, newest first
  • Users can view a profile feed โ€” all posts by one user
  • Basic like count on posts (optional MVP stretch)

Out of scope (initially)

  • Full-text search, trending topics, ads, DMs, replies/threading UI, edit history, verification badges, recommendation ML ranking

Non-functional

  • Feed load p95 < 300 ms for typical users
  • Post creation feels instant (< 200 ms ack to author)
  • High availability โ€” stale feed OK briefly; total outage is not
  • Eventually consistent feed for celebrity posts is acceptable
  • Scale to 500M users, 200M DAU, heavy read traffic

3. Back-of-the-envelope estimates

We do rough math so we know what to optimize. Exact precision is not the goal โ€” order of magnitude is.

Why we estimate (beginner tip)

Ask three questions:

  1. How busy? โ†’ QPS (requests per second)
  2. How much data? โ†’ storage (GB/TB)
  3. How fat is the pipe? โ†’ bandwidth (MB/s) when media matters

Cheat sheet: 1 QPS โ‰ˆ 86,400 requests/day. Peak is often 2โ€“5ร— average.

Assumptions (say these out loud)

  • 500M registered users, 200M DAU (daily active users)
  • Each DAU opens their home feed 5ร—/day โ†’ 1B feed reads/day
  • 10% of DAU post 1 post/day โ†’ 20M new posts/day
  • Average user follows 200 accounts; post metadata โ‰ˆ 500 bytes
  • Precomputed feed cache: top 500 post IDs ร— 8 bytes โ‰ˆ 4 KB per active user

Step A โ€” Traffic (QPS)

Feed read QPS:
  1B reads/day รท 86,400 โ‰ˆ 12,000/s average
  Peak (5ร— avg)            โ‰ˆ 60,000/s

Post write QPS:
  20M posts/day รท 86,400 โ‰ˆ 230/s average
  Peak (5ร— avg)            โ‰ˆ 1,000/s

Read:write ratio โ‰ˆ 1B / 20M = 50:1 (reads dominate)

Naive fan-out on write (push post to every follower):
  20M posts ร— 200 followers = 4B feed-row writes/day
  4B / 86,400 โ‰ˆ 46,000 fan-out writes/s  โ†’ too expensive for everyone

Step B โ€” Storage

Posts (5 years retention):
  20M/day ร— 365 ร— 5 ร— 500 bytes โ‰ˆ 18 TB

Feed cache (upper bound โ€” all 200M DAU precomputed):
  200M users ร— 4 KB โ‰ˆ 800 GB in Redis
  Trim to top 500โ€“1000 post IDs โ†’ realistic ~400โ€“800 GB

Social graph (follows):
  500M users ร— 200 follows ร— 16 bytes โ‰ˆ 1.6 TB (manageable with sharding)

Step C โ€” Bandwidth / other (if relevant)

Feed API response (20 posts ร— ~1 KB JSON each):

60,000 feed reads/s ร— 20 KB โ‰ˆ 1.2 GB/s peak API egress

Media links (not bytes) in feed โ€” images served from CDN separately

Fan-out worker throughput is the hidden bandwidth cost โ€” Redis ZADD ops, not HTTP.

Step D โ€” Read:write ratio

PathApprox shareImplication
Home feed read (GET /feed)~99%Precompute in Redis sorted sets; O(1) lookup
Create post (POST /posts)~1%Fast ack to author; async fan-out via queue
Profile feed readSmallQuery posts by author_id โ€” no fan-out needed
Follow/unfollowTiny writesUpdate graph DB; optional backfill into feed cache

What the numbers tell us

  • 60k peak feed reads/s โ†’ feed must be a Redis cache lookup, not a join across 200 followees at request time
  • 46k fan-out writes/s (naive) โ†’ hybrid model: fan-out on write for normal users; pull at read for celebrities (>10k followers)
  • 18 TB posts over 5 years โ†’ shard Posts DB by post_id or author_id
  • 800 GB feed cache โ†’ Redis cluster; trim ZSETs to top 1000; expire cold users after 7 days
  • Async fan-out via Kafka/SQS โ†’ post creation returns in <200 ms; workers push post IDs to follower feeds
  • Snowflake post IDs โ†’ time-ordered without extra index column

Common mistake for this problem

Using fan-out on write for every user, including celebrities with 50M followers. One post would trigger 50M Redis writes โ€” use fan-out on read (pull) for high-follower accounts and merge at read time.

4. High-Level Design (HLD)

Components

ComponentRole
Feed APIGET /feed, auth, pagination
Post ServiceCreate post, store metadata
Social Graph DBfollower โ†’ followees relationships
Posts DBPost content, author, timestamp (sharded by post_id or user_id)
Feed Cache (Redis)Precomputed user_id โ†’ [post_id, โ€ฆ] sorted lists
Fan-out WorkersOn new post, push post ID into followers' feed caches
Message QueueDecouple post write from fan-out; retry on failure
Media Service + S3 + CDNOptional images; store blobs in S3, serve via CDN

Flows

Create post (fan-out on write path)

  1. Client POST /posts with text (+ media upload separately)
  2. Post Service assigns post_id (Snowflake), writes row to Posts DB
  3. Returns 201 immediately to author
  4. Enqueues fan-out job { post_id, author_id, timestamp }
  5. Worker loads author's followers from Social Graph
  6. For each follower (or batch): ZADD feed:{follower_id} timestamp post_id in Redis (trim to top 1000)
  7. If author is a celebrity (> 10k followers), skip fan-out; followers pull at read time

Read home feed

  1. Client GET /feed?cursor=โ€ฆ
  2. Fetch post_id list from Redis feed:{user_id} (sorted set, newest first)
  3. If cache miss or user follows celebrities: merge cached IDs + pull recent posts from celebrity followees
  4. Batch GET posts by IDs from Posts DB (or post cache)
  5. Hydrate author info, like counts; return JSON page

Read profile feed

  1. Query Posts DB: WHERE author_id = ? ORDER BY created_at DESC LIMIT 20
  2. No fan-out needed โ€” single user's timeline

Trade-offs: fan-out on write vs fan-out on read

StrategyProsCons
Fan-out on writeFeed read is O(1) cache lookup โ€” very fastExpensive for celebrities; wasted work if follower never opens app
Fan-out on readCheap write; no push to millionsSlow read โ€” must query all followees at request time
Hybrid (recommended)Fast reads for 99% of users; celebrities don't melt the systemTwo code paths; merge logic at read time

Other trade-offs

  • Chronological vs ranked feed โ€” chronological is simpler; ranking needs ML features and offline jobs
  • Redis sorted sets vs plain lists โ€” sorted sets give score-based ordering and easy trim
  • Push notifications โ€” separate notification service subscribed to same post events

5. Low-Level Design (LLD)

APIs

POST /api/v1/posts
Body: { "text": "Hello world", "mediaIds": [] }
โ†’ 201 { "postId": "1234567890", "createdAt": "..." }

GET /api/v1/feed?limit=20&cursor=<opaque>
โ†’ 200 {
     "posts": [ { "postId", "authorId", "text", "likeCount", "createdAt" } ],
     "nextCursor": "..."
   }

GET /api/v1/users/:userId/posts?limit=20&cursor=...
โ†’ 200 { "posts": [...], "nextCursor": "..." }

POST /api/v1/users/:userId/follow
DELETE /api/v1/users/:userId/follow

GET /api/v1/users/:userId/followers?limit=50
GET /api/v1/users/:userId/following?limit=50

Schema / tables

users (
  user_id        BIGINT PRIMARY KEY,
  username       VARCHAR(50) UNIQUE NOT NULL,
  display_name   VARCHAR(100),
  created_at     TIMESTAMPTZ NOT NULL
)

posts (
  post_id        BIGINT PRIMARY KEY,       -- Snowflake ID (time-sortable)
  author_id      BIGINT NOT NULL,
  text           VARCHAR(280) NOT NULL,
  media_urls     JSONB DEFAULT '[]',
  like_count     INT DEFAULT 0,
  created_at     TIMESTAMPTZ NOT NULL,
  INDEX (author_id, created_at DESC)
)
-- shard posts by hash(post_id) or by author_id

follows (
  follower_id    BIGINT NOT NULL,
  followee_id    BIGINT NOT NULL,
  created_at     TIMESTAMPTZ NOT NULL,
  PRIMARY KEY (follower_id, followee_id),
  INDEX (followee_id)   -- for fan-out: "who follows this author?"
)

-- optional: celebrity flag or follower_count cache
user_stats (
  user_id          BIGINT PRIMARY KEY,
  follower_count   INT DEFAULT 0,
  is_celebrity     BOOLEAN GENERATED OR cached threshold
)

Redis structures

feed:{user_id}           โ†’ ZSET  score=timestamp_ms  member=post_id
post:{post_id}           โ†’ HASH  text, author_id, created_at (hot cache)
followees:{user_id}      โ†’ SET of followee_ids (for read-time merge)
celebrity_followees:{user_id} โ†’ SET subset for pull-at-read

Modules

FeedController / PostController / GraphController
FeedService          โ€” merge cache + celebrity pull
PostService          โ€” create, validate, enqueue fan-out
FanOutWorker         โ€” push to follower feeds
GraphService         โ€” follow/unfollow, list followers
PostRepository       โ€” CRUD posts (sharded)
FeedCache            โ€” Redis ZSET operations
CelebrityDetector    โ€” follower_count > THRESHOLD
IdGenerator          โ€” Snowflake

Key algorithms (pseudocode)

Create post + async fan-out

function createPost(authorId, text, mediaIds):
  postId = idGen.nextId()
  post = { postId, authorId, text, mediaIds, createdAt: now() }
  postRepo.insert(post)
  queue.publish("fanout", { postId, authorId, createdAt: post.createdAt })
  return post

function fanOutWorker(job):
  authorId = job.authorId
  if userStats.followerCount(authorId) > CELEBRITY_THRESHOLD:
    return   // skip push; readers pull at read time

  followerIds = graphRepo.getFollowers(authorId)   // paginate in batches of 1000
  score = job.createdAt.toEpochMs()
  for batch in chunks(followerIds, 500):
    pipeline = redis.pipeline()
    for followerId in batch:
      key = "feed:" + followerId
      pipeline.zadd(key, score, job.postId)
      pipeline.zremrangeByRank(key, 0, -1001)   // keep newest 1000
      pipeline.expire(key, 7 days)              // cold users rebuild on miss
    pipeline.execute()

Read home feed (hybrid merge)

function getHomeFeed(userId, limit, cursor):
  cachedIds = feedCache.getRange(userId, cursor, limit)   // from ZSET

  celebrityFollowees = graphRepo.getCelebrityFollowees(userId)
  pulledIds = []
  if celebrityFollowees not empty:
    pulledIds = postRepo.getRecentByAuthors(
      celebrityFollowees,
      since: now() - 24 hours,
      limit: 50
    )

  allIds = mergeSortedByTime(cachedIds, pulledIds)
  allIds = dedupe(allIds).take(limit)

  posts = postRepo.getByIds(allIds)          // batch fetch
  posts = hydrateAuthorsAndLikes(posts)
  nextCursor = encodeCursor(allIds.last)
  return { posts, nextCursor }

Follow / unfollow

function follow(followerId, followeeId):
  graphRepo.insert(followerId, followeeId)
  userStats.incrementFollowerCount(followeeId)
  // optional: backfill recent posts from followee into follower's feed cache
  recentPosts = postRepo.getRecentByAuthor(followeeId, limit=50)
  for post in recentPosts:
    feedCache.zadd(followerId, post.createdAt, post.postId)

Concurrency notes

  • Fan-out is idempotent โ€” ZADD same (score, post_id) twice is safe
  • Follower count โ€” increment/decrement in DB or async counter; celebrity threshold can lag slightly
  • Post ID from Snowflake โ€” no coordination on write path across Post Service instances
  • Cache stampede on cold feed โ€” on miss, single-flight rebuild: one request rebuilds, others wait
  • Trim feed ZSET โ€” cap size so memory bounded; older posts fall off (acceptable for home feed)

6. Scale evolution

StageUsersChange
MVP< 100kSingle DB; fan-out on read only (query followees' posts)
Growth1MRedis feed cache + fan-out on write for users with < 10k followers
Scale50M+Shard Posts DB; dedicated fan-out worker pool; celebrity hybrid
Huge500M+Multi-region Redis; graph DB sharded; optional Kafka for fan-out; ranking pipeline offline

7. Recap

  • Reads >> writes โ€” optimize feed load with precomputed Redis timelines
  • Fan-out on write for normal users; fan-out on read (pull) for celebrities โ€” hybrid is industry standard
  • Snowflake post IDs give time ordering without extra index
  • Async fan-out via queue keeps post creation fast and allows retries

Practice: Draw the hybrid read path from memory. Explain why a user with 50M followers breaks naive fan-out on write.