How do you build a news feed: fan-out on write or on read?
Fan-out on write precomputes each follower's timeline, so reads are instant, but one post from an account with millions of followers means millions of writes. Fan-out on read is cheap to write and expensive to read. Large feeds use both: push for most accounts, pull for the very popular ones.
The verdict, as we’re about to prove: most real feeds are hybrid.
- Fan-out on write (push): when someone posts, add the post’s ID to every follower’s precomputed timeline. Reading a feed is one lookup. Writing costs one insert per follower.
- Fan-out on read (pull): store posts only once; when someone opens the feed, fetch recent posts from everyone they follow and merge them. Writing is cheap; reading costs one query per followed account.
- Hybrid: push for ordinary accounts, pull for the few accounts with huge followings, and merge the two at read time. Feeds that need ranking add a scoring step on top.
Case file
The case A user waits for their feed while a huge account’s post makes the system groan Suspects Fan-out on write (push), fan-out on read (pull) Culprit Doing the work on the side that has more of it The fix Hybrid: push for ordinary accounts, pull for celebrities, merge at read
The case #
Somebody opens the app and waits for their feed. Somebody else, with an enormous following, hits “post”, and the system groans. The feed is one of those mysteries where everything turns on a single question: “when do we do the work?” And it has a clean answer. Move the work to whichever side has less of it, and when neither side wins outright, split the difference by account.
The two suspects #
The two pure approaches are mirror images of each other, and that symmetry is the first clue to why a hybrid works:
| Fan-out on write (push) | Fan-out on read (pull) | Hybrid | |
|---|---|---|---|
| Cost of a post | One write per follower | One write | One write per follower, except for celebrity accounts |
| Cost of opening the feed | One read of a precomputed list | One read per followed account, then a merge | One list read plus a few celebrity lookups |
| Read latency | Lowest | Grows with the number of accounts followed | Low |
| Freshness | Delay while fan-out runs (seconds for big accounts) | Always current | Mostly current |
| Storage | A timeline per user (IDs only) | Posts only | Timelines for most users |
| Weak spot | Accounts with millions of followers | Users following thousands of accounts | More moving parts |
Diagram source
flowchart TB
subgraph Write path
P[New post] --> S[(Posts table)]
P --> FQ[[Fan-out queue]]
FQ --> W[Fan-out workers]
W -->|for each follower, unless author is a celebrity| T[(Timelines: user → recent post IDs)]
end
subgraph Read path
R[Open feed] --> T
R -->|followed celebrities| S
R --> M[Merge, rank, hydrate posts] --> F[Feed page]
end
Use case 1: a social feed #
The scene: millions of users; most have a few hundred followers; a few have tens of millions; users open the app far more often than they post.
What I’d build: hybrid. Here’s the elegant bit: the expensive case for push (huge followings) is exactly the cheap case for pull (a handful of accounts to fetch), so each approach covers the other’s weakness. Neither suspect is guilty; each is the other’s alibi.
- Timelines are capped lists of post IDs per user (the newest few hundred), kept in a fast store (Redis sorted sets, which the leaderboard case looks inside, or a wide-column table keyed by user).
- On post: write the post once, then enqueue a fan-out job. Workers page through the author’s followers and append the post ID to each timeline. Skip inactive followers; they get a pull-based rebuild when they return.
- Celebrity accounts (above a follower threshold) aren’t fanned out. At read time, the feed service fetches their recent posts directly and merges them with the precomputed timeline.
- Hydration: the timeline holds only IDs; the feed service fetches post bodies, authors and counts in batches, from caches.
The fan-out queue quietly does a lot of work here. It absorbs bursts: a viral post delays some followers’ timelines by seconds instead of slowing the poster’s request (queue vs API case).
Use case 2: a B2B activity stream #
The scene: a SaaS shows each user what happened in their workspace: comments, status changes, mentions. Workspaces have tens to thousands of members, and permissions decide who sees what.
What I’d build: fan-out on read, from one events table.
- Activity is stored once per workspace, indexed by
(workspace_id, created_at). - Opening the stream is one indexed query for the workspace, filtered by what the user may see.
- Permissions change often (someone is removed from a project). With pull, the next read reflects that immediately; with push, you’d have to delete entries from timelines.
The number of “accounts followed” is effectively one (the workspace), so pull is cheap. Same question, opposite answer, for a clear reason. That’s the pleasure of a good rule: it convicts different suspects in different cases.
Use case 3: a notifications inbox #
The scene: each user has an inbox of things addressed to them: mentions, replies, approvals.
What I’d build: fan-out on write, because each notification already has a small, known set of recipients. Write one row per recipient with a read/unread flag; the inbox is a single indexed query. The notification system case covers delivery by email and push.
Ranking #
A chronological feed is a merge by time. A ranked feed adds a step:
- Candidate generation: take a few hundred recent posts from the timeline and celebrity pulls.
- Scoring: a model or rules score each candidate (relationship strength, engagement, recency, content type).
- Rules: diversity (not five posts in a row from one author), dedup, filtering blocked or hidden content.
The tempting wrong turn is precomputing ranked feeds for everyone. Keep ranking at read time over a bounded candidate set instead.
Pagination #
A small clue that saves a lot of confusion: use a cursor (the last post’s timestamp and ID), not page numbers. New posts arriving at the top would shift offset-based pages and show duplicates.
Remember this
- Push makes reads cheap, pull makes writes cheap.
- Big followings are expensive to push and cheap to pull, so each covers the other.
- Timelines hold IDs only, paginate with a cursor, rank over a bounded set.
What I’d do on Monday #
- Pull when each user sees few sources or permissions change often.
- Push with capped timelines for social follow graphs, through a queue.
- Hybrid once some accounts have very large followings: don’t fan out for them; merge at read time.
- Timelines hold IDs only; hydrate from caches in batches.
- Cursor pagination and ranking over a bounded candidate set.
It’s a lovely design to carry around in your head: the same two moves, push and pull, cover social feeds, activity streams and inboxes, as long as you keep asking where the work is cheapest.
Comments
No comments yet.
Sign in to comment. We'll email you a link; no password needed.