Design a Feed App (Like Twitter)
Problem Statement
Design a feed-based social media platform similar to Twitter (X), where users can:
The system should support millions of active users, low-latency feed generation, and scalable content storage and retrieval.
🎯 Core Requirements
Functional Requirements
User Management
Post Management
Feed Generation
Real-Time Updates
Trending Topics (Optional)
Search
Notifications (Optional)
Non-Functional Requirements
Attribute with Target
Availability
99.9%
Latency
Feed generation < 200ms
Scalability
100M+ users, millions of posts/day
Consistency
Eventual consistency acceptable for feed updates
Durability
No loss of posts or user relationships
Security
Authentication & authorization for all actions
🧩 Core Components
Component with Description
User Service
Manages user accounts, profiles, and relationships (follow/unfollow).
Post Service
Handles creation, storage, and retrieval of posts (tweets).
Feed Service
Generates and serves personalized feeds for users.
Fan-out Service
Pushes new posts to followers’ feeds (fan-out on write).
Timeline Store
Stores precomputed or dynamically generated user feeds.
Search Service
Enables searching posts, hashtags, and users.
Notification Service
Sends alerts for interactions (likes, comments).
Media Service
(Optional) Handles attachments like images or videos.
Cache Layer (Redis)
Caches hot feeds and posts for low-latency retrieval.
Analytics / Trending Service
Tracks engagement and trending topics.
🧱 High-Level Architecture
1
2 +-------------------------+
3 | API Gateway |
4 +-----------+--------------+
5 |
6 +----------------------------------------+
7 | Application Layer |
8 +----------------------------------------+
9 | User Svc | Post Svc | Feed Svc | Auth Svc|
10 +----------------------------------------+
11 |
12 +---------------+
13 | Message Queue |
14 | (Kafka / SQS) |
15 +-------+-------+
16 |
17 +----------------+----------------+
18 | |
19 +-------v--------+ +-------v--------+
20 | Fan-out Svc | | Feed Generator |
21 | (Push tweets | | (Pull timeline) |
22 | to followers) | | dynamically) |
23 +----------------+ +----------------+
24 |
25 +--------------------------------------+
26 | Data & Storage Layer |
27 +--------------------------------------+
28 | Posts DB | User Graph DB |
29 | Feed Cache | Search Index (ES) |
30 | Blob Store | Analytics Store |
31 +--------------------------------------+
32 |
33 +--------------------------------------+
34 | Client Layer (UI/Apps) |
35 +--------------------------------------+🧱 Data Model (Simplified)
User
1
2{
3 "user_id": "u123",
4 "username": "sahil",
5 "name": "Sahil Batra",
6 "followers": ["u321", "u456"],
7 "following": ["u789"],
8 "created_at": "timestamp"
9}Post
1
2{
3 "post_id": "p567",
4 "author_id": "u123",
5 "content": "Excited to start learning system design!",
6 "timestamp": "timestamp",
7 "likes": 100,
8 "comments": 15,
9 "reposts": 10,
10 "hashtags": ["#systemdesign", "#learning"]
11}FeedEntry
1
2{
3 "user_id": "u321",
4 "post_id": "p567",
5 "timestamp": "timestamp"
6}⚙️ Feed Generation Models
🅰️ Fan-out on Write (Push Model)
🅱️ Fan-out on Read (Pull Model)
🧠 Hybrid Approach
⚡ Key Workflows
1. Posting a Tweet
2. Reading the Feed
3. Following a User
🧱 Storage Design
| Data | Storage | Rationale |
| ------------------ | ------------------------------------------ | ---------------------------- |
| User Profiles | Relational DB (PostgreSQL) | Strong consistency |
| User Relationships | Graph DB (Neo4j / DynamoDB adjacency list) | Efficient follower lookups |
| Posts | NoSQL (Cassandra / DynamoDB) | High write throughput |
| Feeds | Redis / Cassandra | Fast reads |
| Media | Blob Storage (S3 / GCS) | Durable large object storage |
| Search | Elasticsearch | Text and hashtag search |
| Analytics | ClickHouse / BigQuery | Real-time metrics and trends |
📡 APIs
| Method | Endpoint | Description |
| ------ | -------------------- | ----------------------- |
| `POST` | `/users/{id}/follow` | Follow another user |
| `POST` | `/posts` | Create a new post |
| `GET` | `/users/{id}/feed` | Retrieve user’s feed |
| `GET` | `/posts/{id}` | Get post details |
| `POST` | `/posts/{id}/like` | Like a post |
| `GET` | `/trending` | Fetch trending hashtags |
⚙️ Scaling & Optimization
Caching:
Sharding:
Async Processing:
Rate Limiting:
Indexing:
Eventual Consistency:
Follower Graph Optimization:
🚨 Challenges & Trade-offs
| Challenge | Solution |
| ------------------------------------------ | ---------------------------------------- |
| Celebrity users with millions of followers | Use **fan-out on read** for such users |
| Large feed size | Pagination + lazy loading |
| Feed freshness vs cost | Hybrid push/pull model |
| Duplicate delivery | Use unique message IDs in queues |
| Consistency | Eventual consistency using async updates |
✅ Goal:
Design a scalable, low-latency social feed system that supports millions of users, combines push and pull-based feed delivery, ensures real-time updates, and provides extensibility for future features like trending topics, rich media, and recommendations.