Design a News Feed
2 min read
Design a News Feed (Twitter / Instagram)
1. Requirements
- Functional: publish a post; view a feed of followed accounts, newest/ranked first.
- Non-functional: read-heavy, low feed latency, eventual consistency is fine.
2. The core question: when do we build the feed?
Fan-out on write (push) — when you post, copy it into every follower's precomputed feed.
- ✅ Feed reads are instant.
- ❌ A celebrity with 50M followers triggers 50M writes — the fan-out problem.
Fan-out on read (pull) — build the feed on demand by querying everyone you follow.
- ✅ Cheap writes.
- ❌ Slow reads, heavy DB load per request.
3. The hybrid (what real systems do)
- Push for normal users (precompute feeds into Redis).
- Pull for celebrities — fetch their recent posts at read time and merge.
4. High-level design
Post ─▶ Post service ─▶ DB
└─▶ Fan-out service ─▶ Redis feed lists (per user)
Feed ─▶ Feed service ─▶ Redis + pull celebrity posts ─▶ Rank ─▶ Client
5. Deep dives
- Ranking: newest-first is easy; engagement ranking needs a scoring model.
- Storage: keep feeds bounded (last N posts).
- Media: store in object storage + CDN; the feed holds only URLs.
- Pagination: cursor-based (
since_id), not offset — stable under inserts.
Takeaway: news feeds are a fan-out problem. The hybrid push/pull model is the standard answer.