- rtshkmr's digital garden/
- References/
- Architecture Design Basics/
- Pattern Taxonomy/
- Domain-Specific Patterns/
- Real-time & Fan-out Patterns/
Real-time & Fan-out Patterns
··
200 words·
1 min
Table of Contents
🟠P1 — how to push updates to many recipients; classic interview problem
Problem #
A user posts a message. Their 10,000 followers need to see it. Do you push it to all followers’ feeds immediately, or let followers pull it when they open the app?
Fan-out Strategies #
| Strategy | Mechanism | Pros | Cons |
|---|---|---|---|
| Fan-out on write | On post, write to every follower’s feed | Read is fast (pre-computed) | Write amplification (10K writes per post) |
| Fan-out on read | On read, query poster’s timeline | Write is fast (one write) | Read is slow (assemble from many sources) |
| Hybrid | Fan-out on write for normal users, fan-out on read for celebrities | Balanced | More complex |
Presence & Ephemeral State #
- Heartbeat-based: Client sends periodic heartbeats; server marks offline after misses
- Redis with TTL:
SETEX user:123:online 30 true, refreshed by heartbeats
Instinct: Presence is best-effort. Don’t store in primary database. Redis with TTL keys is the standard.
Instinct #
Hybrid is the standard answer for social-media-style problems. Fan-out on write for users with <10K followers (pre-compute their feeds). Fan-out on read for celebrities (too many followers to write to). This is what Twitter famously does. The threshold is tunable based on write capacity.