#📝 Note: Fan-Out Pattern
#Overview
Fan-out is the strategy of copying or delivering a message/event from one source to many destinations. The central trade-off is write amplification vs read latency: fan-out-on-write is fast to read but expensive to write; fan-out-on-read is cheap to write but slow to read.
#Core Idea
#Fan-Out-on-Write (Push Model)
When a user posts/acts, immediately propagate the result to all followers' inboxes.
- Read: O(1) — read pre-computed inbox.
- Write: O(N) — write to N followers. Expensive for celebrities with millions of followers.
#Fan-Out-on-Read (Pull Model)
When a user opens the feed, fetch content from all followed accounts in real-time.
- Read: O(N) — query N accounts on every feed load. Slow.
- Write: O(1) — no fan-out, just write the event.
#Hybrid Model (Recommended at Scale)
- Regular users (< threshold followers): Fan-out-on-write.
- Celebrity users (> threshold): Fan-out-on-read, merged at request time.
- Inactive users: Skip fan-out entirely; recompute on first login.
#Comparison Table
| Aspect | Fan-Out-on-Write | Fan-Out-on-Read | Hybrid |
|---|---|---|---|
| Read latency | O(1) fast | O(N) slow | O(1) for most |
| Write cost | O(N) expensive | O(1) cheap | Mixed |
| Storage | High (duplicated) | Low | Moderate |
| Celebrity problem | Write storms | Read storms | Solves both |
| Stale data risk | Low | None | Low |
#When to Use
- Fan-Out-on-Write: Uniform follower counts, low write rate, latency-sensitive feeds (e.g., Twitter DMs).
- Fan-Out-on-Read: Celebrity-heavy platform, high write volume, acceptable read latency.
- Hybrid: Any large-scale social feed (Twitter, Instagram, Tinder, LinkedIn).
#When NOT to Use
- Fan-Out-on-Write: When users have millions of followers — write amplification is catastrophic.
- Fan-Out-on-Read: When feed refresh is frequent and follower count is high — read storms.
#Trade-offs
- Pros (Hybrid): Balances write and read costs; gracefully handles heterogeneous user graphs.
- Cons (Hybrid): Complex merge logic; need a "celebrity" threshold that can drift over time.
#Used In This Repo
- Tinder Feed (#26) — Candidate stack pre-computation uses a push model for active users, pull for inactive
- E-Commerce (#11) — Product recommendations fan-out to user home pages