News Feed Fan-out
Overview
When someone posts, how do all their followers see it quickly? Fan-out is the pattern of delivering one post to many feeds. Choosing when to do that work is a classic design trade-off.
Detailed Notes
What is fan-out
Fan-out means one write spreading out to many destinations. In a news feed, one post must show up in the feed of every follower.
The problem it solves
A user opens the app and expects a feed of recent posts from hundreds of accounts in under 200 ms. Building it from scratch each time (asking every followed account for posts and merging) is slow. But copying every post to every follower in advance is wasteful for an account with 50 million followers. We need to decide where to spend the work: when posting or when reading.
How it works
| Approach | How | Pros | Cons |
|---|---|---|---|
| Fan-out on write (push) | When a post is created, add its id to each follower's precomputed feed in a cache | Very fast feed reads | Expensive writes for huge accounts; wasted work for inactive followers |
| Fan-out on read (pull) | On opening the feed, fetch recent posts from each followed account and merge | Cheap writes, always fresh | Slow reads for users who follow many accounts |
| Hybrid | Push for normal users, pull for celebrities and merge at read time | Balances both | More complex logic |
Typical components: a post service that stores the post once, a queue feeding fan-out workers, a feed cache (a list of post ids per user), and a store for the social graph (who follows whom). Feeds hold only ids, not full posts, which keeps them small.
A simple example
A newspaper delivered to your door is push: the work happens at printing time and reading is instant. Walking to the shop each morning and gathering sections yourself is pull: no advance work, slower reading. A hybrid delivers most papers to the door, but for a few giant publishers you pick up that one yourself.
System design example
Reads outnumber writes by about 100 to 1 or more, so feeds should be mostly precomputed. A normal user posts: the post is saved, and a queue triggers workers that push the id into about 300 follower caches. A celebrity with 30 million followers posts: pushing would create a write storm, so their posts are skipped during fan-out and pulled and merged into each follower's feed when it is opened. Workers are idempotent so queue retries do not duplicate posts.
Summary
- Fan-out delivers one post to many feeds.
- Push makes reads fast but writes costly; pull does the reverse.
- Hybrid pushes for normal users and pulls for celebrities.
- Store posts once; feeds hold only ids.
Architecture Diagram
Posts are stored once and ids are pushed to follower feeds.
- 1The post is saved once in the post store.
- 2A message goes to the fan-out queue.
- 3Workers push the post id into each follower's feed cache.
- 4Celebrity posts are pulled and merged at read time.
Common Interview Questions
Chapter quiz
Miscellaneous System Design Concepts · 5 questions
1.With hash mod N placement, what happens when N changes from 4 to 5?
2.What do virtual nodes provide in consistent hashing?
3.How do idempotency keys prevent double charges?
4.In a saga, a failed step triggers:
5.Why use a hybrid fan-out for feeds?
0 of 5 answered
Finished this lesson?
0 of 91 lessons done · 0% of the course

