Fan-out on Write vs. Read
By Ankur NayakReviewed
Fan-out is how one event reaches many recipients. Fan-out on write pushes a post into every follower's precomputed timeline when it is created; fan-out on read assembles a timeline from the people you follow when you open the app. Large feeds use a hybrid.
How it works
Fan-out on write (push): when a user posts, a background job inserts the post ID into a cached timeline for each follower. Reading a feed is then a single, fast lookup. The cost is paid at write time and grows with follower count, which is a problem for accounts with millions of followers.
Fan-out on read (pull): nothing happens at write time; when a user opens their feed, the system fetches recent posts from everyone they follow and merges them. Writes are cheap, but every read does many lookups, which is slow for users who follow many accounts.
The hybrid used by large social networks pushes posts from ordinary accounts and pulls posts from very popular accounts at read time, merging the two. The same trade-off appears in group chat (copy the message to every member's inbox vs. store once per group) and notifications.
Key points
- Push: cheap reads, expensive writes
- Pull: cheap writes, expensive reads
- Hybrid: push for most, pull for celebrities
- Do fan-out asynchronously through a queue
When to use it, and pitfalls
Use it for
- Social feeds and timelines
- Group messaging and notifications to many recipients
Watch out for
- Celebrity accounts creating millions of writes per post
- Wasted work pushing into timelines of inactive users
- Deletes and edits must fan out too
Trade-offs
Push vs. pull for the home timeline
Hybrid: push for accounts below a follower threshold, pull for those above.
Pros: Fast reads for nearly everyone; Bounded write amplification
Cons: Two code paths and a merge step at read time
Used in
Related building blocks
Frequently asked questions
How do social networks handle celebrities with millions of followers?
They skip fan-out on write for those accounts. When a follower opens their feed, the precomputed timeline (from ordinary accounts) is merged with recent posts fetched directly from the few celebrities they follow.
Sources
- System Design Primer: Design the Twitter timeline and search · GitHub
- Timelines at Scale · InfoQ (QCon)