How Would You Design a Twitter-Style News Feed?
Learn how to design a Twitter-style news feed with hybrid fan-out on write/read, timeline caching, and ranking at scale.
Expected Interview Answer
A Twitter-style news feed is built with a fan-out architecture that precomputes each follower’s timeline on write for most users, falls back to a fan-out-on-read merge for celebrities with millions of followers, and stores the resulting ranked timeline in a fast cache like Redis for instant reads.
When a regular user tweets, the system fans out that tweet id into every follower’s precomputed timeline list (fan-out on write), so reading a timeline is a cheap cache lookup. For celebrity accounts with tens of millions of followers, fanning out on every tweet would be prohibitively expensive, so those tweets are instead merged into a follower’s timeline at read time (fan-out on read), blending precomputed entries with a live query against the small set of celebrities the user follows. A ranking service then reorders the raw chronological candidates using engagement signals, recency decay, and author affinity before the feed is served. The write path uses a durable tweet store and a graph service for the follow relationships, while the read path leans almost entirely on caching to keep latency low at massive scale.
- Hybrid fan-out keeps normal-user timeline reads O(1) while avoiding celebrity write storms
- Precomputed timelines give sub-100ms feed loads even at hundreds of millions of users
- Separating ranking from delivery lets relevance improve without changing the storage model
- Caching the hot timeline path isolates the durable tweet store from read traffic spikes
AI Mentor Explanation
Designing a news feed is like a stadium scoreboard operator who, for most players, updates every fan’s personal scorecard the instant a run is scored, so checking your scorecard is instant. But for a superstar batter followed by the whole stadium, updating every single scorecard on every ball would overwhelm the operator, so instead their runs are merged into each fan’s card only when that fan actually looks at it. This mixed approach — precompute for the many, merge on demand for the few global superstars — is exactly how fan-out on write versus fan-out on read is chosen per account size. The scoreboard operator also reorders entries by excitement, not just time, mirroring feed ranking.
Step-by-Step Explanation
Step 1
Ingest the post
A new tweet is written durably to a tweet store and assigned an id, then published to a fan-out worker queue.
Step 2
Fan out based on follower count
For regular authors, the worker pushes the tweet id into every follower’s precomputed timeline cache; for celebrity authors it skips fan-out entirely.
Step 3
Merge on read for celebrities
When a user opens their feed, the service merges their cached precomputed timeline with a live lookup of tweets from the small set of celebrities they follow.
Step 4
Rank and serve
A ranking service reorders the merged candidate list by recency, engagement and affinity before returning the final feed page.
What Interviewer Expects
- Identifies the hybrid fan-out-on-write / fan-out-on-read trade-off and when to use each
- Explains why celebrity accounts break naive fan-out (write amplification)
- Mentions caching the precomputed timeline (e.g., Redis) to keep reads fast
- Separates ranking/relevance from raw storage and delivery
Common Mistakes
- Proposing pure fan-out on write for every account, ignoring celebrity write storms
- Proposing pure fan-out on read for every account, ignoring normal-user read latency
- Forgetting a durable source of truth (tweet store) behind the cache
- Not addressing ranking at all and treating the feed as strictly chronological
Best Answer (HR Friendly)
“A news feed like Twitter’s works by preparing most people’s timelines ahead of time, so when you open the app it just shows you what is already ready to go. For accounts with a massive number of followers, the system instead pulls their latest posts in only when someone actually checks their feed, because pre-writing to millions of timelines every time would be too expensive. On top of that, the posts get reordered by relevance rather than just showing them in strict time order.”
Code Example
CELEBRITY_FOLLOWER_THRESHOLD = 1_000_000
def publish_tweet(author_id, text):
tweet_id = tweet_store.insert(author_id=author_id, text=text)
if follower_graph.follower_count(author_id) < CELEBRITY_FOLLOWER_THRESHOLD:
# fan-out on write: push into every follower's precomputed timeline
for follower_id in follower_graph.iter_followers(author_id):
timeline_cache.push(follower_id, tweet_id)
# celebrities are skipped here; their tweets are merged at read time
return tweet_id
def get_timeline(user_id, page_size=20):
precomputed = timeline_cache.get(user_id, page_size)
celeb_ids = follower_graph.celebrities_followed(user_id)
live_celeb_tweets = tweet_store.recent_by_authors(celeb_ids, page_size)
merged = merge_by_recency(precomputed, live_celeb_tweets)
return ranking_service.rank(user_id, merged)[:page_size]Follow-up Questions
- How would you handle a user unfollowing someone whose tweets are already fanned out into their timeline?
- How would you rank the feed beyond pure recency — what signals would you use?
- How do you avoid re-fanning-out a deleted tweet to millions of already-populated timelines?
- How would you shard the timeline cache to handle hundreds of millions of users?
MCQ Practice
1. Why do celebrity accounts typically use fan-out on read instead of fan-out on write?
Fanning out a single celebrity tweet to millions of timelines on write causes massive write amplification, so it is instead merged at read time.
2. What is the main benefit of fan-out on write for regular users?
Precomputing timelines on write means reads are just fast cache lookups rather than expensive fan-in queries.
3. What role does the ranking service play after fan-out/merge?
Ranking takes the merged chronological candidate set and reorders it using engagement, recency decay and affinity signals.
Flash Cards
Fan-out on write? — Precomputing and pushing a new post into every follower’s timeline at publish time.
Fan-out on read? — Merging an author’s posts into a follower’s timeline only when the follower requests their feed.
Why hybrid fan-out? — Fan-out on write does not scale for celebrities; fan-out on read is too slow for normal-user reads at scale.
What sits behind the timeline cache? — A durable tweet store that is the source of truth, decoupled from the fast read-path cache.