# News Feed System Design: Fan-Out on Write vs Fan-Out on Read

> How to design a news feed: fan-out on write versus fan-out on read, the celebrity problem, Redis sorted set feeds, cursor pagination and ranking, with the arithmetic shown.

URL: https://www.matthewswong.com/en/blog/design-news-feed-fan-out-on-write-vs-read/

- Author: Matthews Wong (https://www.matthewswong.com/en/about/)
- Published: 2026-10-10
- Updated: 2026-10-10
- Category: Backend

> **Key takeaway:** To design a news feed, use fan-out on write for ordinary accounts: when someone posts, push the post id into each follower's Redis sorted set, so a feed read is one lookup. Skip the push for accounts with huge followings and merge their posts at read time. Page with a cursor and rank only the newest few hundred candidates.

A feed looks like one query: show me the latest posts from the people I follow. On a small table that is exactly what it is, a join and an ORDER BY, and it is fine. Then you write down how often feeds are opened compared with how often anyone posts, and that join becomes the most expensive thing in the system.

This is a worked design, not a war story. I have not run a social network, so every number below comes from stated assumptions with the arithmetic shown, and the Redis behaviour comes from its documentation. The stack is the one I would reach for on a single VPS: Postgres as the source of truth, Redis for the feeds and a NestJS service in front.

## What do fan-out on write and fan-out on read mean?

Fan-out is one event spreading to many destinations. Wikipedia describes it in messaging as delivering a message to one or multiple destinations, possibly in parallel. In a feed, the event is a new post and the destinations are the timelines of everyone who follows the author. The only real decision is when you do that spreading.

- Fan-out on write (push): when a post is created, copy its id into every follower's precomputed feed. Opening a feed is one lookup.
- Fan-out on read (pull): store the post once, under its author. When a user opens the feed, fetch the newest posts of every account they follow and merge them.
- Hybrid: push for ordinary authors, pull for authors with a very large following, and merge both when the feed is read.

Every feed design is a choice about where to pay: at write time, once per follower, or at read time, once per account followed. Which is cheaper depends on how those two counts compare against how often each event happens, so the next section puts numbers on it.

## How much work does each model actually do?

Write the assumptions down first and derive everything from them. These are inputs for a design exercise on a single VPS, not measurements from a real product, and you should replace them with your own logs.

Pull does 300 million outbox lookups a day against push's 15 million feed inserts, a factor of 20, and that factor is simply reads per post: 2,000,000 feed opens divided by 100,000 posts. Whenever a feed is opened more often than anyone posts, which is nearly always, push is the cheaper side to pay. Its price is that the work happens whether or not the follower ever opens the app.

| Model | Work per post | Work per feed open | Main weakness | Pick it when |
| --- | --- | --- | --- | --- |
| Fan-out on write (push) | 150 inserts on average | 1 range query | A huge account turns one post into a burst of writes; inactive followers still get the work | Feeds are opened far more often than people post and follower counts are bounded |
| Fan-out on read (pull) | 1 insert | 150 outbox lookups plus a merge | Read latency grows with the number of accounts followed | The graph is small, or most users rarely open the feed |
| Hybrid | 150 inserts for ordinary authors, 1 for large ones | 1 range query plus about 2 outbox lookups | Two code paths and a threshold to tune | A few accounts hold a large share of all follow edges |

The table shows why neither model is free. Push moves the cost to writes and storage. Pull keeps storage tiny but makes every read slower and less predictable, because its latency scales with how many accounts the reader follows.

## What is the celebrity problem and how does a hybrid fix it?

Averages hide the account that breaks push. Assume one author has 500,000 followers, half the user base. A single post from that account is 500,000 inserts, which is a very different event from the average post at 150.

The hybrid rule is a threshold on follower count. Below it, push as usual. At or above it, do not fan out at all: write the post only to the author's own outbox and merge those outboxes into the reader's feed when it is read. Readers follow only a handful of such accounts, so the merge costs a few extra lookups instead of hundreds.

The threshold is a tuning knob, not a constant of nature. I would start at 10,000 followers, a number I picked rather than measured, then watch fan-out queue lag and move it. Moving it is safe because every feed can be rebuilt from Postgres.

> Put only post ids in the feed, never bodies. A feed entry then costs the same however long the post is, an edit needs no fan-out, and a deleted post is dropped when you hydrate ids from Postgres.

## How do you store a feed in Redis sorted sets?

Postgres stays the source of truth for posts and follows. Redis holds one sorted set per active user, with the post id as the member and the same id as the score, so newest-first reading is a range query. The Redis docs give ZRANGE a cost of O(log(N)+M) for N elements and M returned, and ZREMRANGEBYRANK the same shape, which is why trimming after every insert is cheap.

A fan-out worker does the writing, outside the request that created the post: insert the post, enqueue a job, return. The worker walks the follower list in keyset pages and writes to Redis in pipelines.

Trimming bounds memory. 200 entries per active feed times 200,000 active feeds is 40,000,000 entries. At an assumed 50 bytes each that is 2.0 GB, and that 50 is a placeholder: replace it with what MEMORY USAGE reports on your own data before trusting the total. A user who scrolls past 200 entries falls through to a Postgres query.

> A Redis feed is a cache, not a record. If it is flushed, fan-out lagged, or a follower was skipped, the answer is to rebuild from Postgres, never to treat Redis as the only copy. Design the rebuild path on day one, because you will use it.

## How do you paginate a feed with a cursor?

A feed changes while people read it, so page numbers drift: a new post pushes everything down and page 2 repeats an item from page 1. I covered the general case in the cursor versus offset pagination post. Here the cursor is simply the id of the last item returned.

With BYSCORE and REV, ZRANGE takes the highest score first, and a leading parenthesis makes a bound exclusive, so the next page starts strictly below the last id. This only works when no two entries share a score, because Redis orders equal scores lexicographically and a tie at a page boundary would be skipped. A unique id as the score avoids that. The docs also warn that a large LIMIT offset forces Redis to walk past that many elements, which is the other reason to use a cursor and not an offset. Doubles hold integers exactly up to 2 to the power 53, about 9.0e15, far above any realistic row count.

## Where does ranking fit in?

Ranking is a second stage on top of the feed, not a different storage model. Get candidates cheaply first: the newest ids from the feed plus the large-account outboxes. Then score and sort only those. Twitter's open-sourced recommendation repository describes the same shape, with candidate sources, a light ranker, a heavy neural ranker, visibility filters and a mixer, and says about half of the posts in the For You timeline come from the in-network source.

For a first version, a transparent formula beats a model you cannot debug. This one is my own illustration, not a recommendation from any source, so tune the weights against what your users actually do.

Ranking breaks the pure score cursor, because the returned order is no longer the stored order. Either snapshot the ranked id list for a few minutes and page by position inside the snapshot, or keep chronological paging and rank within each page. Decide which you ship and say so in the product, because users notice when a feed reshuffles under them.

## Which model should you choose, and what breaks first?

Run this checklist before building anything.

Compute reads per post from your own logs. If feeds are opened far more often than anyone posts, pay at write time.

Plot the follower distribution. If a few accounts hold a large share of all follow edges, add a threshold and the hybrid path.

If you have a few thousand users, ship the single indexed SQL query first. It is correct, and you can measure before you add Redis.

Keep Postgres as the source of truth and make the feed rebuild a tested job, not a runbook paragraph.

Make every fan-out job idempotent. Adding the same member with the same score again changes nothing, so retries are safe.

What breaks first is rarely the arithmetic. Deleted posts and unfollows leave ghost ids in feeds, so hydrate from Postgres and drop what no longer exists. A flushed Redis serves empty feeds until you rebuild lazily on the first read from each followed account's recent posts. And fan-out lag during a burst makes new posts appear late, so alert on queue depth, not on CPU.

Pay for fan-out where the traffic is cheap to predict. Reads outnumber posts, so push ordinary authors into a capped Redis sorted set per user, treat large accounts as pull, page with a cursor, and rank only a few hundred candidates. Keep Postgres as the truth and every other layer disposable.

## Sources and further reading

- Redis docs: sorted sets
- Redis docs: ZRANGE with BYSCORE, REV and LIMIT
- Redis docs: ZREMRANGEBYRANK
- Twitter on GitHub: the-algorithm recommendation pipeline
- Wikipedia: Fan-out (software)

## Source links

- https://redis.io/docs/latest/develop/data-types/sorted-sets/
- https://redis.io/docs/latest/commands/zrange/
- https://redis.io/docs/latest/commands/zremrangebyrank/
- https://github.com/twitter/the-algorithm
- https://en.wikipedia.org/wiki/Fan-out_(software)

## FAQ

### What is the difference between fan-out on write and fan-out on read?

Fan-out on write copies a new post into every follower's precomputed feed when it is created, so opening a feed is a single lookup. Fan-out on read stores the post once under its author and gathers the posts of every followed account when the feed is opened. Write pays per follower and read pays per account followed, so the cheaper one depends on how often feeds are opened versus how often people post.

### What is the celebrity problem in news feed design?

It is the case where one account has so many followers that a single post triggers a huge burst of fan-out writes. With 500,000 followers, one post is 500,000 inserts, which in the worked example equals 48 minutes of average write load. The usual fix is a hybrid: skip the push above a follower threshold and merge that account's posts into feeds at read time.

### How do you store a news feed in Redis?

Use one sorted set per active user, with the post id as the member and the same unique id as the score. Add with ZADD, trim the oldest entries with ZREMRANGEBYRANK to keep a fixed cap such as 200, and read newest-first with ZRANGE using BYSCORE and REV. Keep the post bodies in Postgres and treat the Redis feed as a rebuildable cache.

### Why use cursor pagination instead of page numbers for a feed?

A feed changes while someone reads it, so a new post shifts every item down and page 2 can repeat an item from page 1. A cursor, here the id of the last item returned, asks for entries strictly older than that point and stays stable. It also avoids the cost Redis documents for large LIMIT offsets, which must walk past that many elements.

### Should a small app use fan-out on write?

Usually not at first. With a few thousand users, one indexed SQL query that joins follows to posts is correct and easy to measure. Move to fan-out on write only when you can show that reads per post are high and that the query is the bottleneck, and keep Postgres as the source of truth when you do.

---

- HTML version: https://www.matthewswong.com/en/blog/design-news-feed-fan-out-on-write-vs-read/
- Bahasa Indonesia: https://www.matthewswong.com/id/blog/design-news-feed-fan-out-on-write-vs-read/
- Site index for agents: https://www.matthewswong.com/llms.txt
- Sitemap: https://www.matthewswong.com/sitemap.xml
- API description (OpenAPI): https://www.matthewswong.com/openapi.json
