Chapter 10System Design~2 min read
Case Study: Twitter Design करा
Real System Design Interview Practice
"Design Twitter/X" हा system design interview मधला सर्वात popular question आहे. Step by step बघू — requirements, high-level design, database schema, scalability challenges.
Step 1: Requirements Clarify करा
Functional & Non-functional Requirements
text
Functional Requirements:
✓ Users tweets post करू शकतात (text, images, videos)
✓ Users एकमेकांना follow करू शकतात
✓ Home feed दिसतो (followed users चे tweets)
✓ Tweets like, retweet, reply करता येतात
✓ Search (tweets, users, hashtags)
✓ Notifications
Non-functional Requirements:
✓ DAU: 200 million (Daily Active Users)
✓ 500 million tweets/day
✓ Read-heavy: Read:Write = 100:1
✓ High Availability: 99.99%
✓ Low Latency: feed < 200msStep 2: Capacity Estimation
Back of envelope calculations
text
Writes:
500M tweets/day = ~5800 tweets/sec (peak: ~15000/sec)
Tweet size: ~300 bytes (text) + media separately
Storage: 500M × 300B = 150 GB/day (text only)
Reads:
100x reads — 580,000 reads/sec
Home feed: user opens app → 20 tweets load
Media:
Average tweet: 10% have images (1MB each)
50M images/day × 1MB = 50 TB/day
→ CDN + object storage (S3) mandatoryStep 3: High-Level Design
Architecture overview
text
[Client App]
↓
[CDN] ← static content (images, videos)
↓
[Load Balancer]
↓
[API Servers] (stateless, horizontally scaled)
↓ ↓ ↓
[Tweet DB] [User DB] [Message Queue]
(Cassandra) (MySQL) (Kafka)
↓
[Fan-out Service]
↓
[Timeline Cache]
(Redis)Step 4: Feed Generation — Fan-out
Feed generation (home timeline) हा Twitter चा biggest engineering challenge आहे. User tweet करतो — त्याच्या सगळ्या followers च्या feed मध्ये त्वरित दिसायला हवा. दोन approaches: Fan-out on Write आणि Fan-out on Read.
- ▸Fan-out on Write (Push): Tweet होताच सगळ्या followers च्या timeline cache update करा. Fast reads. Problem: Celebrity user (100M followers) — 100M cache updates!
- ▸Fan-out on Read (Pull): Feed request आल्यावर followed users चे latest tweets merge करा. Simple. Problem: Slow reads, expensive on every request.
- ▸Hybrid Approach (Twitter actually does this): Normal users → Fan-out on write. Celebrities → Fan-out on read. Celebrity tweets वेगळे merge होतात.
💡
Interview मध्ये trade-offs clearly explain करा. "Celebrity problem मुळे pure fan-out on write काम करत नाही, म्हणून hybrid approach" — हे म्हणणं interviewer ला impress करतं.
✅ Key Points — लक्षात ठेवा
- ▸Requirements → Capacity → Design → Deep Dive
- ▸Read-heavy system: Cache heavily, Read Replicas
- ▸Fan-out on Write: fast reads, slow writes
- ▸Fan-out on Read: slow reads, fast writes
- ▸Hybrid: normal users push, celebrities pull
0/11 chapters पूर्ण