Topic 11System Walkthroughs
Design a Twitter Feed
Fan-out write vs fan-out read. One of the most common hard system design problems.
Design a system where users post tweets and see a feed of tweets from people they follow. The core challenge: how do you generate a personalized feed for hundreds of millions of users efficiently?
Requirements
Core scope for this problem.
- ›Users can post tweets, follow other users, and view a home feed
- ›300M DAU, avg 500 follows per user, avg 5 feed reads per day
- ›Feed read QPS: 300M × 5 ÷ 86,400 ≈ 17K QPS
- ›Tweet write QPS: assume 1% write → ~35 writes/sec (much lower)
Fan-out on write (push model)
When a user tweets, pre-compute and push to all followers' feeds.
- ›On post: look up all followers, push tweet ID into each follower's feed cache (Redis sorted set)
- ›Feed read: just read the pre-built cache — very fast
- ›Problem: celebrities with 10M followers mean 10M writes per tweet
- ›Good for: most users with normal follower counts
Fan-out on read (pull model)
When a user requests their feed, pull and merge tweets from everyone they follow.
- ›On feed request: query recent tweets from all followed users, merge and rank
- ›Problem: user follows 1000 people = 1000 DB queries per feed load
- ›Good for: celebrity accounts whose tweets go to many feeds
Hybrid approach (Twitter's actual strategy)
The production solution — combine both.
- ›Regular users: fan-out on write — push to follower caches
- ›Celebrity accounts (>10K followers): fan-out on read — merge on request
- ›Feed generation: pre-built cache for most followers + late-merge celebrity tweets
- ›Tweet storage: separate tweet DB (Cassandra for write scale) from feed cache (Redis)
Interview tips
- ✓This is a known hard problem — showing you know the hybrid approach signals seniority
- ✓Address the celebrity / hotspot problem explicitly
- ✓Discuss feed ranking: chronological is easy, ML ranking is a follow-up
- ✓Mention that DMs and notifications are separate systems
Follow-up questions to expect
- ?How do you handle a tweet going viral after it's already been pushed to feeds?
- ?How would you add ML-based feed ranking?
- ?How do you handle users with 50 million followers posting simultaneously?
TLDR
- ›Fan-out on write = fast reads, expensive celebrity writes
- ›Fan-out on read = slow reads, avoids celebrity write amplification
- ›Hybrid: push for normal users, pull for celebrities
- ›Redis sorted sets are the natural fit for feed caches (ranked by timestamp)
- ›Separate tweet storage (Cassandra) from feed cache (Redis)