Fan-out on Write vs Fan-out on Read
π§ Core Ideaβ
- Fan-out on Write β Pay cost during write
- Fan-out on Read β Pay cost during read
π Trade-off: Write Amplification vs Read Amplification
Fan-out on Write (Push Model)β
How it worksβ
-
When a user creates a post:
- System pushes it to all followersβ feeds
-
Feed is precomputed and stored per user
Prosβ
- Fast read latency (feed already ready)
- Simple read queries
Consβ
- High write cost (1 post β millions of writes)
- Hot shard / partition issues
- Storage duplication (same post stored many times)
- Partial failures β inconsistent feeds
Key Challengesβ
- Write amplification
- overload
- Fault tolerance (retry, idempotency)
- in case multiple write fails
- Ordering consistency
- Storage explosion
Best Use Caseβ
- Read-heavy systems
- Users with moderate follower count
- Low latency feed requirement
Fan-out on Read (Pull Model)β
How it worksβ
- Post stored once
- On feed request:
- Fetch posts from all followed users
- Merge + sort + rank
Prosβ
- Cheap writes
- No duplication
- Scales well for high-follower users
Consβ
- Slow reads (aggregation required)
- Complex merging logic
- High CPU/memory usage
- Cache invalidation challenges
Key Challengesβ
- Read amplification
- Merge & ranking complexity
- Pagination across multiple sources
- Cold start latency
Best Use Caseβ
- Write-heavy systems
- Users following fewer accounts
- Need for fresh data
Hybrid Approach (Industry Standard)β
Strategyβ
- Normal users β Fan-out on Write
- High-follower users β Fan-out on Read
Flowβ
-
On post:
- Normal β push to feeds
- Celebrity β store once
-
On read:
- Fetch precomputed feed
- Merge celebrity posts dynamically
Advanced Conceptsβ
Async Fan-outβ
Post β Queue β Workers β Feed updates
Consistency Modelβ
- Eventual consistency is acceptable
- Slight delays in feed updates are normal
Backpressure Handlingβ
- Drop or delay low-priority updates
- Use queues to smooth spikes
βοΈ Quick Comparisonβ
| Factor | Fan-out on Write | Fan-out on Read |
|---|---|---|
| Write Cost | High | Low |
| Read Cost | Low | High |
| Latency (Read) | Fast | Slower |
| Scalability | Hard (writes) | Hard (reads) |
| Storage | High | Low |