This is my reading notes for Chapter 11 in book “System Design Interview – An insider’s guide (Vol. 1)”.
1. Problem Understanding
A news feed system is a personalized content feed that dynamically updates for users as their friends and connections post new content (e.g., Facebook feed, Instagram feed, or Twitter timeline). The primary challenge is building a system that can efficiently handle a large number of posts and a massive user base.
- Key Features:
- Display content (posts, images, videos) from users’ friends or followers.
- Posts are typically sorted in reverse chronological order or based on a weighted ranking algorithm.
- Handle large traffic, for example, 10 million Daily Active Users (DAUs).
- Support large numbers of friends/followers per user, for instance, 5000 friends per user.
Clarifying Questions:
- Is the system for both mobile and web apps?
- What types of content can users post (text, images, videos)?
- How is the feed ordered (reverse chronological or ranking algorithm)?
- What is the traffic scale (e.g., how many posts per day)?
- What is the maximum number of friends a user can have?
Example: Suppose we are designing Facebook’s news feed. Facebook has over 2 billion users, with some users having up to 5000 friends. Each of these users can post media like images and videos that need to be served to their friends’ feeds in real time.
2. High-Level Design
The news feed system consists of two major flows:
- Feed Publishing: This occurs when a user creates a new post. The post data is written into the database and cache, and the post is propagated to the feeds of the user’s friends or followers.
- News Feed Building: This occurs when a user opens the app and requests to view their news feed. The system retrieves posts from friends, sorts them (e.g., by reverse chronological order), and displays the results to the user.
High-Level Architecture Components:
- Clients (Mobile/Web apps): Users interact with the system.
- API Gateway: Routes requests from clients to the appropriate services.
- Post Service: Handles the creation and storage of posts.
- News Feed Service: Handles news feed construction by fetching and aggregating posts from friends.
- Notification Service: Sends notifications to users when a friend posts new content.
Example:
- When a user posts a photo on Facebook, the system must store the post in the database and update the feeds of all their friends so that the next time those friends check their feed, they see the new post.
3. API Design
Two key APIs drive the news feed system:
- Feed Publishing API:
- Endpoint:
POST /v1/me/feed
- Parameters:
content
: The post content (text, images, videos, etc.)auth_token
: User authentication token
- Function: This API allows a user to publish a post, which gets stored in the database and cache.
- Endpoint:
- News Feed Retrieval API:
- Endpoint:
GET /v1/me/feed
- Parameters:
auth_token
: User authentication token
- Function: This API retrieves a user’s news feed by fetching posts from friends and ordering them appropriately.
- Endpoint:
Example:
- For the retrieval API, the response could look like this:
{
"userId": "1234",
"posts": [
{
"postId": "5678",
"content": "Hello, world!",
"createdAt": "2023-01-01T12:00:00Z"
},
{
"postId": "5679",
"content": "This is another post",
"createdAt": "2023-01-02T14:00:00Z"
}
]
}
4. Detailed Design
To make the system scalable and efficient, we break down the process into several key components:
- Feed Publishing Flow:
- When a user publishes a post, the post data is first written to a database and also cached for quick access.
- The post is then pushed to the news feeds of the user’s friends via the fanout service.
- News Feed Retrieval Flow:
- When a user requests their news feed, the system pulls the relevant post IDs from the cache or database, aggregates them, sorts them (typically by time or ranking), and sends the fully populated feed back to the user.
Fanout Service:
The fanout service handles the distribution of posts to friends’ news feeds. Two models exist:
- Fanout on Write (Push Model): The feed is precomputed when a user posts, and the post is pushed to all their friends’ feeds immediately.
- Fanout on Read (Pull Model): The feed is constructed when a user requests their news feed by pulling posts from friends.
Example: A user publishes a post, and the fanout service pushes the post to the feeds of their 2000 friends. When any of these friends open their app, the post is already in their feed and loads instantly.
5. Caching Strategy
Caching is essential for performance. The system uses multiple caches to minimize latency and improve efficiency:
- News Feed Cache: Stores post IDs for a user’s news feed.
- Content Cache: Stores the actual post data (e.g., text, images).
- Social Graph Cache: Stores the relationship data (e.g., friends or followers).
- Action Cache: Stores likes, comments, and shares.
- Counter Cache: Stores counts of likes, comments, and other interactions.
Example:
When User A posts, their friends’ news feed caches are updated with the new post ID. When User B requests their feed, the cache is hit, and post data is retrieved from the content cache.
6. Scaling and Optimization
Handling millions of users requires efficient scaling strategies:
- Database Sharding: Splitting the database by user ID or post ID allows horizontal scaling.
- Stateless Web Tier: The system should keep the web tier stateless to scale easily. All state is stored in shared services like databases or caches.
- Message Queues: Used to decouple components and handle spikes in activity. For example, fanout tasks are queued for processing to avoid overwhelming the system.
- Multiple Data Centers: Distribute the load across regions, ensuring low latency and high availability.
- Monitoring and Metrics: Track key performance indicators like latency, error rates, and query volumes.
Key Takeaways
- Trade-offs in Fanout Strategies: Fanout on write offers faster retrieval but can be computationally expensive for users with many friends. Fanout on read is less resource-intensive but slower to deliver content.
- Caching Is Critical: A multi-layered caching strategy can drastically reduce latency by avoiding database calls for frequently accessed data.
- Sharding and Database Scaling: Horizontal scaling and sharding help distribute the load across multiple servers, preventing bottlenecks in any single database.
- Message Queues for Decoupling: Decoupling write and read processes via message queues allows for better handling of peak loads, especially when processing tasks like distributing posts to thousands of friends.
- User Experience: Optimizing for low-latency feed retrieval ensures a smooth and responsive user experience, which is crucial for user retention in social platforms.
These are the detailed notes for designing a news feed system, incorporating key components, considerations, examples, and trade-offs necessary to handle real-world scale and performance demands.