09. Whole Systems / Lesson 3 of 4
Design a News Feed
Build each person's home feed from the people they follow, fast.
- 14 min
- Advanced
- 12 steps
- Feed load—
- Writes/s—
- Fan-out size—
- Request
- Response
- Cache hit
- Cache miss
- Write
- Health check
Step 1 of 12: Show me what my friends posted
Read every step as text
Step 1: Show me what my friends posted
The home feed is the first thing you see: recent posts from the people you follow, in a good order, in a blink. Two operations: publish a post, and load my feed.
It looks easy, but each person follows a different group. So every person has a different feed, and a post may need to show up in thousands of them.
Requirements: a fresh feed, fast to load, even for people with huge audiences.
Step 2: Napkin math: users, reads and writes
Say 100,000,000 people open the app each day, 5 times each: 500,000,000 feed loads a day, which is about 5,787 a second. Say 10,000,000 posts a day: 116 a second.
Reads outnumber posts about 50 to 1. A normal account has 200 followers, so every post could mean 200 feed updates: 116 × 200 = 23,200 a second. Keep these in mind for the next steps.
Feeds are read-heavy, so make the read path cheap.
Step 3: The simplest version: one query
Keep two tables: posts and who follows whom. To load a feed, find the people I follow, take their latest posts, sort by time and return 20.
In SQL that is one query with a subquery. It is correct and tiny, and it is where you start. The next step looks at what it costs when many people do it at once.
Always say the simple version first; then show where it breaks.
Step 4: Fan-out on read: build the feed when it is opened
This is fan-out on read (pull). Every feed load looks at each of the 200 accounts I follow. Posting is cheap (one write), but reading is heavy: 5,787 loads × 200 lookups = 1,157,400 queries a second.
Even with 10 lookups at a time, 20 rounds × 5 ms plus 20 ms to merge is 120 ms, on the busiest path of the product.
The work happens at the worst moment: while the user waits.
Step 5: Fan-out on write: build the feeds when a post is made
Flip it. When someone posts, a fan-out worker looks up their followers and adds the post id to each follower's ready-made feed list. Reading is now one list lookup plus fetching 20 posts: 5 + 10 = 15 ms.
The price moves to writing: 116 posts × 200 followers = 23,200 list writes a second. These happen in the background through a queue, so the person posting does not wait.
Do the work once at write time instead of many times at read time.
Step 6: One post, five million writes
A celebrity with 5,000,000 followers posts. Fan-out on write must add that post to 5,000,000 feed lists. At 100,000 writes a second the workers need 50 seconds, and every other post waits behind them in the queue.
If 1 post in 100 is a celebrity's, the load becomes about 5.8 million list writes a second, nearly all for accounts most of whom may not even open the app today.
Fan-out size varies enormously: an average of 200 hides a few accounts with millions.
Step 7: Hybrid: push for most, pull for celebrities
Use both. Normal accounts are pushed into followers' feed lists, as before. Accounts above a limit (say 1 million followers) are not pushed; their posts stay in the post store, and a feed load pulls the few celebrities I follow and merges them in, at a cost of about 8 ms.
Writes fall back to about 23,000 a second, with no spikes. A feed load is 15 + 8 = 23 ms. Most people follow only a handful of celebrities, so the pull is small.
Pick the method per account: push where the audience is small, pull where it is huge.
Step 8: Cache the feeds and the posts
Keep the newest part of each active user's feed list, and the popular post bodies, in memory (see caching). A list read drops from 5 ms to 2 ms, and with 90% post hits fetching 20 posts takes about 3.0 ms instead of 10 ms.
A feed load is now 2 + 3.0 + 8 = 13 ms. Only recent entries need to be cached: nobody scrolls back a month, and an empty cache entry is rebuilt from the post store.
Cache the feed for people who visit often; rebuild it for people who come back after a year.
Step 9: Rank, do not only sort by time
Newest-first is simple, but people prefer a feed of the posts they care about. A ranker takes the few hundred candidate posts from the lists, gives each a score, and returns the best 20.
A simple score multiplies a few signals: how close I am to the author, how recent the post is, and how many people liked it. Ranking adds about 15 ms, so a feed load is 28 ms. Heavy models run offline and only look scores up.
Keep ranking cheap and bounded: score a few hundred candidates, never the whole feed.
Step 10: Photos and videos go through a CDN
The feed answer is only text and links. The photos are big: 20 photos of 200 KB is 4 MB a load, and 5,787 loads a second is 23.1 GB a second, about 185 Gbit/s. Our servers must never carry that.
Store the files in object storage and serve them from a CDN near the user (see the content delivery lesson). The feed API only returns each photo's URL.
Metadata travels through our system; the heavy bytes travel through the CDN.
Step 11: Scrolling: use a cursor, not page numbers
The first load returns 20 posts. When the user scrolls, the app asks for the next 20. With page numbers (
page=2), a new post arriving at the top shifts every item down, so the user sees one post twice.Instead return a cursor: the id or time of the last post seen.
GET /feed?after=p_9031&limit=20means "20 posts older than p_9031". The list stays stable, and every page costs about the same.Cursor pagination also stays fast deep in the list; big offsets make the database skip many rows.
Step 12: The news feed in one picture
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.