One request, a few hundred milliseconds
A reader opens the app and the client sends GET /feed. Within a few hundred milliseconds it has to come back with about 20 posts from the 200 accounts this reader follows, ranked so the ones they are most likely to care about come first, each with its author, text and counts of likes and comments. Scrolling asks for the next 20. A post one of those 200 accounts publishes now should be in this list within seconds, at the reader's next refresh. Everything else in this design exists so that this one request stays cheap.
Now multiply it. Say 300 million daily users, half of whom post once a day (150 million posts ÷ 86,400 s ≈ 1,700 posts a second) and all of whom open the feed five times (≈ 17,000 feed reads a second). An account has 200 followers on average; the top accounts have 50 million. Storage is the easy part: 150 million posts a day × 1 KB ≈ 150 GB a day into a post store partitioned by author. What makes the problem hard is the 10:1 read:write ratio combined with a wildly skewed follower graph: the cheap trick for the median user is a catastrophe for the celebrity. Turning a vague brief into numbers like these is the first step in Approaching a system design problem.
IDs in the feed, posts in one place
Each post is stored once, sorted by time within its author, under an ID that is itself time-ordered, which the paging below relies on (Unique ID generation shows how to mint such IDs on many machines). Follows are kept in both directions, followers of X and who X follows, as adjacency lists keyed by user in a table or a key-value store; every question here is one hop, which is why no graph database is needed (when one is, see Graph databases). What the reader's request reads first is a feed cache: one bounded list per user of roughly the 500 newest post IDs they should see.
IDs, not posts. With a copy of the post in each of 200 followers' lists, every edit or delete would be 200 writes, and any one that failed would show stale text. With IDs, the body lives once in the post store and is fetched only for the 20 posts on screen, a step called hydration. The general trade between a copy and the cost of keeping it correct is the ground of Normalisation & denormalisation.
Precompute for 200 followers, not for 50 million
Building the feed on every open would mean merging the recent posts of 200 accounts, 17,000 times a second. Precomputing it, appending each new post's ID to every follower's list (fan-out on write), costs about 1,700 × 200 ≈ 340,000 inserts a second, which a sharded in-memory store can be sized to absorb, and turns the read into one list lookup. One post from a 50-million-follower account would be 50 million inserts, so authors above a threshold, say 10,000 followers, are skipped, and their recent posts are pulled when a follower opens the feed (fan-out on read). Why the split works, where to put the line, and what it does for inactive followers, unfollows and deletes, is the whole of Fan-out: push vs pull.
Publishing a post
The author's request does the minimum: the Post Service stores the post, enqueues one fan-out job and answers. A crash between the store and the enqueue would leave a post that reaches no feed, the gap Transactional Outbox closes. Fan-out workers take the job, read the author's followers and add the post ID to each follower's list, which is kept sorted by post ID, not by arrival (a sorted set), and trimmed to the newest 500. Behind the queue, the publish request does the same work for 200 followers as for 9,000, store the post and enqueue one job; as long as the workers keep up with the queue, the last followers get the post a few seconds after the first.
Opening the feed: merge, rank, hydrate
Back to the reader's GET /feed. It runs in this order:
- The Feed Service reads the reader's precomputed IDs from the feed cache.
- It looks up which above-threshold accounts the reader follows, usually a handful, and pulls their recent post IDs from the post store.
- It merges both into one candidate set of a few hundred IDs.
- The Ranking Service reorders the candidates using engagement features.
- The Feed Service hydrates the top 20 (post bodies, authors, counts) and returns them.
Step 4 of the list, the ranking, is a model call inside a request with a budget of a few hundred milliseconds, so it gets a deadline and a fallback: when the Ranking Service is slow or down, the Feed Service returns the merged candidates newest first. A slow model should cost the reader the ordering, not the feed; a timeout that ends in an empty screen is a design mistake. How the model picks and scores candidates is explained in Recommendation systems: retrieval, ranking & cold start.
Page two
Paging by offset, "items 21 to 40", fails on a feed in two ways. New posts arrive at the top between requests and push everything down, so the reader sees the end of page one again; and a second ranking pass can reorder the candidates, so some posts show twice and others never. So each response carries a cursor instead of a page number. One common design ranks the candidate set once when the reader opens the feed, keeps that ordered list of IDs in the cache for a few minutes, and makes the cursor a position in it; new posts wait for the next refresh, and if that list has expired or been lost, the next page starts a fresh feed from the top. Past the end of the candidates, the cursor carries where each source stopped while the candidates were collected, not a position in the ranked order: the oldest post ID read from the feed cache and from each pulled account. That boundary skips nothing already in the cache, because the feed cache is sorted by the time-ordered post ID: a fan-out job that arrives late lands in its place, not at the top, and if that place is above a boundary the reader already passed, the post shows up on the next refresh instead. The next page reads below each boundary: further down the feed cache while it lasts, then a range read per followed author, the read-time path again, a cost worth accepting when few readers scroll that far. For keyset and cursor paging outside feeds, read Pagination: offset, keyset & cursors.
Two caches that carry the load
The feed cache is the one every request touches, and it is derived: if it is lost, feeds can be rebuilt on read from the post store and the follow lists, through the same path the celebrity merge already uses, and no post is lost, because those stores still hold everything. It is not free: after a large loss every open is that merge, the load precomputing exists to avoid, so the rebuild has to be paced to what the post store can serve. The other is the celebrities' recent posts, a few thousand short lists each read by millions of followers on every open. Those are hot keys, and the moment one expires under that load is where Hot keys & cache stampedes picks up. The rest, a post cache for hydration and the general trade-offs, follows Caching layers.
In the News Feed Challenge
Open the News Feed Challenge and make the hybrid split visible: the fan-out queue and workers on the write path, and a read path that merges the feed cache with a direct pull of celebrity posts before ranking, with the threshold written on the fan-out edge. Author and reader are the same app: draw one client whose requests enter through one API gateway, which routes POST /posts to the write path and GET /feed to the read path. Photo Sharing Feed is the same skeleton with a heavier payload: the post store shrinks to metadata, and object storage plus a CDN carry the images.