System Design Problems
Design a News Feed System
A news feed system displays a personalized stream of posts from users a person follows. The core challenge is fan-out: a single post by a popular user must be delivered to millions of followers in near real-time.
- Fan-out on Write β Pre-compute feeds when a post is published (push model)
- Fan-out on Read β Compute feeds dynamically when a user loads their feed (pull model)
- Hybrid Approach β Combine both strategies based on follower count
The fundamental tension: pre-computing feeds is fast to read but expensive to write; computing on-the-fly is cheap to write but slow to read.
Requirements
Functional Requirements
- Users can create posts (text, images, videos)
- Users see a personalized feed of posts from people they follow
- Feed is sorted by time (or relevance/ranking)
- Users can like, comment, and share posts
- New posts appear in followers' feeds within seconds
- Support for trending topics and discovery
Non-Functional Requirements
- Latency: Feed loads in < 200ms
- Throughput: 500K feed reads/sec, 50K posts/sec
- Freshness: New posts visible within 5 seconds
- Consistency: Eventual consistency is acceptable
- Scalability: 500M users, 100M daily active users
Back-of-the-Envelope Estimation
Feed Generation Strategies
Hybrid Strategy
The hybrid approach handles the celebrity problem:
Detailed Design
Data Models
// Post
{
post_id: "p_123",
user_id: "u_456",
content: "Hello world!",
media_urls: ["https://..."],
created_at: "2026-06-20T10:00:00Z",
like_count: 42,
comment_count: 5
}
// Feed (per user, stored in Redis sorted set)
// Key: feed:{user_id}
// Score: post timestamp
// Value: post_id
Feed Storage
Use Redis sorted sets for feed storage:
ZADD feed:u_789 1687267200 p_123
ZADD feed:u_789 1687267100 p_122
ZREVRANGE feed:u_789 0 19 // Get 20 most recent posts
Ranking Algorithm
Feed items can be ranked by time or by relevance:
Real-time Updates
For real-time feed updates, use WebSockets or Server-Sent Events (SSE):
- User connects via WebSocket
- When a followed user posts, push the new post via WebSocket
- Client inserts the post at the top of the feed
- For batch updates, poll every 30 seconds
Scaling Considerations
Database Sharding
Partition the posts table by user_id hash:
shard = hash(user_id) % NUM_SHARDS
This co-locates all posts by the same user on the same shard, enabling efficient queries for "get all posts by user X."
Feed Cache Sizing
Practice Exercises
-
Design: How would you implement a "For You" personalized feed that uses machine learning to rank posts based on user preferences? What features would you use?
-
Scale: If a celebrity with 50M followers publishes a post, estimate the time and resources needed to fan out the post to all followers using the push model.
-
Consistency: How would you handle the case where a user unfollows someone but still sees their posts in the feed? Design a feed invalidation strategy.
-
Trade-offs: Compare push, pull, and hybrid feed generation for a system with 100M users where 0.1% are celebrities (10M+ followers).
What to Learn Next
-> Design Chat System Real-time messaging with WebSocket and presence tracking.
-> Event-Driven Architecture Event sourcing, CQRS, and asynchronous communication.
-> Caching Strategies Cache-aside, write-through, and distributed caching patterns.
-> Message Queues Kafka, RabbitMQ, and event-driven fan-out.
-> Design Recommendation System ML-based content ranking and personalization.
-> Data Replication Replication strategies for high-availability data access.
Extended Learning: Beyond the Basics
Key Concepts Summary
This lesson covers fundamental concepts that serve as building blocks for more advanced topics. Master these basics before moving to intermediate and advanced material.
Prerequisite Knowledge
Before studying this topic in depth, ensure you understand:
- Basic definitions and terminology
- Core formulas and their derivations
- Common applications in real-world scenarios
- Typical pitfalls and misconceptions
Step-by-Step Walkthrough
Let us work through a complete example to solidify understanding:
Step 1: Identify the problem type. Determine which category this problem falls into and which approach is most appropriate.
Step 2: Apply the relevant formula. Substitute known values into the appropriate equation, paying attention to units and constraints.
Step 3: Simplify and solve. Work through the algebra step by step, checking each intermediate result.
Step 4: Verify the answer. Check that the solution satisfies the original conditions and makes logical sense.
Common Mistakes to Avoid
- Misidentifying the problem type β Always start by categorizing the problem
- Incorrect formula application β Verify the formula applies to your specific case
- Unit errors β Always check units are consistent throughout
- Sign errors β Pay careful attention to positive and negative values
- Rounding too early β Keep full precision until the final answer
- Forgetting constraints β Check that solutions satisfy all given conditions
Real-World Applications
Understanding this concept is essential for:
- Advanced problem solving in examinations
- Building intuition for more complex topics
- Developing analytical thinking skills
- Applying mathematical reasoning to practical situations
Practice Exercises
- Basic: Solve 5 problems of increasing difficulty using the core concept.
- Intermediate: Apply the concept to word problems with multiple steps.
- Advanced: Combine this concept with 2-3 other topics in a comprehensive problem.
- Challenge: Create your own problem that requires this concept plus additional knowledge.
Interview and Exam Tips
- Common question patterns: Identify which exam or interview formats test this topic
- Time management: Practice completing problems within time constraints
- Common traps: Know the typical mistakes examiners set as distractors
- Verification strategy: Develop a quick method to check your answer
Related Topics
- Previous Topic β Build on the foundation laid here
- Next Topic β See how this concept extends to more advanced material
- Practice Problems β Additional exercises with solutions
- Video Explanation β Visual walkthrough of key concepts
Quick Reference Card
| Concept | Formula/Rule | Key Insight | Common Use |
|---|---|---|---|
| Core Definition | See main text | Understand the intuition | Foundation for all applications |
| Key Formula | See main text | Memorize the derivation | Direct problem solving |
| Special Case | See main text | Recognize when it applies | Simplifies complex problems |
| Edge Case | See main text | Know the limitations | Avoids errors |
Further Reading
For students who want to go deeper:
- Standard textbooks on the subject
- Research papers applying this concept
- Online courses with video explanations
- Practice problem collections with detailed solutions