Design Twitter
Problem Statement
Design a highly scalable microblogging platform like Twitter, where users can post short messages (tweets), follow other users, and view a feed of recent tweets from people they follow.
The system should support millions of users, real-time feeds, high write/read throughput, and handle features such as likes, retweets, hashtags, and search efficiently.
🎯 Core Requirements
1. User Operations
2. Tweet Operations
3. Feed Generation
4. Trending and Search
5. Notifications & Engagement
6. Scalability & Performance
7. Non-Functional Requirements
⚙️ Key Functional Components
ComponentResponsibility
User Service
Manage users, profiles, and follow relationships.
Tweet Service
Create, delete, and store tweets.
Feed Service
Generate personalized user timelines.
Notification Service
Send alerts for mentions, likes, and follows.
Search Service
Index and retrieve tweets and hashtags.
Media Service
Handle storage and retrieval of images/videos.
Analytics/Trends Service
Track trending hashtags and tweet statistics.
Auth Service
Manage login, tokens, and permissions.
🧱 Data Model (Simplified)
User
1
2User {
3 user_id: UUID
4 username: string
5 name: string
6 bio: string
7 followers: list<user_id>
8 following: list<user_id>
9 created_at: timestamp
10}Tweet
1
2Tweet {
3 tweet_id: UUID
4 user_id: UUID
5 content: string
6 media_url: optional<string>
7 created_at: timestamp
8 like_count: int
9 retweet_count: int
10 hashtags: list<string>
11}Feed
1
2Feed {
3 user_id: UUID
4 tweets: list<tweet_id>
5 last_updated: timestamp
6}🧩 High-Level Architecture
1
2 +-------------------+
3 | API Gateway |
4 +-------------------+
5 |
6 +-----------+-----------+
7 | |
8+---------------+ +---------------+
9| Auth Service | | User Service |
10+---------------+ +---------------+
11 | |
12+---------------+ +----------------+
13| Tweet Service |<----->| Feed Service |
14+---------------+ +----------------+
15 | |
16+---------------+ +----------------+
17| Media Service | | Notification |
18+---------------+ | Service |
19 | +----------------+
20 |
21+-----------------------------------+
22| Storage Layer |
23| (SQL/NoSQL, Blob Store, Cache) |
24+-----------------------------------+
25 |
26+----------------+
27| Search & Trends|
28+----------------+🧠 System Design Considerations
1. Data Storage
2. Feed Generation Approaches
ApproachDescriptionProsCons
Fan-out on write
Push tweets to all followers’ feeds at post time.
Fast reads.
Expensive writes (for users with many followers).
Fan-out on read
Generate feed dynamically on user read.
Efficient writes.
Slower reads, higher latency.
Hybrid
Precompute feeds for active users, lazy for others.
Balanced approach.
Slight complexity.
3. Scaling & Performance
4. Search and Trends
5. Fault Tolerance
📈 Example Workflows
Post Tweet
Get User Feed
🚀 Scalability Features
✅ Goal:
Design a scalable, distributed, and performant microblogging platform like Twitter that supports posting, following, feed generation, and real-time updates at global scale.