Case Study: News Feed

Design a Twitter/Instagram-style home timeline — fan-out on write vs read, the celebrity problem and the hybrid approach, feed caches, ranking, pagination and media.

Advanced 3 min readLesson 18 of 20

1. Requirements

Functional: users post (text and media), follow others, and see a home feed of posts from people they follow, newest or ranked first, with infinite scroll.

Non-functional: feed loads fast (p99 under ~200 ms), highly available, eventual consistency is fine (a post may take seconds to appear), extremely read-heavy.

2. Estimates

  • 300M DAU; each opens the feed ~10 times a day → 3B feed reads/day ≈ 35k reads/s (peak 100k+).
  • 2 posts/user/day → 600M posts/day ≈ 7k writes/s.
  • Average 200 followers; some accounts have 100M.

3. The core question: when do you build the feed?

Fan-out on write (push)

When a user posts, insert the post ID into every follower's feed cache.

  • Reading is a single cache lookup: fast
  • Writing costs O(followers): a celebrity post means 100M inserts
  • Wasted work for inactive followers
Fan-out on read (pull)

At read time, fetch recent posts from everyone the user follows and merge them.

  • Writing is cheap: just store the post
  • Reading is slow: hundreds of queries plus a merge, on every load
  • Always fresh; nothing precomputed

The hybrid (what large networks do): push for normal users; for celebrities (followers above a threshold), don't fan out — at read time, merge the precomputed feed with recent posts pulled from the few celebrities the user follows. Also skip fan-out to long-inactive users and rebuild their feed on their next visit.

4. High-level design

postreadfollowershydrateClientsPost serviceFeed servicePosts DBFan-out queueFan-out workersFeed cacheuser → [post IDs]Social graph

5. Write path

  1. Store the post (and upload media to object storage) and return to the user immediately.
  2. Publish PostCreated to the fan-out queue.
  3. Workers fetch the author's followers (paginated) from the social graph store and push the post ID onto each follower's feed list in Redis (LPUSH then LTRIM to keep the latest ~800).
  4. Celebrities skip step 3.

6. Read path

  1. Read post IDs from the user's feed cache (plus pull recent posts from followed celebrities).
  2. Merge and rank.
  3. Hydrate: fetch post bodies, author info and counts in batch from caches (multi-get), dropping deleted posts and blocked authors.
  4. Return a page with a cursor (the last post ID/score), not an offset — the feed changes while the user scrolls.

Store only IDs in feed lists: small, and edits or deletions to a post appear everywhere without rewriting millions of feeds.

7. Ranking

Chronological is simplest. Ranked feeds score candidate posts with a model using features like affinity with the author, post engagement so far, recency, and content type. A typical pipeline: candidate generation (feed cache + celebrities + recommendations) → lightweight ranking → heavy ranking on the top few hundred → re-ranking for diversity and business rules.

8. Other concerns

  • Counts (likes, views): counters in Redis, aggregated asynchronously; approximate is fine.
  • Media: CDN with multiple resolutions.
  • New users / cold start: popular and recommended content.
  • Storage: posts sharded by post ID; the social graph sharded by user, with both "followers" and "following" lists.
  • Hot partitions: celebrity posts get enormous reads; cache them aggressively and replicate hot keys.
Key takeaways
  • Fan-out on write gives fast reads; fan-out on read gives cheap writes.
  • Hybrid: push for normal users, pull for celebrities, skip inactive users.
  • Feed caches hold post IDs only; hydrate at read time in batches.
  • Cursor pagination; ranking via candidate generation then staged scoring.
  • Eventual consistency is acceptable and is what makes the design scale.