Module 34: System Design — News Feed System

Comprehensive architectural breakdown of a large-scale News Feed service (Twitter/X, Facebook/Instagram), covering Fan-out-on-Write (Push), Fan-out-on-Read (Pull), Hybrid Celebrity fanout models, and Redis timeline ranking.


Prerequisites

Before tackling the news feed system, review these core modules:


ELI5 (30-Second Summary)

Imagine you write a short letter to your 10 friends. You can either walk to each friend's house and drop a copy in their mailbox (Push / Fan-out-on-Write), or your friends can walk to your house every morning and read what you wrote on your front porch chalkboard (Pull / Fan-out-on-Read).

Push is great because when your friends wake up, their mailboxes are already full—reading their feed is instantaneous! But what if you are a superstar celebrity with 100 million fans? If you post a letter, walking to 100 million houses creates a traffic jam that crashes the city.

A Hybrid News Feed pushes posts to regular friends' mailboxes instantly, but for mega-celebrities, fans fetch their posts only when they open the app and merge them into their timeline on the fly!


Physical Analogy

Think of a Suburban Postal Route vs. Central Newspaper Kiosk:

Physical Analogy News Feed Architecture Concept Technical Description
Author writing an article Post Publishing API (POST /v1/feed) Ingests post content, media references, and metadata.
Suburban Mail carrier Fan-out Worker Pool (Async Queue) Reads follower list and pushes post ID into follower inbox caches.
Personal Home Mailbox User Feed Timeline (Redis Sorted Set ZSET) Stores pre-computed list of Post IDs sorted by timestamp/rank.
Celebrity Newspaper Kiosk Celebrity Outbox Cache (Pull Fan-out) Standalone feed buffer read on-demand during timeline fetch.
Reading today's breakfast mail Feed Retrieval API (GET /v1/feed) Pulls top 20 Post IDs from Redis, hydrating full posts from KV cache.

The Problem It Solves

Social networks with hundreds of millions of daily active users (DAUs) face severe asymmetric read/write traffic:

  1. Massive Read Dominance: Users read their feed 50x to 100x more often than they post (100:1 read-to-write ratio).
  2. Sub-100ms Latency Expectation: When a user launches the app, their timeline must render within 100 milliseconds. Joining thousands of followee tables via relational SQL SELECT * FROM posts WHERE user_id IN (SELECT followee_id...) ORDER BY created_at DESC locks tables and takes dozens of seconds at scale.
  3. The Celebrity "Hotspot" Problem: A user with 80 million followers publishing a post requires 80 million database/cache write operations in seconds, overwhelming worker queues.

Visual Architecture

Hybrid Social News Feed Fan-Out Architecture

flowchart TD
    subgraph PublishingPath["1. Post Publishing Flow (Fan-out on Write)"]
        ClientPost["User Client"] -->|"POST /v1/posts"| Gateway["API Gateway / LB"]
        Gateway --> PostSvc["Post Service"]
        PostSvc --> DBPost[("Post DB / Blob Store")]
        PostSvc --> FanoutQueue["Kafka / RabbitMQ (Fanout Queue)"]
        FanoutQueue --> FanoutWorkers["Fanout Worker Pool"]
        FanoutWorkers --> FollowerSvc["Graph Service (Get Followers)"]
        FollowerSvc --> FilterCeleb{"Is Author Celebrity (>10k)?"}
        FilterCeleb -->|"No (Normal User)"| PushRedis["Push Post ID to Follower Timelines (Redis ZSET)"]
        FilterCeleb -->|"Yes (Celebrity)"| SkipPush["Write ONLY to Celebrity Outbox Cache"]
    end

    subgraph ReadingPath["2. Feed Generation Flow (Hybrid Merge)"]
        ClientRead["User Client"] -->|"GET /v1/feed?page=1"| FeedSvc["News Feed Service"]
        FeedSvc --> ReadZset["1. Fetch Top 100 Post IDs from User's Redis ZSET"]
        FeedSvc --> FetchCeleb["2. Query Followed Celebrities' Outboxes"]
        FeedSvc --> MergeSort["3. K-Way Merge & Rank Candidates"]
        MergeSort --> Hydrate["4. Hydrate Post Bodies & User Data from Memcached"]
        Hydrate --> ReturnJSON["5. Return Enriched JSON to Client"]
    end

Real Numbers & System Sizing

Consider a platform like Twitter or Instagram with 300 Million Daily Active Users (DAU):


Deep Dive: Under the Hood

1. Fan-Out on Write (Push Model)

2. Fan-Out on Read (Pull Model)

3. The Hybrid Celebrity Model (Industry Standard)


Trade-offs & Decision Matrix

Dimension Fan-out on Write (Push) Fan-out on Read (Pull) Hybrid Push-Pull Model
Read Latency Ultra-low (< 10 ms) High (200 - 800 ms) Very low (15 - 30 ms)
Write Latency / Load Massive write amplification Minimal (O(1) post insert) Balanced & predictable
Celebrity Handling Catastrophic (queue backlogs) Excellent Excellent (special outbox routing)
Resource Utilization High RAM (stores dormant feeds) High CPU during read spikes Optimal (RAM for active, CPU on-demand)

