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.