DEV Community

Cover image for Design a News Feed System: Fan-out on Write vs Fan-out on Read
ScaleDojo
ScaleDojo

Posted on Edited on

Design a News Feed System: Fan-out on Write vs Fan-out on Read

The news feed is a classic system design question. It looks simple (show posts from people I follow), but the scale makes it interesting: millions of users, constant posting, and feeds that must load in milliseconds.

Here is a practical way to design it.

Requirements

Functional:

  • Users can publish posts

  • Users can follow other users

  • Users see a feed of posts from people they follow

  • Feed is ordered by time or relevance

Non-functional:

  • Feed loads fast (low latency)

  • Highly available

  • Eventual consistency is acceptable (a post can appear a few seconds late)

The core problem

When a user opens the app, you need their feed instantly. There are two basic ways to build it.

Approach 1: Fan-out on read (pull)

Build the feed at the moment the user opens it.

  1. Look up everyone the user follows.

  2. Fetch recent posts from each of them.

  3. Merge and sort the results.

Pros:

  • Writes are cheap, since a post is stored once.

  • No wasted work for inactive users.

Cons:

  • Reads are slow and expensive, especially if the user follows thousands of accounts.

Approach 2: Fan-out on write (push)

Build the feed when a post is created.

  1. A user publishes a post.

  2. The system finds all their followers.

  3. The post ID is pushed into each follower's precomputed feed.

Pros:

  • Reading the feed is a single fast lookup.

Cons:

  • Writes are heavy. One post can trigger thousands or millions of writes.

  • Wasted work for followers who never open the app.

The celebrity problem

A user with 50 million followers makes fan-out on write a disaster, because one post triggers 50 million writes.

The standard answer is a hybrid approach:

  • Normal users: fan-out on write, so feeds are precomputed.

  • Celebrities: do not fan out. When a follower opens their feed, merge the precomputed feed with recent posts from the celebrities they follow, fetched at read time.

Mentioning this in an interview shows real depth.

High-level architecture

  1. Post service: stores new posts.

  2. Fan-out service: consumes new-post events from a queue and writes to follower feeds.

  3. Feed store: a cache or key-value store holding a list of post IDs per user (Redis works well).

  4. Feed service: reads the list of IDs, then fetches post details.

  5. Social graph service: stores who follows whom.

Feed entry sketch:

# feed:{user_id} -> sorted set of post_ids scored by timestamp
redis.zadd(f"feed:{follower_id}", {post_id: timestamp})

# read the latest 20 entries
post_ids = redis.zrevrange(f"feed:{user_id}", 0, 19)
Enter fullscreen mode Exit fullscreen mode

Store only post IDs in the feed, not full posts. It saves memory and keeps edits and deletions consistent.

Ranking

A chronological feed is the simplest. For a relevance-ranked feed, score each candidate post using signals like:

  • Closeness to the author (how often you interact)

  • Post engagement (likes, comments)

  • Recency

  • Content type

Ranking can run as a separate step over a few hundred candidate posts before returning the top results.

Pagination

Avoid offset-based pagination, because new posts shift the list. Use cursor-based pagination: the client sends the ID or timestamp of the last item it saw, and the server returns items older than that.

Scaling

  • Use an async queue between the post service and fan-out workers, so posting stays fast.

  • Shard the feed store by user_id.

  • Cache hot posts and media metadata.

  • Serve images and videos through a CDN.

  • Cap feed length (for example, the latest 800 posts per user).

Trade-offs to state
Fan-out on write

  • Fast reads, heavy writes.

Fan-out on read

  • Cheap writes, slow reads.

Hybrid

  • Best of both, but more complex to build and operate.

Eventual consistency

  • A small delay is acceptable and gives much better scale.

Common mistakes

  1. Choosing one fan-out strategy without mentioning the other.

  2. Ignoring the celebrity problem.

  3. Storing full post content in every feed.

  4. Using offset pagination.

  5. Skipping the queue between posting and fan-out.

To get comfortable with this, walk through it end to end in a hands-on system design interview prep environment and practice explaining each trade-off as you go.

Top comments (1)

Collapse
 
sophiabennettdev profile image
Sophia Bennett •

Worth reading!