Great! Let’s walk through a system design interview example to see how you can approach it step-by-step.
Example: Design Twitter (or TinyTwitter)
1. Clarify Requirements
First, ask clarifying questions to understand the scope:
- Functional Requirements:
- Users can post tweets (text + images).
- Users can follow others.
- Users can see a feed of tweets from people they follow.
- Users can like, retweet, and comment.
- Users can search for tweets.
- Non-Functional Requirements:
- High availability (99.99% uptime).
- Low latency (feed loads in < 200ms).
- Scalable to millions of users.
- Durable (no data loss).
2. Estimate Scale (Back-of-the-Envelope)
Assume:
- Users: 100M daily active users (DAU).
- Tweets per second (QPS): 100M tweets/day → ~1,157 tweets/sec.
- Followers per user: Average 200 followers.
- Feed size: 100 tweets per user (paginated).
3. High-Level Design (Components)
Draw a diagram with these components:
-
Client (Mobile/Web App)
- Posts tweets, fetches feeds, interacts with APIs.
-
API Gateway
- Routes requests to different services (e.g.,
/tweet, /user).
-
Load Balancer
- Distributes traffic across servers.
-
Tweet Service
- Handles tweet creation, storage, and retrieval.
- Uses a NoSQL database (e.g., Cassandra, DynamoDB) for scalability.
-
User Service
- Manages user profiles, authentication (OAuth), and follow relationships.
- Uses a relational DB (e.g., PostgreSQL) for complex queries.
-
Feed Service
- Generates a user’s feed (tweets from followed users).
- Uses caching (Redis) to store recent feeds.
- Option 1: Pull model (fetch tweets when requested).
- Option 2: Push model (update feed in real-time via a message queue).
-
Notification Service
- Sends push notifications for likes, retweets, etc.
- Uses WebSockets or Firebase Cloud Messaging (FCM).
-
Search Service
- Enables full-text search on tweets.
- Uses Elasticsearch or Solr.
-
CDN (Content Delivery Network)
- Serves static assets (images, videos) globally.
-
Message Queue (Kafka/RabbitMQ)
- Handles async tasks (e.g., sending notifications, updating feeds).
-
Database
- Tweets: NoSQL (Cassandra/DynamoDB) for high write throughput.
- Users & Follows: SQL (PostgreSQL) for complex joins.
- Feed Cache: Redis for low-latency reads.
4. Deep Dive (Key Challenges & Solutions)
A. Storing Tweets
- Problem: High write throughput (1,157 tweets/sec).
- Solution:
- Use a partitioned NoSQL DB (e.g., Cassandra) for tweets.
- Shard by
tweet_id or user_id to distribute load.
B. Generating the Feed
- Problem: Fetching tweets from all followed users is slow.
- Solutions:
- Pull Model (Lazy Loading):
- When a user requests their feed, fetch tweets from followed users.
- Pros: Simple, no extra storage.
- Cons: High latency if many followers.
- Push Model (Real-Time Updates):
- Use a message queue (Kafka) to push new tweets to followers’ feeds.
- Store feeds in Redis for fast retrieval.
- Pros: Low latency, real-time updates.
- Cons: More complex, requires background jobs.
C. Handling Follow Relationships
- Problem: Storing and querying "who follows whom" efficiently.
- Solution:
- Use a graph database (Neo4j) or PostgreSQL with adjacency list.
- Cache frequent queries in Redis.
D. Scaling Reads (Feed Service)
- Problem: Millions of users requesting feeds simultaneously.
- Solution:
- Caching: Store feeds in Redis (TTL-based invalidation).
- Read Replicas: Use database replicas for read-heavy operations.
E. Handling Hot Users (Celebrities)
- Problem: A tweet from a celebrity (e.g., Elon Musk) gets millions of likes/retweets.
- Solution:
- Sharding: Distribute likes/retweets across multiple DB partitions.
- Rate Limiting: Throttle API calls to prevent abuse.
5. Trade-offs & Alternatives
- SQL vs NoSQL:
- SQL (PostgreSQL) for complex queries (e.g., user profiles).
- NoSQL (Cassandra) for high write throughput (e.g., tweets).
- Pull vs Push Model:
- Push is better for real-time but adds complexity.
- Monolith vs Microservices:
- Start with a monolith, then split into microservices as needed.
6. Final Diagram (Simplified)
Client → API Gateway → Load Balancer → [Tweet Service, User Service, Feed Service]
↓
[Cassandra, PostgreSQL, Redis]
↓
[CDN, Kafka, Elasticsearch]
Key Takeaways for Interviews
✅ Start with requirements (functional & non-functional).
✅ Estimate scale (users, QPS, storage).
✅ Break into components (APIs, databases, caching, etc.).
✅ Discuss trade-offs (e.g., consistency vs availability).
✅ Mention optimizations (sharding, caching, CDN).
Would you like to try another system (e.g., Uber, TinyURL)? 🚀