System Design Problem

Design a News Feed System (Facebook / Instagram)

Commonly Asked By:MetaTwitterLinkedInByteDance

  • User can create a post (text, image, video, link)
  • User can follow/unfollow other users (or friend them)
  • News feed displays posts from followed users/friends, ranked by relevance
  • Feed supports infinite scroll / pagination
  • Real-time or near-real-time feed updates when a followed user posts
  • Support reactions (like, love, etc.) and comments on posts
  • Support re-sharing / reposting
Loading...

The Two Approaches: Fan-Out on Write vs. Fan-Out on Read

Fan-Out on Write (Push Model)

  • How: When a user creates a post, immediately write the post ID to every follower's feed cache
  • Pros: Feed reads are fast (pre-computed), O(1) read
  • Cons: Expensive for users with millions of followers (celebrity problem). Wastes writes for inactive users
  • Best for: Normal users with < 10K followers

Fan-Out on Read (Pull Model)

  • How: When a user requests their feed, fetch latest posts from all followed users at query time
  • Pros: No wasted writes, handles celebrities naturally
  • Cons: Slow feed reads (must query many users' post timelines and merge)
  • Best for: Celebrity accounts

Hybrid Approach ⭐ (Recommended)

  • Normal users (< 10K followers): Fan-out on write → push post ID to followers' feed caches
  • Celebrities (> 10K followers): Fan-out on read → when a user opens their feed, fetch celebrity posts on-the-fly and merge with pre-computed feed
  • Classification: A background job periodically classifies users as "normal" or "celebrity" based on follower count