Design a News Feed like Facebook or X
Build every user a ranked timeline from the people they follow.
Asked at Meta, Twitter and 3 more
3 min read·7 sections
A news feed shows each user a ranked list of posts from the people and pages they follow. Facebook, X, LinkedIn and Instagram all have one. The central question is when to assemble a user's feed: at write time when someone posts, or at read time when someone opens the app.
Requirements#
Functional
- Users publish posts (text, images, links).
- Users see a feed of recent posts from accounts they follow, ranked.
- The feed supports infinite scroll.
Non-functional
- Feed loads in under 500 ms.
- 300 million daily users, each opening the feed several times a day.
- A new post can take a few seconds to appear in followers' feeds.
Estimates#
Reads outnumber writes about 20 to 1, so we optimise reads.
Fan-out on write vs fan-out on read#
Fan-out on write (push)
When a user posts, push the post ID into the precomputed feed of every follower. Reading a feed is then a single lookup. The problem is celebrities: one post from an account with 50 million followers means 50 million writes.
Fan-out on read (pull)
Store posts only with their author. When a user opens the app, fetch recent posts from everyone they follow and merge them. Writes are cheap, but reads become slow and expensive.
The hybrid
Push for normal accounts, pull for celebrities. At read time, merge the user's precomputed feed with recent posts from the few celebrities they follow.
Architecture#
- Feed cache: for each user, a Redis list or sorted set of the latest few hundred post IDs. Only IDs, not full posts, to save memory.
- Hydration: the feed service turns IDs into full posts (author, text, media URLs, like counts) from a post cache.
Ranking#
Chronological feeds are simple, but most products rank by predicted engagement. A typical pipeline:
- Candidate generation: gather a few hundred recent posts from the feed cache and celebrity pulls.
- Scoring: a model predicts the chance of a like, comment or share, using features such as affinity with the author, post age and media type.
- Re-ranking: apply diversity rules, so the feed isn't five posts in a row from one friend.
Pagination#
Use cursor-based pagination (the last seen post ID or score) rather than page numbers. New posts arriving at the top would otherwise shift pages and cause duplicates.
Edge cases#
- Inactive users: don't fan out to accounts that haven't logged in for 30 days; build their feed on read when they return.
- Deletes and privacy changes: filter at hydration time, since removing an ID from millions of feeds is slow.
- New follows: backfill a few recent posts from the newly followed account into the feed.
Finished this walkthrough?
Mark it as read to track your progress in the list.