System Design · 09. Whole Systems · 14 min · Advanced

Design a News Feed

Build each person's home feed from the people they follow, fast.

In short

5,787 feed loads and 116 posts a second. Pull makes reads heavy (120 ms); push makes writes heavy and breaks on celebrities; the hybrid pushes normal accounts and pulls celebrities.

A feed and post cache, a small ranker and a CDN for photos bring the feed load to about 28 ms. Cursors keep scrolling stable.

Key ideas

  • Requirements: a fresh feed, fast to load, even for people with huge audiences. (step 1)
  • Pick the method per account: push where the audience is small, pull where it is huge. (step 7)
  • Metadata travels through our system; the heavy bytes travel through the CDN. (step 10)

Good to know

  • Feeds are read-heavy, so make the read path cheap. (step 2)
  • Always say the simple version first; then show where it breaks. (step 3)
  • Do the work once at write time instead of many times at read time. (step 5)
  • Cache the feed for people who visit often; rebuild it for people who come back after a year. (step 8)
  • Cursor pagination also stays fast deep in the list; big offsets make the database skip many rows. (step 11)

Watch out

  • The work happens at the worst moment: while the user waits. (step 4)
  • Fan-out size varies enormously: an average of 200 hides a few accounts with millions. (step 6)
  • Keep ranking cheap and bounded: score a few hundred candidates, never the whole feed. (step 9)
CDNUsersRankerFeed APIPost storeNew-post queueFan-out workersFeed + post cacheFeed lists

Key numbers

Feed load
28 ms
Writes/s
23,000/s
Fan-out size
200; celebrities 0

Terms

Fan-out
Spreading one action, such as a post, out to many places, such as every follower's feed.
Fan-out on read (pull)
Build a feed when it is opened by looking at each account the user follows.
Fan-out on write (push)
When a post is made, add it to every follower's ready-made feed list.
Celebrity problem
An account with millions of followers makes one post cause millions of feed writes.
Hybrid fan-out
Push posts of normal accounts, but pull the posts of celebrities when the feed is read.
Feed cache
Recent feed lists and popular posts kept in memory for fast reads.
Feed ranking
Scoring candidate posts and returning the best, rather than only the newest.
Cursor pagination
Asking for the next page by the last item seen, so the list does not shift as new posts arrive.