Hello! Welcome back to our deep dive into feed systems.
In the last lesson, we designed the high-level architecture for a social media feed. We established a microservices-based system, defined our APIs and data models, and most importantly, identified the central challenge: how to generate a user's feed efficiently. We concluded by introducing two competing strategies, fan-out-on-read and fan-out-on-write, and hinted that a hybrid approach is often the best solution.
Today, we're going to put those strategies under the microscope. Our learning outcome is to deep dive into feed generation strategies: fan-out-on-write vs fan-out-on-read. This is a classic trade-off discussion in system design interviews. Being able to dissect the mechanics, pros, and cons of each, and articulate why a hybrid model is superior, is a strong signal of a senior engineer's architectural thinking.
Let's start with the simplest, most intuitive approach.
1. Fan-out-on-Read (The "Pull" Model)
The "pull" model, or fan-out-on-read, generates the feed at the moment the user requests it. The client "pulls" the content on demand.
How It Works
Imagine a user opens their app. Here's the sequence of events:
- The client sends a
GET /v1/feedrequest to the Feed Service. - The Feed Service asks the User Service, "Who does this user follow?"
- The User Service returns a list of followed user IDs.
- For each followed user ID, the Feed Service asks the Post Service, "What are the most recent posts from this user?"
- The Feed Service receives multiple lists of posts.
- It merges all these lists, sorts them by timestamp, and returns the top N (e.g., 20) posts to the client.
This entire process happens in real-time while the user is looking at a loading spinner.

Analysis: The Pros and Cons
This approach is simple to conceptualize, but its performance characteristics are critical to understand.
Designing a News Feed System: Facebook and Twitter ...
The article 'Designing a News Feed System' provides a concise breakdown of the pull model's advantages and disadvantages. This will help you structure your thoughts on the trade-offs.
Read the section titled 'Pull Model (On-demand Timelines)', focusing on the 'Advantages' and 'Disadvantages' lists.
Let's summarize the trade-offs:
-
Pros:
- Simple Write Path: When a user creates a post, it's a single write to the Post database. The system doesn't need to do any extra work at write time.
- No Wasted Resources: Feeds for inactive users are never generated, saving computation and storage.
- Data is "Fresh": The feed is always built from the most current data.
-
Cons:
- High Read Latency: This is the deal-breaker. If a user follows 1,000 people, the system might have to perform 1,000 queries and sort thousands of posts, all while the user is waiting. This will not meet our non-functional requirement for a low-latency (<200ms) feed. This is often called a "thundering herd" or "fan-out" problem at read time.
In an interview, you should present this as a starting point, but quickly identify that the high read latency makes it unviable for a large-scale, read-heavy system. This naturally leads to the next logical step: pre-computation.
2. Fan-out-on-Write (The "Push" Model)
To solve the read latency problem, we can do the heavy lifting ahead of time. The "push" model, or fan-out-on-write, pre-computes a user's feed whenever a new, relevant post is created. The new post is "pushed" to the feeds of all followers.
How It Works
This flow is asynchronous and happens in the background.
- A user creates a post. The Post Service writes it to the Post DB.
- The Post Service then publishes a message (e.g.,
{ "userId": "userA", "postId": "post123" }) to a Message Queue (like Kafka or RabbitMQ). - A dedicated Fanout Service (a set of workers) consumes messages from this queue.
- For each message, the Fanout Service gets the list of all followers for
userAfrom the User Service. - For each follower, the service adds
post123to that follower's pre-computed feed. This feed is typically stored in a fast, in-memory Cache (like Redis), often as a list of post IDs. For example, it might executeLPUSH followerZ_feed post123.
Now, when a user requests their feed, the system simply reads this pre-computed list from the cache, which is extremely fast.
System Design Interview Walkthrough: Design Twitter
The 'System Design Interview Walkthrough: Design Twitter' video by Hello Interview explains this fan-out-on-write process very clearly.
Watch from 14:45 to 16:07. Pay close attention to the role of the message queue and the concept of updating a 'timeline cache' for each follower.
Analysis: A New Set of Trade-offs
This approach solves our read latency problem, but introduces complexity on the write side.
-
Pros:
- Low Read Latency: Feed retrieval is incredibly fast, as it's just a simple cache read. This meets our core performance requirement.
-
Cons:
- High Write Amplification: A single post from a user with a million followers triggers one million cache writes. This is a massive "fan-out" on the write path.
- The "Celebrity" Problem: This high write amplification can overwhelm the fanout service and the cache when a celebrity or mega-influencer posts. This is a classic failure mode.
- Wasted Resources (again): If a user is inactive, we are still computing, pushing, and storing their feed, which they may never read. This can be a significant cost at scale.
We've solved one problem but created another. A purely push-based model is also not robust enough. This is where you, as a senior candidate, can shine by proposing a solution that combines the best of both worlds.
3. The Hybrid Approach: The Practical Solution
The most effective systems use a hybrid model that intelligently blends the push and pull strategies. The core idea is to treat "normal" users and "celebrity" users differently.
How It Works
-
Define a "Celebrity": First, we establish a threshold. For example, any user with more than 10,000 followers is considered a celebrity. This is a configurable parameter.
-
Apply Fan-out-on-Write for Normal Users: If a user with <10k followers posts, we use the fan-out-on-write mechanism. The post is pushed to the feed caches of all their followers.
-
Apply Fan-out-on-Read for Celebrities: If a celebrity posts, we do nothing on the write path. We do not fan out the post to millions of followers.
-
Merge at Read Time: When any user requests their feed, the system performs a two-step process:
- Step A (Push): It fetches the user's pre-computed feed from their Redis cache (which contains posts from all the non-celebrities they follow).
- Step B (Pull): It identifies which celebrities the user follows (this list is usually small). It then makes a direct request to the Post Service to get the most recent posts from just those celebrities.
- Step C (Merge): It merges the two lists (from Step A and B) in the application layer, sorts by time, and returns the result.
Designing INSTAGRAM: System Design of News Feed
Both of the videos we've looked at conclude with this hybrid approach. Let's hear Gaurav Sen's take on it in the context of Instagram.
Watch the segment from 21:24 to 23:31. The video discusses the problem with celebrity posts and introduces the idea of a hybrid model combining push for normal users and pull for celebrities.
This hybrid model is a powerful solution because it optimizes for the common case (posts by non-celebrities) while having a safe, bounded fallback for the extreme case (celebrity posts). It ensures that reads are fast for everyone, and the write path is never overwhelmed.
Conclusion
In this deep dive, we dissected the core feed generation strategies. Understanding this trade-off is fundamental to designing any large-scale social or content-based system.
Key Takeaways:
- Fan-out-on-Read (Pull): Simple write path but suffers from high read latency. Not suitable for read-heavy systems requiring fast performance.
- Fan-out-on-Write (Push): Provides excellent read latency but introduces high write amplification and struggles with the "celebrity problem".
- Hybrid Model: The optimal solution. It uses fan-out-on-write for the majority of users and a selective fan-out-on-read for celebrities, balancing the load and ensuring a good user experience for everyone.
- Communicating in an Interview: Start with the naive pull model, explain its flaws, introduce the push model as a solution, identify the new flaws (celebrity problem), and finally present the hybrid model as the robust, scalable architecture. This demonstrates a structured and sophisticated thought process.
Next Lesson Preview:
Our fan-out-on-write strategy relies on asynchronous processing and caching, which means a follower's feed is not updated instantaneously. This introduces the concept of eventual consistency. In the next lesson, we will discuss the trade-offs of consistency, availability, and performance in feed system design, connecting our architectural choices to fundamental distributed systems principles like the CAP theorem.