The Challenge
Building a timeline system that can:
Key Constraints
Fan-Out Approaches
Approach 1: Fan-out on Write (Push Model)
When a user tweets, push the tweet to all followers' timelines.
Architecture:
1User tweets → Write to DB → Fan-out service → Update N follower timelinesPros:
Cons:
Example:
Approach 2: Fan-out on Read (Pull Model)
When a user loads timeline, fetch tweets from all followees.
Architecture:
1User requests timeline → Fetch tweets from N followees → Merge & sort → ReturnPros:
Cons:
Example:
Hybrid Approach (Best Solution)
Use fan-out on write for regular users and fan-out on read for celebrities.
Architecture
1┌─────────────┐2│ User Tweets │3└──────┬──────┘4 │5 ▼6┌─────────────────┐7│ Tweet Service │8└──────┬──────────┘9 │10 ├─────────────────┬─────────────────┐11 ▼ ▼ ▼12┌─────────────┐ ┌─────────────┐ ┌─────────────┐13│ Regular User│ │ Celebrity │ │ Timeline │14│ Fan-out │ │ Fan-out │ │ Merge │15│ (Push) │ │ (Pull) │ │ Service │16└─────────────┘ └─────────────┘ └─────────────┘17 │ │ │18 └─────────────────┴─────────────────┘19 │20 ▼21 ┌──────────────┐22 │ Timeline │23 │ Cache │24 └──────────────┘Implementation Strategy
- Tweet → Push to follower timelines
- Store in Redis cache
- Tweet → Store in DB only
- On timeline load → Fetch celebrity tweets separately
- Sort by timestamp
- Apply ranking algorithm
- Return top N tweets
Database Schema
1-- Tweets table2CREATE TABLE tweets (3 id BIGINT PRIMARY KEY,4 user_id BIGINT,5 content TEXT,6 created_at TIMESTAMP7);8 9-- Follows table10CREATE TABLE follows (11 follower_id BIGINT,12 followee_id BIGINT,13 PRIMARY KEY (follower_id, followee_id)14);15 16-- Timeline cache (Redis)17-- Key: timeline:{user_id}18-- Value: List of tweet IDs (sorted by timestamp)Key Optimizations
1. Caching Strategy
2. Ranking Algorithm
Not just chronological! Consider:
3. Pagination
4. Real-time Updates
Scaling Considerations
Horizontal Scaling
Monitoring
Trade-offs
| Approach | Write Latency | Read Latency | Storage | Complexity |
|----------|---------------|--------------|---------|------------|
| Fan-out Write | High | Low | High | Medium |
| Fan-out Read | Low | High | Low | High |
| Hybrid | Medium | Low | Medium | Very High |
The hybrid approach balances performance with complexity, which is why it's used by Twitter, Facebook, and other social platforms.