SysDesignPrep.com
Interactive concepts

Fan-out on write vs fan-out on read: an interactive news feed simulator

A news feed can be built two ways. Fan-out on write, or push, copies each new post into every follower’s precomputed feed, so reading a feed is a single lookup. Fan-out on read, or pull, stores each post once and assembles a feed when it is opened, from everyone the reader follows. Push makes reads cheap and writes expensive, pull the reverse, and an account with millions of followers breaks push, so large systems use a hybrid.

The playground sizes a feed with 5,000 ordinary posts a second, a celebrity with as many followers as you choose, and hundreds of thousands of feed loads a second. Compare the work each approach does and how long the celebrity’s post takes to reach everyone.

Celebrity followersFeed loads a secondHybrid pulls accounts above

5,000 ordinary posts a second, each to 300 followers; a celebrity with 10M followers posting twice a second; 300,000 feed loads a second. Fan-out on write turns each celebrity post into 10M feed inserts: 21.5M a second in all, 430% of what the fan-out workers can do, so they fall further behind every second. Fan-out on read writes almost nothing, but every feed load reads all 300 accounts the reader follows: 90M lookups a second. The hybrid pushes ordinary posts and pulls only celebrities, so a load is 6 lookups and the workers stay at 30%: the least work of the three.

Fan-out on writepush each post into every follower’s feedfalling behind
Feed inserts a second
21.5M
Lookups a second
300k
Lookups per feed load
1
Fan-out workers busy
430%
Celebrity post reaches everyone
never
21.8M operations a second
Fan-out on readassemble each feed when it is read
Feed inserts a second
5k
Lookups a second
90M
Lookups per feed load
300
Fan-out workers busy
none needed
Celebrity post reaches everyone
at once
90M operations a second
Hybridpush ordinary accounts, pull celebritiesleast work
Feed inserts a second
1.5M
Lookups a second
1.8M
Lookups per feed load
6
Fan-out workers busy
30%
Celebrity post reaches everyone
at once
3.3M operations a second
feed insertslookups to serve feedsBars share one scale. Fan-out workers can do 5M inserts a second; each person follows 300 accounts, 5 of them celebrities.

Fan-out on write: push

Each user has a precomputed feed, typically a capped list of post ids in a cache such as Redis. When someone posts, fan-out workers look up their followers and insert the post id into each follower’s list. Reading a feed is then one lookup, which is why push suits feeds, where reads far outnumber writes. The cost of a post is its author’s follower count. For ordinary accounts with a few hundred followers that is fine; for a celebrity with 10 million it is 10 million inserts per post, and in the third preset two such posts a second need over four times what the fan-out workers can do, so they fall further behind every second. Push also does work for followers who will never open the app.

Fan-out on read: pull

Each post is written once, to its author’s list. Opening a feed fetches the recent posts of every followed account, merges them and ranks the result. Writing is trivial and a celebrity costs nothing extra, but every load does work proportional to how many accounts the reader follows: 300 lookups a load in the playground, 90 million a second at 300,000 loads a second. Feed loads are also latency-sensitive, and a merge across hundreds of sources is slow at the tail.

The hybrid: push most, pull the celebrities

Push posts from ordinary accounts, which are almost all of them, and skip fan-out for accounts above a follower threshold. At read time, take the precomputed feed and merge in the recent posts of the few celebrities the reader follows. In the playground that is 6 lookups per load instead of 300, and the fan-out workers stay at 30% busy however famous the celebrity. Twitter described this design for its home timelines: tweets fanned out to followers’ cached timelines, with the largest accounts merged in when a timeline is read.

Fan-out on writeFan-out on readHybrid
Cost of a postOne insert per followerOne writeOne insert per follower, below the threshold
Cost of a feed loadOne lookupOne per followed accountOne, plus one per followed celebrity
Celebrity postsSlow to deliver, can swamp the workersFreeFree
FreshnessDelayed by fan-outAlways currentCurrent for celebrities, near-current for the rest
StorageA feed per userPosts onlyA feed per user

Choosing the threshold

Set the threshold where pushing one account’s posts starts to cost more than pulling it on every read. Too high, and celebrities are still pushed: in the last preset the threshold of 100 million lets a 10 million follower account through, and the hybrid overloads exactly as push does. Too low, and every load pulls dozens of accounts. Two refinements matter in practice: skip fan-out to followers who have not been active recently, and rebuild their feed by pulling when they come back; and cap each cached feed at a few hundred entries, since nobody scrolls further.

In a system design interview

Quote the numbers that drive the choice: “We expect 300,000 feed loads a second and 5,000 posts a second, so reads dominate and we precompute feeds: each post is fanned out to followers’ feeds in Redis, capped at 800 ids. Accounts with more than a million followers are not fanned out; their recent posts are cached and merged in at read time. Inactive users get no fan-out and have their feed rebuilt when they return.” Then expect the follow-ups: ranking, deletes and edits, a user who follows thousands of accounts, and how long a post takes to appear.

Read the guide to fan-out on write vs read, see Kafka partitions and consumer groups for the queue that feeds the fan-out workers, or watch a strong candidate design a news feed.

Questions people ask

What is fan-out on write?

Building each user’s feed ahead of time: when someone posts, the post is inserted into the precomputed feed of every follower, usually a capped list of ids in a cache. Reading a feed is then a single lookup. The cost of a post grows with its author’s follower count, which is the approach’s weakness.

What is fan-out on read?

Building a feed when it is opened: posts are stored once per author, and loading a feed fetches the recent posts of every account the reader follows, merges and ranks them. Writes are cheap and celebrities cost nothing extra, but every load does work proportional to the number of accounts followed.

Which is better, fan-out on write or fan-out on read?

Fan-out on write suits feeds, where reads vastly outnumber writes and must be fast, as long as no account has a huge following. Fan-out on read suits systems with few reads per write or very uneven follower counts. Large social networks use a hybrid: push for ordinary accounts and pull for the few with millions of followers.

How do news feeds handle celebrities with millions of followers?

They do not fan their posts out. A celebrity’s recent posts are kept in a cache, and when a user loads their feed, the precomputed part from ordinary accounts is merged with the recent posts of the handful of celebrities they follow. This keeps a single post from creating millions of writes and delays.

What follower threshold should a hybrid feed use?

The point where pushing an account’s posts costs more than pulling them on each read of its followers’ feeds, often somewhere in the hundreds of thousands to a few million followers, depending on how often the account posts and how often its followers read. Too high a threshold lets fan-out overload; too low makes every feed load pull many accounts.

How are precomputed feeds stored?

Typically as a list per user in an in-memory store such as Redis, holding post ids rather than full posts, capped at a few hundred entries because users rarely scroll further. Post bodies are fetched separately by id from a cache or store. Feeds for inactive users can be dropped and rebuilt by pulling when they return.

How does Twitter build the home timeline?

As described by its engineers, tweets were fanned out on write into each follower’s cached timeline in Redis, so reading a timeline was a fast lookup, while tweets from accounts with very large followings were merged in at read time rather than fanned out. Ranking and other features have since been layered on top.