1️⃣ High Level Design

News-Feed.excalidraw

Client
   |
API Gateway
   |
+-------------------+
| Post Service      |
+-------------------+
   |
 Post Table

Post Flow

User creates post

Post Service

Post Table

Follow Flow

User follows another user

Follow Service

Follow Table

Feed Flow (Naive Approach)

User requests feed

Feed Service

Get followed users

Fetch posts of each user

Merge + Sort

Return Feed

6️⃣ Pagination Design

Use cursor-based pagination. Cursor = timestamp of oldest item seen.

Example:

Feed Page 1
 
Post A (10:00)
Post B (09:59)
Post C (09:58)
 
Cursor = 09:58

Next request:

GET /feed?cursor=09:58

Returns older posts.

Benefits:

  • No duplicate records
  • Infinite scrolling
  • Better than offset pagination

7️⃣ Problem With Naive Feed Generation

Suppose:

User follows 10,000 people

Feed Service must:

Read 10,000 timelines
Fetch millions of posts
Merge all results
Sort them

Problems:

  • Huge network calls
  • High latency
  • Expensive sorting
  • Doesn’t scale

8️⃣ Fanout on Write (Precomputed Feed)

Idea

Instead of computing feed during reads:

Compute feed when post is created

Create a new table:

PrecomputedFeed

Schema

UserIdPostIds
UserA[P1,P2,P3…]

Store latest ~200 posts per user.


Post Creation Flow

User creates post

Post Service

Post Table

Find Followers

Insert PostId into follower feeds

Feed Read Flow

Feed Service

PrecomputedFeed Table

Return Feed

Benefits:

✅ Extremely fast reads

✅ Simple feed retrieval


9️⃣ Problem: Celebrity Accounts

Suppose:

Justin Bieber
100M followers

One post requires:

100M feed writes

This causes:

  • Massive write amplification
  • Huge spikes
  • Long post creation delays

🔟 Async Fanout

Never perform fanout synchronously.

Use Queue + Workers.

Post Service

Queue

Feed Workers

PrecomputedFeed

Workflow

Post Created

Publish Event

Worker Consumes Event

Fetch Followers

Write Feed Entries

Large Fanout

Split work.

100M followers

becomes

1000 jobs
   ×
100K followers each

Distributed across worker fleet.


1️⃣1️⃣ Hybrid Fanout Strategy

Observation:

Most users → few followers
Few users → millions of followers

Use:

Fanout on Write

For normal users.

Write feed during post creation

Fanout on Read

For celebrities.

Generate feed dynamically

Follow Table Addition

isPrecomputed
FollowingFollowedisPrecomputed
ABtrue
ACelebrityfalse

Feed Generation

Feed Request

Read Precomputed Feed

Read Celebrity Posts

Merge Results

Return Feed

Best of both worlds.


1️⃣2️⃣ Hot Key Problem

Scenario

A celebrity creates a post.

Millions of users request:

Post #123

All requests hit same partition.

DynamoDB Partition X

Result:

Hot Partition
Throttling
Failures

1️⃣3️⃣ Solution: Distributed Cache

Add cache before database.

Feed Service

Redis Cache

DynamoDB

Benefits:

  • Most reads served from memory
  • Reduces DB traffic drastically

1️⃣4️⃣ Cache Hot Key Problem

Even cache can become hot.

All requests

Same Cache Key

Single Redis node overloaded.


1️⃣5️⃣ Solution: Cache Replication

Instead of sharding:

Redis-1
Redis-2
Redis-3
Redis-4

Feed Service randomly picks one.

Request

Random Cache Instance

Benefits:

  • Load distributed
  • No single hot cache node
  • Much higher throughput

1️⃣6️⃣ Key Interview Concepts

Fanout on Read

Generate feed during read time

Pros:

  • Cheap writes

Cons:

  • Expensive reads

Fanout on Write

Generate feed during write time

Pros:

  • Fast reads

Cons:

  • Expensive writes

Hybrid Model

Normal Users  → Fanout on Write
Celebrities   → Fanout on Read

Industry standard approach.


1️⃣7️⃣ Final Architecture

                +----------------+
                | API Gateway    |
                +----------------+
                         |
       +-----------------+-----------------+
       |                                   |
+-------------+                    +-------------+
| Post Service|                    |Feed Service |
+-------------+                    +-------------+
       |                                   |
       v                                   |
+-------------+                            |
| Post Table  |                            |
+-------------+                            |
       |                                   |
       v                                   |
     Queue ------------------> Feed Workers
                                       |
                                       v
                              +------------------+
                              |Precomputed Feed  |
                              +------------------+
                                       |
                                       v
                                Redis Cache
                                       |
                                       v
                                  Feed User

Interview Sound-Bite 🎯

News Feed is fundamentally a fanout problem. The key tradeoff is between fanout-on-read and fanout-on-write. A scalable design typically uses a hybrid approach, precomputing feeds for normal users while generating celebrity content dynamically, combined with asynchronous workers, queues, and caching to handle scale.