Design a home timeline (Twitter/Instagram style) that shows recent posts from accounts a user follows.
How to approach it
Scale by following the request path and fixing the first real bottleneck. Reads usually dominate, so caching and replication come first; writes and storage are handled by sharding on a high-cardinality key. Push slow, retryable work onto queues. Add infrastructure only where the numbers justify it.
Scaling decisions
| Area | Choice |
|---|---|
| Fan-out | Fan-out-on-write for normal users, on-read for celebrities (hybrid) |
| Cache | Store each user timeline as a capped list in Redis |
| Sharding | Shard posts by author_id |
| Queue | Asynchronous fan-out via a queue |
Why
- Fan-out: precomputed feeds make reads cheap, but fanning a celebrity post to millions is too expensive
- Cache: O(1) timeline reads; only keep the most recent N entries
- Sharding: spreads write load and keeps an author’s posts together
- Queue: keeps publish latency low and absorbs bursts