Common Pitfalls & Anti-Patterns

  1. Hydrating Full Post Objects in the Timeline Queue: Storing the entire post JSON (text, comments, author profile) inside the follower's Redis ZSET multiplies storage by 1,000x. Rule: Only store 64-bit Post IDs in the feed timeline; hydrate full post objects from Memcached/Redis KV cache only for the paginated window being viewed.
  2. Infinite Timeline Retention: Keeping years of post history in Redis. Most users only browse the most recent 500-1,000 posts. Set a hard limit on Redis ZSET size (ZREMRANGEBYRANK user:timeline 0 -801).
  3. Synchronous Fanout in Web Request Handler: Iterating over followers inside the HTTP POST /posts handler blocks the client for minutes. Fanout must always be decoupled into an asynchronous message queue (Kafka).

Try It Yourself

A runnable, zero-dependency Python simulation demonstrating normal user fanout-on-write, celebrity fanout-on-read outbox routing, and real-time hybrid feed merge ranking is included in this directory.

Run the simulation:

python demo.py

Production References & Further Reading


Top 5 Interview Questions with Model Answers

Q1: How do you handle the "Celebrity Problem" (Hotspot) in a News Feed system?

Model Answer: The celebrity problem occurs when an account with millions of followers publishes a post, causing write amplification that chokes message queues. We solve this using a Hybrid Fan-out Model:

Q2: What data structure in Redis is ideal for user news feeds and why?

Model Answer: Redis Sorted Sets (ZSET) are optimal. The member is the unique 64-bit post_id, and the score is the integer timestamp (or ranking score).

Q3: How do you support non-chronological ranked feeds (e.g., machine learning relevance scoring)?

Model Answer: Feed ranking operates in a multi-stage pipeline:

  1. Candidate Retrieval: Fetch the top 500-1,000 recent candidate post IDs from the user's hybrid inbox.
  2. Feature Hydration: Enrich candidate IDs with author authority, user affinity score, post engagement (likes, comments, recency), and media type.
  3. ML Scoring Model: Pass feature vectors through a lightweight ranking model (e.g., GBDT or deep neural network) to output an affinity probability P(click).
  4. Re-sorting & Delivery: Sort the candidate items by score, apply business heuristics (e.g., diversity rules preventing 5 consecutive videos), and return the top 20 to the client.

Q4: How do you paginate a news feed reliably without missing or duplicating posts?

Model Answer: Traditional offset-based pagination (OFFSET 20 LIMIT 20) fails in real-time feeds because new posts inserted at the top shift indices, causing duplicate or skipped items across pages. We use Cursor-Based Pagination using time-sortable Snowflake IDs:

Q5: How do you manage timeline storage for inactive users who haven't opened the app in months?

Model Answer: We implement Active User Gating:


Check Yourself Quiz

1. Why is Fan-out on Write preferred for read-heavy social platforms with standard users? A) Because it eliminates the need for database storage.
B) Because it converts expensive multi-table read joins into instantaneous O(1) cache lookups at read time.
C) Because it requires zero message queues.
D) Because it avoids using Redis.

Answer: B. By pre-computing timelines at write time, user reads are instant cache lookups, satisfying sub-100ms mobile expectations.
2. Why should full post content NOT be stored directly in follower timeline caches? A) Redis cannot store text.
B) Storing full posts across millions of follower lists causes massive data redundancy and memory exhaustion; storing only 64-bit Post IDs saves 95%+ RAM.
C) Post content must be encrypted with SSL inside Redis.
D) Because post IDs cannot be indexed.

Answer: B. Storing only the 8-byte ID reduces the footprint to a few kilobytes per user; content is hydrated only when viewed.
3. What pagination strategy prevents duplicated posts when a user scrolls down while new posts arrive? A) Offset-based pagination (`OFFSET 50`)
B) Cursor-based pagination using monotonically increasing Snowflake IDs
C) Random sampling
D) Polling every 5 seconds

Answer: B. Cursor pagination queries strictly earlier timestamp/IDs than the last viewed item, immune to top-of-feed insertions.
4. In a hybrid fanout system, how is a post by a celebrity with 20M followers distributed? A) Pushed to all 20M follower inboxes immediately.
B) Saved only to the celebrity's outbox cache; merged on-the-fly when followers load their feed.
C) Sent via SMS to all followers.
D) Discarded until the next day.

Answer: B. Writing to the celebrity outbox avoids millions of immediate writes, deferring the merge to read time.
5. What cache eviction policy is most suitable for News Feed timeline Redis clusters? A) LFU (Least Frequently Used)
B) LRU / TTL-based eviction prioritizing recently active users
C) Random eviction
D) Never evict; crash on full memory

Answer: B. Feeds of dormant users should be evicted first, while active users' timelines remain hot in RAM.