News Feed (Scaling & Ranking)

A News Feed (like Twitter, Facebook, or Instagram) is one of the most complex scaling challenges in system design due to the "Fan-out" problem.


1. The Blueprint (Requirements)

A production-ready feed must handle:


2. Step-by-Step Implementation

Step 1: Feed Data Model

A feed item is basically a list of post_ids associated with a user_id.

Step 2: Pre-calculation (The Fan-out)

To make it fast, we don't calculate the feed when you log in. We calculate it when people post.


3. High-Level Architecture

graph TD
    UserA[Creator] --> LB1[Load Balancer]
    LB1 --> PostService[Post Service]
    PostService --> FanOut[Fan-out Worker]
    FanOut --> Cache[Redis: User Feeds]
    
    UserB[Viewer] --> LB2[Load Balancer]
    LB2 --> FeedService[Feed Service]
    FeedService --> Cache

4. Handling Scale (The "System Design" Part)

The Fan-out Problem

How do you deliver a post to 100 million people?

  1. Push Model (Fan-out on Write):

    • When User A posts, the system pushes that post to the "Home Feed" caches of all their followers.
    • Pro: Near-instant for the viewer.
    • Con: If User A is a celebrity (e.g., Cristiano Ronaldo), pushing to 500M followers takes too long and crashes the system.
  2. Pull Model (Fan-out on Load):

    • Feeds are generated only when a user logs in.
    • Pro: No write-time pressure.
    • Con: Very slow for the viewer if they follow many people.

The "Hybrid" Approach (Used by Twitter/Facebook)

Feed Ranking

In reality, you don't just see the newest posts.


Key Takeaway

Scaling a News Feed is a trade-off between Write latency and Read speed. A hybrid approach using Pre-computed Caches for normal users and On-demand Pulling for celebrities is the standard solution for massive scale.