1️⃣ High Level Design
News-Feed.excalidraw
Client
|
API Gateway
|
+-------------------+
| Post Service |
+-------------------+
|
Post TablePost Flow
User creates post
↓
Post Service
↓
Post TableFollow Flow
User follows another user
↓
Follow Service
↓
Follow TableFeed Flow (Naive Approach)
User requests feed
↓
Feed Service
↓
Get followed users
↓
Fetch posts of each user
↓
Merge + Sort
↓
Return Feed6️⃣ 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:58Next request:
GET /feed?cursor=09:58Returns older posts.
Benefits:
- No duplicate records
- Infinite scrolling
- Better than offset pagination
7️⃣ Problem With Naive Feed Generation
Suppose:
User follows 10,000 peopleFeed Service must:
Read 10,000 timelines
Fetch millions of posts
Merge all results
Sort themProblems:
- 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 createdCreate a new table:
PrecomputedFeedSchema
| UserId | PostIds |
|---|---|
| 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 feedsFeed Read Flow
Feed Service
↓
PrecomputedFeed Table
↓
Return FeedBenefits:
✅ Extremely fast reads
✅ Simple feed retrieval
9️⃣ Problem: Celebrity Accounts
Suppose:
Justin Bieber
100M followersOne post requires:
100M feed writesThis causes:
- Massive write amplification
- Huge spikes
- Long post creation delays
🔟 Async Fanout
Never perform fanout synchronously.
Use Queue + Workers.
Post Service
↓
Queue
↓
Feed Workers
↓
PrecomputedFeedWorkflow
Post Created
↓
Publish Event
↓
Worker Consumes Event
↓
Fetch Followers
↓
Write Feed EntriesLarge Fanout
Split work.
100M followersbecomes
1000 jobs
×
100K followers eachDistributed across worker fleet.
1️⃣1️⃣ Hybrid Fanout Strategy
Observation:
Most users → few followers
Few users → millions of followersUse:
Fanout on Write
For normal users.
Write feed during post creationFanout on Read
For celebrities.
Generate feed dynamicallyFollow Table Addition
isPrecomputed| Following | Followed | isPrecomputed |
|---|---|---|
| A | B | true |
| A | Celebrity | false |
Feed Generation
Feed Request
↓
Read Precomputed Feed
↓
Read Celebrity Posts
↓
Merge Results
↓
Return FeedBest of both worlds.
1️⃣2️⃣ Hot Key Problem
Scenario
A celebrity creates a post.
Millions of users request:
Post #123All requests hit same partition.
DynamoDB Partition XResult:
Hot Partition
Throttling
Failures1️⃣3️⃣ Solution: Distributed Cache
Add cache before database.
Feed Service
↓
Redis Cache
↓
DynamoDBBenefits:
- Most reads served from memory
- Reduces DB traffic drastically
1️⃣4️⃣ Cache Hot Key Problem
Even cache can become hot.
All requests
↓
Same Cache KeySingle Redis node overloaded.
1️⃣5️⃣ Solution: Cache Replication
Instead of sharding:
Redis-1
Redis-2
Redis-3
Redis-4Feed Service randomly picks one.
Request
↓
Random Cache InstanceBenefits:
- Load distributed
- No single hot cache node
- Much higher throughput
1️⃣6️⃣ Key Interview Concepts
Fanout on Read
Generate feed during read timePros:
- Cheap writes
Cons:
- Expensive reads
Fanout on Write
Generate feed during write timePros:
- Fast reads
Cons:
- Expensive writes
Hybrid Model
Normal Users → Fanout on Write
Celebrities → Fanout on ReadIndustry 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 UserInterview 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.