Step 1 · The pattern
Copy each post to every follower
When someone posts, the fan-out workers copy the post into each follower's timeline (each dot is a batch of followers). Opening the app is then a single cheap read of your own timeline. Ordinary accounts post 2 times a second here, and the workers keep up easily. (All numbers are illustrative.)
- post
- copied to a timeline
In short
Fan-out on write vs read: how it works and how it breaks
A home timeline can be built in two ways. Fan-out on write copies each new post into the timeline of every follower, so reading a timeline is one cheap lookup. Fan-out on read stores each post once and gathers posts from everyone a person follows when they open the app, which makes reads expensive. Most apps fan out on write, because reads far outnumber posts. The exception is the account with millions of followers: one post becomes millions of timeline writes, the fan-out workers spend minutes on it, and everyone else's posts wait in the same queue. Large social apps use a hybrid. Ordinary accounts fan out on write; accounts above a follower threshold do not. Their posts are stored once and merged into a follower's timeline when it is read, which costs each read a little and the system a lot less.
What this simulation simplifies
- Each dot is a batch of followers, so an ordinary post is two writes and the celebrity's is thirty. Real fan-outs are millions of writes, done over minutes.
- Every follower's timeline is written. Real systems often skip followers who have not been active recently.
- Reading a timeline in the fix merges one store of large accounts' posts. Real readers may follow several large accounts, each adding a little read cost.
How this comes up in interviews
"Design the home timeline for a social app with a billion users, where some accounts have tens of millions of followers."
Then they ask
- Why not fan out on read for everyone?
- How do you choose the follower threshold for the hybrid?
- How does a user see their own post immediately if fan-out is asynchronous?
A strong answer covers
- Fan-out on write makes reads a single lookup; reads far outnumber posts, so it is the default.
- Its cost is followers times posts; one huge account floods the shared fan-out queue and delays everyone.
- Hybrid: fan out ordinary accounts on write, store very large accounts' posts once and merge them on read.
- Set the threshold where fan-out cost exceeds merge cost, and keep merged accounts per read small.
- Fan-out is asynchronous: show authors their own post at once and track fan-out delay as a service level.
Points to cover, not a script. Say them in your own words and tie them to a system you have worked on.
On one page: the track cheat sheet.
Sources: Raffi Krikorian: Timelines at Scale (QCon San Francisco 2012, InfoQ) ↗ · High Scalability: The architecture Twitter uses to deal with 150M active users (notes on the talk) ↗ · Report a problem
Diagrams are our own simulations: the timings, rates and counts in them are illustrative, not measurements. Captions are original wording.