Building at scale · L2Fan-out on write vs fan-out on read

Fan-out on write vs fan-out on read

Learn the two ways a post reaches millions of feeds, what each one costs, and how big apps mix them for accounts with millions of followers.

L2 SeniorBuilding at scale21 min read
Fact-checked by AI against cited sources · Human review pendingLast reviewed 8 Nov 2026 · How we check content
Section 1 of 8

What broke

Around 2012, Twitter had about 150 million active users. The Twitter numbers in this lesson come from a detailed 2013 summary of a talk by Raffi Krikorian. 1 Your home timeline was a ready-made list in a large Redis cluster. When someone tweeted, Twitter copied the tweet's ID into the list of every follower. 1

For most accounts this worked well. A tweet to 1 million followers took a couple of seconds to reach them all. The goal was under 5 seconds. 1

Then a few accounts had tens of millions of followers. A tweet from Lady Gaga could take up to 5 minutes to reach her 31 million followers. Queues for these large fan-outs backed up all the time. 1

The delay caused strange bugs. A fan who got her tweet early could reply at once. Other fans saw the reply before the tweet it answered. 1 This is likely because the reply had a small audience, so it was delivered fast.

Twitter's plan was to stop pushing tweets from the biggest accounts. Instead, it would merge their tweets into your timeline when you read it. 1 Raffi Krikorian described Twitter's timelines in a talk at QCon San Francisco in late 2012. 2

Section 2 of 8

The idea in simple words

A feed shows posts from many people you follow. You can do the gathering work at one of two times.

Fan-out on write (push). When someone posts, copy the post's ID into a list for each follower. Opening your feed is then one cheap read of your own list.

Fan-out on read (pull). Store each post once, with its author. When you open your feed, fetch recent posts from everyone you follow and merge them. Writing is cheap. Every read does the work.

Neither one wins every time. Push hurts when one author has millions of followers. This is the celebrity problem. Pull hurts when one reader follows thousands of accounts, and readers open the app all day.

The key idea. Push pays per follower, at write time. Pull pays per followed account, at read time. Most big feeds push for normal accounts and pull for the few huge ones.

Section 3 of 8

How it works

Step through one normal post and one post from a huge account. Use Next and Back, or the ← and → keys.

One normal post is pushed to followers' lists, one star post is pulled and merged at read timeRavi5148… Meena5044… Liaway: no list in cache Asha posts 57queuejob: fan out 57 workers write 57 into each active list5757each list keeps only the newest IDs Star posts 5830 M followersStar's posts: 58 …no fan-out: 1 write Ravi opens the appRavi's list: 57 51 48+Star's posts: 58✓ merged: 58 · 57 · 51 · 48 Li logs in after a month awayrebuild: who Li follows → their posts SeniorPath
  1. Asha has a few hundred followers. When she posts, the app saves post 57 once and puts one small job on a queue: fan out post 57.
  2. Workers read Asha's follower list. They add the ID 57 to the top of each active follower's list in the cache. Each list is capped, so the oldest ID falls off the end. Li has been away, so the workers skip her.
  3. Star has 30 million followers. Pushing her post would mean 30 million writes. So her posts are not fanned out. Post 58 is saved once, in Star's own list of posts.
  4. Ravi opens the app. The server reads his ready-made list and the latest posts of the few stars he follows. It merges them by time or by score. One cache read plus a few small pulls.
  5. Li comes back after a month. Her list is gone from the cache. The server looks up who she follows, reads their recent posts from the database, and builds her list again.
Step 1 of 5

Do the cost maths first

Write cost with push is posts per second × average followers. Read cost with pull is feed opens per second × accounts followed. Compare the two before you choose.

Example with made-up numbers: an app gets 1,000 posts a second, and the average author has 200 followers. Push makes 200,000 list writes a second. Each write is tiny, and reads become one lookup.

Now one author has 30 million followers. One post from her is 30 million writes. That is 150 seconds of the whole app's normal push work, for one post.

Real apps read much more than they write. Around 2012, Twitter served about 300,000 timeline reads a second, but only about 6,000 write requests a second. 1 With that ratio, doing the work once at write time is usually the better deal.

Store timelines as capped lists of IDs

Keep only IDs in the list, not the full post. Twitter stored the tweet ID, the author's ID and 4 bytes of flags, such as "this is a retweet". 1 Each home timeline was capped at 800 entries, and each timeline entry was kept on 3 machines in the Redis cluster. 1

When a reader opens the app, the server loads full posts for the IDs it shows. Short IDs keep the list small, and an edited post changes in one place only.

Mastodon, the open-source social server, works the same way today. Its home feeds live in Redis. Each feed keeps at most 800 items, and a trim step removes anything older. 7

Skip inactive users

Most people do not open the app every day. Pushing posts into their lists wastes memory and work.

Twitter kept only active users in Redis, meaning people who had logged in within 30 days. Others got no entries. When they came back, Twitter rebuilt their timeline. It looked up who they follow, read those posts from disk and put the list back in Redis. 1

