Design a News Feed: Fan-Out, the Celebrity Problem and Ranking
Fan-out on read versus fan-out on write, why accounts with millions of followers break the simple model, and how the hybrid approach keeps the feed instant.
A news feed shows a user the recent posts from the accounts they follow. A user may follow hundreds of accounts, and yet the feed is expected to appear the moment the app opens.
The central design decision is when the feed is assembled: at the time of reading, or at the time of posting. These notes explain both models, where each one breaks, and the hybrid design that is the standard answer.
1. Requirements
- Functional: a user can create a post, follow other users, and view a feed of posts from followed accounts, newest first.
- Non-functional: the feed loads quickly, around 200 ms.
- Non-functional: high availability. The feed should always load, even if it is slightly out of date.
- Consistency: it is acceptable for a new post to appear in followers' feeds a few seconds late. This is eventual consistency.
- Workload: read-heavy. Users open the feed far more often than they post.
2. Scale estimation
Assume 100 million daily active users, each opening the feed 10 times a day and posting once every two days. Feed reads are 1 billion per day, about 10,000 per second on average and 30,000 at peak. Posts are 50 million per day, about 500 per second. If the average user has 200 followers, 500 posts per second become 100,000 feed insertions per second when posts are pushed to followers. This last number is the cost of the fan-out on write model, and it is the reason workers and queues are needed.
3. API and data model
POST /api/v1/posts
Body: { "text": "...", "mediaIds": ["m_91"] }
Response: 201 Created { "postId": "p_7781" }
GET /api/v1/feed?cursor=<opaque>&limit=20
Response: 200 OK { "posts": [ ... ], "nextCursor": "..." }
POST /api/v1/users/{id}/follow
DELETE /api/v1/users/{id}/followposts (post_id PK, author_id, text, media_url, created_at)
follows (follower_id, followee_id, created_at) PK (follower_id, followee_id)
+ reverse index on followee_id -> "who follows this author?"
Feed cache (Redis sorted set, one per user)
key : feed:{userId}
member: postId
score : post timestamp
size : capped at the newest ~800 entries4. Fan-out on read (pull model)
In the pull model the feed is built when the user opens the app. The service fetches the list of followed accounts, queries the recent posts of each, merges them and sorts by time.
Writing a post is cheap: one insert. Reading is expensive: a user who follows 500 accounts triggers hundreds of lookups and a merge on every refresh. Since reads are the dominant operation, this model puts the heavy work on the hot path, and feed latency becomes poor at scale.
5. Fan-out on write (push model)
In the push model the work is done when a post is created. The post is stored, and then a fan-out service looks up the author's followers and inserts the post ID into each follower's feed cache. When a follower opens the app, the feed is already assembled; reading it is a single range query on one key.
Reads become very fast, which is what a read-heavy system needs. The cost is moved to the write side, and it is paid asynchronously: the post API returns as soon as the post is stored and a fan-out job is queued. Workers process the job in the background, which is why a new post may take a few seconds to reach all followers.
WRITE PATH
Client --> Post service --> Posts DB
|
v
Fan-out queue --> Fan-out workers --> Follow graph (who follows the author?)
|
v
Feed cache: ZADD feed:{followerId} for each follower
READ PATH
Client --> Feed service --> Feed cache (post IDs)
| + pull recent posts of followed celebrities, merge
v
Post cache / Posts DB (hydrate IDs into full posts) --> rank --> response6. The celebrity problem
Fan-out on write works when follower counts are modest. An account with 50 million followers breaks it: a single post requires 50 million cache insertions. The fan-out queue backs up, other users' posts are delayed behind it, and most of those insertions are wasted on followers who will not open the app before the entry is pushed out of their feed.
This is known as the celebrity problem or the hot key problem, and it is the most important part of this design.
7. The hybrid model
The standard solution combines both models. For ordinary accounts, use fan-out on write. For accounts above a follower threshold, skip the fan-out entirely. Their posts are stored only in their own timeline.
When a user opens the feed, the service reads the precomputed feed from the cache and, in addition, fetches the recent posts of the few celebrity accounts that user follows. It merges the two lists and returns the result. A user follows only a handful of such accounts, so this adds a small, bounded amount of read work, and the recent posts of a celebrity are themselves heavily cached because millions of users request them.
A further optimisation is to fan out only to active users. There is no value in maintaining a feed cache for someone who has not opened the app in a month; their feed can be rebuilt with the pull model when they return.
8. Store IDs, not content
The feed cache holds post IDs only. The post content lives in a separate post cache and in the posts database. If full posts were copied into every follower's feed, a post with a million followers would be stored a million times, and editing or deleting it would require a million updates.
With IDs only, the feed entry is a few bytes, and the read path performs a second step called hydration: fetch the posts for those IDs with one multi-get from the post cache. Deleted posts are simply dropped during hydration.
9. Ranking and pagination
The simplest feed is reverse chronological. A ranked feed adds a scoring step after the candidates are collected: each post receives a score based on signals such as recency, the viewer's past interaction with the author, and engagement on the post, and the top results are returned. In an interview it is enough to present ranking as a separate stage that takes candidate posts and returns an ordered list.
Pagination should use a cursor, such as the timestamp or ID of the last post seen, rather than an offset. New posts arrive at the top continuously, and offset-based pages would shift and show duplicates.
10. Additional considerations
- Unfollow: remove that author's posts from the follower's feed cache lazily, or filter them out during hydration by checking the follow relationship.
- New follow: backfill a few recent posts from the newly followed account into the feed.
- Cache loss: the feed cache is derived data. If it is lost, rebuild it with the pull model from the posts database.
- Media: images and videos are stored in object storage and delivered by a CDN; the feed carries only URLs.
Interview summary
- Requirements: fast feed, high availability, eventual consistency, read-heavy.
- Fan-out on write: push post IDs into each follower's feed cache in Redis, asynchronously.
- Celebrities: fan-out on read for high-follower accounts, merged at read time. This is the hybrid model.
- Feed cache stores IDs only; content is hydrated from a separate cache.
- Ranking: chronological first, then engagement-based scoring as a separate stage.
Summary
Building the feed at read time is too slow for a read-heavy product, so posts are pushed into followers' feed caches when they are created. Accounts with very large follower counts are excluded from the push and merged in at read time. The cache stores only post IDs, content is hydrated separately, and ranking is applied as a final stage.
Puneet Tiwari
Full Stack Developer
