Design Twitter
Problem
Design Twitter with timeline, tweets, follow/unfollow.
Requirements
- Post tweets (280 chars)
- Timeline (following users' tweets)
- Follow/unfollow users
- Scale: 300M DAU, 100M tweets/day
Key Challenges
- Fanout problem: User posts → fanout to followers' timelines
- Celebrity problem: Can't fanout to 100M followers
- Hot keys: Trending tweets (cache)
High-Level Design
Data Model
CREATE TABLE users (
user_id BIGINT PRIMARY KEY,
username VARCHAR(50) UNIQUE
);
CREATE TABLE tweets (
tweet_id BIGINT PRIMARY KEY,
user_id BIGINT,
content VARCHAR(280),
created_at TIMESTAMP
);
CREATE TABLE follows (
follower_id BIGINT,
followee_id BIGINT,
PRIMARY KEY(follower_id, followee_id)
);
Timeline Generation
Fanout-on-Write (Push Model)
User posts tweet → Push to all followers' caches
Timeline read: Read from cache (fast)
Pros: Fast reads
Cons: Slow writes for celebrities
Fanout-on-Read (Pull Model)
User posts tweet → Store in DB
Timeline read: Query all followed users, merge tweets
Pros: Fast writes
Cons: Slow reads (many queries)
Hybrid (Twitter's Approach)
- Regular users: Fanout-on-write
- Celebrities (>1M followers): Fan out-on-read
- Merge both in timeline
Scalability
- Cache: Redis cluster (timeline = sorted set by timestamp)
- Sharding: User ID for tweets/follows
- Message queue: Async fanout (Kafka)
Next: Design Instagram.