Mastodon pushes posts only to local followers who signed in within the last 7 days by default. 6 8 Feeds are kept for the same 7 days. 8 When such a user signs in again, Mastodon queues a job that rebuilds the feed from the accounts they follow. 9 7 A code comment states the trade-off. A shorter window saves work when many people sign up but rarely visit. A longer window means less catch-up work when people come back. 8

The cost is a slow first load for a returning user. Show a "loading your feed" state, not an empty screen.

Fan out through a queue

Never fan out inside the request that saves the post. Save the post, put one job on a queue, and answer the user. Workers do the copying later.

Mastodon's fan-out code reads followers in batches. It then queues one small job per follower, and each job adds the post to that follower's feed. 6 Its queues run in priority order. Posts from other servers go to a lower-priority queue, "so that local users still see their posts when the server is under load". 10

Notifications use the same path. At Twitter, delivery reached in-memory timelines, push notifications and emails. 1 Your workers rarely send to each phone one by one. They call a push service. Firebase Cloud Messaging can send one message to every device subscribed to a topic. Its docs say topic messages are built for throughput, not low latency. They also limit each project to 1,000 fan-outs running at once. 11 For urgent alerts to a few people, Firebase advises sending to device tokens instead. 11

For delivery guarantees and retries, see Queues vs logs.

Fill in old posts when someone follows

Push only adds new posts. If you follow Asha today, her older posts are not in your list. You must merge them in once.

Mastodon's feed code has a merge step for this. It copies up to 200 recent posts from the new account into your feed. It does this only for users who signed in recently. If your feed already has 200 or more items, it skips posts older than the oldest one in it. Then it trims the feed back to 800. 7 A reverse step removes an account's posts from your feed after an unfollow. 7

Merge and rank at read time

The hybrid reads two things. It reads your pushed list, and it reads the latest posts of the few huge accounts you follow. Then it merges them by time, or by a score.

Ranking pushes many apps further toward pull. In 2015, Facebook described its News Feed backend this way. "Leaf" servers kept recent actions in memory. An aggregator sent each query to the leaves, then ranked what came back. 4 In other words, Facebook built the feed when you asked for it.

LinkedIn chose pull in 2016. Its tests found that pre-built feeds needed 62 times more data. 5 Pre-built feeds are also stored in ranked order. So testing a new ranking model means re-sorting every stored feed. 5

By 2023, X's published code built the For You and Following tabs at read time. The Following tab fetched posts from the Earlybird search index. The For You tab gathered candidates from several sources, then scored them with a model using about 6,000 features. 3

Section 4 of 8

Trade-offs

OptionWrite costRead costBest for
Fan-out on writeHigh. One write per active follower. Very high for huge accounts.Low. One list read, then load the posts.Read-heavy apps where most accounts have a modest audience.
Fan-out on readLow. One write per post.High. One lookup per followed account, then a merge.Ranked feeds, users who follow few accounts, and early products.
HybridPush for normal accounts, nothing extra for huge ones.One list read plus a few pulls.Big social apps with a few accounts that have millions of followers.
Push to active users onlyLower. Inactive users cost nothing.Slow first load when a user returns.Apps where many users visit rarely.

Simple rule: start with pull while you are small. Add push when reads hurt. Add the hybrid when one account's post takes minutes to deliver.

Section 5 of 8

At work: how a senior uses this

  • Multiply first. Ask: "Posts per second × average followers, and feed opens per second × accounts followed. Which is bigger?"
  • Ask for the follower count at p99 and the maximum, not the average. The largest account decides your worst case.
  • Pick a clear threshold for "huge account", and store it as a setting. Watch how many accounts cross it.
  • Watch fan-out lag: the time from "post saved" to "last follower's list updated". Alert on it.
  • Keep timelines rebuildable. The database is the truth, and the cache list is a copy you can throw away.
  • Decide what happens on block, delete and unfollow. Each one must remove posts from lists that are already built.
Section 6 of 8

With AI

What to ask the agent

"We have 2 million users. The average user follows 150 accounts and opens the app 8 times a day. Users make 500,000 posts a day, and the largest account has 4 million followers. Estimate writes per second for fan-out on write and reads per second for fan-out on read. Show the maths."

"Write a fan-out worker in Node with BullMQ and Redis. It reads followers in batches of 1,000, skips users not seen in 14 days, adds the post ID with ZADD (score = post ID), and trims each feed to 800 with ZREMRANGEBYRANK. Make a retried job safe."

"Add read-time merge for accounts with more than 1 million followers. Merge their latest posts into the cached list by post ID, and remove duplicates."

What to check yourself

  • The worker does not load all followers into memory at once. Agents often write getAllFollowers() and loop.
  • A retried job does not add the same post twice. Check for duplicates or use a sorted set keyed by post ID.
  • The list is trimmed after every insert, so memory stays bounded.
  • Deletes, blocks and unfollows remove posts from lists that are already built.
  • The merge sorts by a stable key, such as a time-ordered post ID, so pages do not repeat posts.
Section 7 of 8

Check yourself

