Design a News Feed
Intermediate · 30 min read · High-Level Design, Case Studies: Core
Fan-out on write vs read, ranking and celebrity users.
Design the home feed of a social network (Twitter/X, Instagram, Facebook): users follow others, post content, and see a feed of recent posts from the people they follow.
1. Clarify the problem
2. Requirements
Functional: publish a post; follow/unfollow; view home feed (paginated); view a user's own profile timeline.
3. Capacity estimation
So: reads outnumber posts 25:1, and feed reads must be fast — precomputing feeds is attractive. But fan-out writes are 200× the post rate, and celebrity posts explode it. That tension drives the design.
4. API
Cursor pagination is mandatory — new posts constantly arrive at the top. See API Design.
5. Data model
Posts and social-graph edges are huge, append-mostly and accessed by key → wide-column stores (Cassandra) or sharded MySQL (Twitter and Facebook historically used heavily sharded MySQL). Media lives in object storage behind a CDN.
6. The core decision: fan-out on write vs read
When a user posts, push the post ID into every follower's precomputed feed.
7. High-level design
8. Deep dives
The feed cache holds only IDs. Hydration batch-fetches post content, author profiles and counters (likes, replies), mostly from caches (post cache, user cache). Storing IDs instead of full posts keeps the feed cache small and means edits/deletes are reflected automatically.
9. Failure modes
10. Trade-offs summary
11. What interviewers look for
Interview questions
- A celebrity with 100 million followers posts. Walk through what happens in your design.
- Why store only post IDs in the feed cache rather than whole posts?
- How would you handle a user who follows 5,000 accounts and hasn't logged in for six months?
Practice
Write your answer before opening the suggested one. Anything you mark "Partly" or "Missed it" comes back in your review deck.
Further reading
Key takeaways
- The heart of the problem is fan-out on write (fast reads, expensive writes) vs fan-out on read (cheap writes, slow reads).
- Use a hybrid: push for normal users, pull for celebrities, and skip inactive users.
- Cache post IDs per user, hydrate from post and user caches, and filter deletes and blocks at read time.
- Give authors read-your-writes by merging their own recent posts.
- Ranked feeds add candidate generation and scoring on top of the same infrastructure.