A news feed shows each user a personalised stream of posts from people they follow. The core challenge is delivering fresh feeds to millions of users cheaply — and the famous fan-out trade-off is at its heart.
1. Requirements
- Functional: users post content; users see a feed of posts from those they follow, newest/most-relevant first.
- Non-functional: very read-heavy, feed loads must be fast (<200ms), eventual consistency is acceptable (a post appearing a few seconds late is fine).
2. Two ways to build a feed
Fan-out on write (push): when a user posts, immediately write that post into the precomputed feed cache of every follower. Feed reads are then instant (just read your list). But a post by someone with millions of followers triggers millions of writes.
Fan-out on read (pull): store posts once; build each user's feed on demand by fetching recent posts from everyone they follow. Cheap writes, but reads are heavy and slower.
3. The celebrity problem & the hybrid
Push breaks for celebrities (millions of followers → write storm); pull is slow for users who follow many people. Real systems use a hybrid: push for normal users, and pull for a small number of very-high-follower accounts, merging their recent posts into your feed at read time.
4. Storage & caching
Posts live in a scalable store (wide-column like Cassandra suits high write volume). Precomputed feeds live in a fast cache (Redis lists) holding just the recent items. Media goes to blob storage behind a CDN. The fan-out work runs asynchronously through message queues.
5. Ranking
A chronological feed is simplest. A ranked feed scores posts by recency, engagement and affinity. Start chronological in an interview, then mention ranking as an extension so you don't over-scope.