News Feed System
Overview
The News Feed system design (like Facebook or Twitter) is a classic interview question that tests your understanding of fan-out, feed ranking, and real-time updates. The core challenge is serving a personalized, ranked feed to millions of users efficiently. Do you push (fan-out on write) or pull (fan-out on read)? How do you rank posts? How do you handle new followers and real-time updates? This design matters in interviews because it's a well-known product and the trade-offs are nuanced—push can lead to write amplification for celebrities, pull can cause read storms. Demonstrating you understand both approaches and when to use hybrid or pre-combined feeds shows systems maturity.
Requirements
Functional
- Display posts from followed users in reverse chronological or ranked order
- Support post creation, like, comment, share
- Real-time updates when new posts arrive
- Infinite scroll (paginated feed)
- Mute/hide posts or users
Non-Functional
- Low latency — feed load <200ms
- High throughput — millions of users, thousands of posts/sec
- Consistency — new posts appear within seconds
Capacity Estimation
Assume 300M users, 500 avg followees. 10K posts/sec. Push: 500 * 10K = 5M writes/sec to feed tables. Pull: 300M reads/sec on feed load.
Architecture Diagram
Component Deep Dive
Post Service
Creates posts, stores in DB. Publishes event to message queue for fan-out.
Fan-out Service
On new post: writes to feed tables of all followers (push) or does nothing (pull). Hybrid: push for active users, pull for inactive.
Feed Service
Retrieves feed for user. Reads from pre-computed feed (push) or aggregates on read (pull). Applies ranking.
Ranking Service
Scores posts by relevance, recency, engagement. ML model or heuristic. Runs offline or at read time.
Cache
Redis. Caches user's feed (first page). Invalidate on new post (push).
Message Queue
Kafka. Decouples post creation from fan-out. Enables async processing.
Database Design
Posts table: post_id, user_id, content, created_at. Feed table (push): user_id, post_id, created_at, score. Social graph: follower → followee. Cassandra/DynamoDB for feed; MySQL/Postgres for posts.
API Design
| Method | Path | Description |
|---|---|---|
POST | /api/posts | Create post. Body: {content, media?}. Returns post_id. |
GET | /api/feed?cursor=&limit= | Get user's feed. Paginated. |
POST | /api/posts/{id}/like | Like a post. |
GET | /api/posts/{id} | Get single post with comments. |
Scalability & Trade-offs
- Push vs pull: Push = fast reads, slow writes (celebrity problem). Pull = slow reads, simple writes. Hybrid balances both.
- Ranking: Chronological is simple; ML ranking improves engagement but adds latency and complexity.
- Consistency: Eventual consistency is acceptable for feed; strong consistency for likes/comments.
Related System Designs
Social Media (Instagram)
Designing a Social Media platform (Instagram) combines photo/video storage, news feed, real-time features, and discovery...
InfrastructureURL Shortener (TinyURL)
The URL Shortener (TinyURL-style) system design is a classic interview question that tests your understanding of distrib...
InfrastructureRate Limiter
A Rate Limiter system design question tests your understanding of distributed systems, consistency, and real-time decisi...