1. An account with 2,000 followers posts 10 times a day. With pure fan-out on write, about how many timeline writes does that make each day?

Show answer

20,000 Each post is written once per follower, so 10 posts × 2,000 followers = 20,000 writes. The number of followers multiplies every post.

2. Twitter's numbers from 2012–13 showed about 300,000 timeline reads a second and about 6,000 write requests a second. Why does this favour fan-out on write for most accounts?

Show answer

Reads are about 50 times more common, so it pays to do the work once at write time and keep reads cheap. When reads far outnumber writes, building each timeline in advance moves work from the busy path to the quiet one. The cost grows with audience size, so it becomes a problem for accounts with huge audiences.

3. Your app pushes posts into followers' cached lists. A user with 30 million followers starts posting. What is the usual fix?

Show answer

Stop fanning out that account's posts, and merge them into each reader's timeline at read time. This is the hybrid. Most posts are pushed, but very large accounts are pulled and merged when a reader opens the app. Twitter described this plan in its 2012 talk, as reported in a 2013 summary.

4. In Mastodon's code, what happens to a user who has not signed in for more than 7 days (the default)?

Show answer

New posts are no longer pushed to their home feed, and the feed is rebuilt when they come back. Fan-out only goes to followers who signed in recently, and a user returning after the active window gets a background job that rebuilds the feed.

5. LinkedIn chose fan-out on read for its feed in 2016. Which reason did it give?

Show answer

Pre-built feeds needed about 62 times more data, and made testing new ranking models slow. Their experiments found the pre-built model's data was 62 times larger. Pre-built feeds are also stored in ranked order, so each new ranking model means re-sorting every feed.

Your progress is saved only in this browser. Export it to keep it safe.

Was this clear?
Report a mistake (opens GitHub in a new tab)
Section 8 of 8

Go deeper

Show 11 sources and related lessons
  1. High Scalability, “The Architecture Twitter Uses to Deal with 150M Active Users, 300K QPS, a 22 MB/S Firehose, and Send Tweets in Under 5 Seconds”, July 2013 (opens in a new tab)
    A detailed summary of Raffi Krikorian's QCon San Francisco 2012 talk, and the source of every Twitter number in this lesson. Redis timelines capped at 800 entries, active users, reconstruction, and the trouble with very large accounts.
  2. Raffi Krikorian, “Timelines at Scale”, QCon San Francisco 2012, published on InfoQ April 2013 (opens in a new tab)
    The original talk (video and slides) by Raffi Krikorian, then Senior Director of Twitter's Applications Services group, on how tweets, social graph changes and direct messages flow through the system.
  3. X (Twitter), “Home Mixer” README, the-algorithm repository on GitHub, 2023 (opens in a new tab)
    How the For You and Following timelines were built at read time in the code X published in 2023.
  4. Yufei Zhu, Facebook Engineering, “Serving Facebook Multifeed: Efficiency, performance gains through redesign”, March 2015 (opens in a new tab)
    News Feed as aggregators and leaves. Leaves keep recent actions in memory, and aggregators query them and rank the results.
  5. Ankit Gupta, Swapnil Ghike and Shubham Gupta, “FollowFeed: LinkedIn's Feed Made Faster and Smarter”, LinkedIn Engineering, March 2016 (opens in a new tab)
    Why LinkedIn chose fan-out on read, with a measured storage difference and the cost of testing new ranking models.
  6. Mastodon source code, FanOutOnWriteService (app/services/fan_out_on_write_service.rb), November 2026 (opens in a new tab)
    A real fan-out on write. One background job per follower, queued in batches, only for recently active local followers.
  7. Mastodon source code, FeedManager (app/lib/feed_manager.rb), November 2026 (opens in a new tab)
    Home feeds as Redis sorted sets capped at 800 items, merging a new account's posts on follow, and rebuilding a feed.
  8. Mastodon source code, User::Activity (app/models/concerns/user/activity.rb), November 2026 (opens in a new tab)
    The 7-day "active" window that decides who gets fan-out and whose feed is kept, with the reasons in a code comment.
  9. Mastodon source code, User model (app/models/user.rb), November 2026 (opens in a new tab)
    On sign-in after the active window, a returning user gets a background job that rebuilds the home feed.
  10. Mastodon documentation, “Scaling up your server” (opens in a new tab)
    The Sidekiq queues behind posting, in priority order, and why remote work runs below local work.
  11. Firebase documentation, “Topic messaging on Android” (opens in a new tab)
    Push notifications to many devices through topics, built for throughput, with a limit on fan-outs running at once.

Case studies: Discord: Fanning out messages to millions of users with Elixir · Google: The Tail at Scale: rare slowness becomes common at scale

Related: Scaling reads · Queues vs logs and delivery guarantees · Presence and real-time fan-out · Caching and cache stampedes · Sharding and consistent hashing

Term

See full entry

Jump to a section

  1. 1What broke
  2. 2The idea in simple words
  3. 3How it works
  4. 4Trade-offs
  5. 5At work
  6. 6With AI
  7. 7Check yourself
  8. 8Go deeper