Design a News Feed System
Ch. 12 Case Studies System designThe problem
A news feed is a read-heavy product that is secretly defined by its writes. When a user posts, the question is not 'store the post' — it is 'how many other feeds does this post have to appear in, and when do they get it'. That fan-out question is the whole design: answer it at write time and reads are a trivial fetch, answer it at read time and every open is an expensive join.
We will pin down the numbers, then walk the write path that builds each follower's inbox ahead of time, the read path that simply fetches it, pagination with a cursor that never skips a post, the media pipeline that turns an upload into a CDN URL, and the ranking pass that orders what is already there. By the end, a post has traveled from one user's screen to a million followers' feeds, and we know exactly when each piece of that journey happened.
Requirements
Functional — what it must do
- Post: a user creates a post — text, optionally with media — and it becomes visible to the feeds of the users they chose to share it with.
- Read: a user opens their feed and sees the posts from people and pages they follow, in the order the system decided, with pagination for more.
- Media: a post can carry images or video; the raw upload is processed into viewable sizes and served from a CDN, not the origin.
Non-functional — how well it must do it
- Opening a feed is a fetch, not a computation: the common read answers in low single-digit milliseconds from a pre-built inbox.
- A post to a user with many followers must not block the poster's response; fan-out is asynchronous and survives a follower count in the millions.
- Pagination must be stable: scrolling down never duplicates or skips a post, even while new posts land at the top.
Scale — how much it must take
- On the order of a hundred million daily active users, a few million posts per hour, and a small number of mega-followers with millions of followers each.
Assumptions — the numbers this design is sized for
A rate limit means nothing without a scale. These are the figures every later part relies on: how much traffic arrives, how much work the limiter has to do per second, and how much latency and memory it may spend doing it.
Takeaways
- A feed is a list of pointers, not a list of posts — the heavy content is fetched and cached separately, which is what keeps a mega-follower's post survivable.
- The fan-out decision is the design: push builds each follower's inbox at write time (cheap reads, write cost proportional to followers), pull assembles at read time (cheap writes, an expensive join per read).
- The hybrid that ships pushes for normal users and pulls for mega-followers, bounding the worst-case write while keeping the common read a simple fetch.
- Acknowledge the poster fast and fan out asynchronously: the post button must not wait for a two-million-follower write.
- Paginate with a cursor on (timestamp, id), not an offset, so scrolling stays stable while new posts land at the top.
- Media rides a separate pipeline — signed direct upload, a processing step, and a CDN — and the feed only ever stores and serves a URL reference.
What a feed entry is
Before the pipeline, fix the unit of work: a feed entry is a lightweight pointer, not the post itself. That distinction is what makes a million-follower post survivable.
- 1A pointer, not a copy
When a post lands in a follower's feed, what is stored is not the full post — it is a pointer: the post's id, a timestamp, and enough to render a row. The heavy content, the media, the author's profile, is fetched separately and cached. A feed is a list of pointers.
- 2Follows define the graph
Who sees a post is decided by the follow graph: a user's feed is the union of the posts of the accounts they follow. That graph is the fan-out's destination set, and its shape — a few hundred edges for most, millions for a few — is what the design has to absorb.
- 3The design question
Everything reduces to one question: when a post is created, do we write it into every follower's feed now, or do we assemble each follower's feed when they open it? The answer is the read/write fan-out decision, and the rest of the chapter is how we live with whichever we pick.
The fan-out decision
Push writes a post into every follower's inbox at post time; pull assembles the feed at read time. Neither is free, and the honest design uses both, each where it wins.
- 1Push: build the inbox ahead of time
Under push, the moment a post is stored, a worker writes a pointer into each follower's inbox. Reads become a trivial fetch of an already-built list. The cost is at write time, and it is proportional to the follower count — cheap for a normal user, catastrophic for a mega-follower.
- 2Pull: assemble at read time
Under pull, the post is stored once and nothing else. When a user opens their feed, the system fetches the recent posts of everyone they follow and merges them. Writes are trivial — including for a mega-follower — but every read now does a merge across the follow set, which is expensive at a hundred million feeds.
- 3The honest answer: both
Real systems split by follower count. Normal users get their feeds pushed, so their reads are trivial. Mega-followers get pulled: their posts are not written into two million inboxes; instead, a follower's feed fetches their recent posts at read time and merges them in. The write-time cost is bounded, and the read-time cost only falls on the small set of people who actually follow a mega-account.
The write path
A post is created, stored, and handed to a worker that fans it out to the right inboxes. The poster's response does not wait for the fan-out to finish.
- 1Store the post first
The feed API validates the post, assigns it an id and a timestamp, and stores it in the post store. At this point the post exists and is immutable. Everything that follows — fan-out, media processing — is downstream of this one durable write.
- 2Enqueue the fan-out
Instead of fanning out inline, the API enqueues a fan-out job and returns to the poster. The poster sees 'posted' in a few milliseconds; the worker does the slow, large fan-out in the background. This is what makes a two-million-follower post not freeze the post button.
- 3Fan out by strategy
The worker reads the author's follower count. Below a threshold it pushes: it writes a pointer into each follower's inbox. Above it, it does nothing per-follower — the mega-account's posts will be pulled in at read time instead. One post, two strategies, chosen by a single number.
The read path and pagination
Opening a feed is a fetch of the viewer's pre-built inbox, a merge in for any mega-followers, and a stable cursor so scrolling never skips or repeats a post.
- 1Fetch the inbox, not the graph
The viewer opens the feed. The API reads their inbox — the list of pointers that was built by push — and returns the most recent page. For a normal user this is a single fast read of a pre-built list, which is why the feed feels instant.
- 2Enrich and merge the mega-followers
The pointers are hydrated: each post's media ref is resolved to a CDN URL, and any posts from mega-followers — which were not pushed — are fetched and merged into the page. The client gets a fully-renderable feed from what was, at rest, a thin list of ids.
- 3Page with a cursor, not an offset
Scrolling asks for the next page. The cursor is the timestamp and id of the last entry seen, so the next fetch is 'everything older than this'. An offset would skip posts when new ones land at the top and repeat them when the list shifts; a cursor tracks the data itself and stays stable.
The media pipeline
A post's media is a separate problem: a large upload, a processing step, and a CDN in front. The feed stores a reference; the bytes travel a different path entirely.
- 1Upload direct to the store
The client does not push a photo through the feed API. It asks the upload service for a signed URL and uploads the bytes straight to the object store. That keeps large media off the request path of the feed, and lets the store scale independently of the feed tier.
- 2Process into the sizes we serve
The raw upload is never served as-is. A processor transcodes video and resizes images into the handful of sizes the product actually shows. The result is a set of processed artifacts, each addressed by a stable URL, and the original is kept for reprocessing.
- 3Serve from the edge
When a feed is rendered, each media ref is resolved to a CDN URL. The viewer's browser fetches the image or video from the edge, not from the origin. The feed API never moves a single byte of media — it only ever hands out the URL that points at it.
Ordering what is already there
The feed has the right posts; now they need the right order. Ranking is a scoring pass over the pre-built inbox, not a change to the write path.
- 1Score, then sort
A ranking pass assigns each entry in the page a score from a handful of factors — recency, how the viewer engaged with this author before, whether the post is media — and sorts the page by that score. The set of posts is unchanged; only their order is.
- 2Why rank at read, not write
Ranking is a read-time step because it is personal: the same post orders differently in two viewers' feeds. If it were baked in at write time, the fan-out would have to store a different order per follower — which is exactly the per-follower write cost we spent the whole fan-out section avoiding.
- 3The trade-off accepted
A ranked feed is no longer strictly chronological, and some users prefer the reverse. That is a product choice layered on top of the same machinery: the write path builds the inbox, the read path fetches it, and ranking is the last, cheap, personal pass over a page of twenty.
End to end
One post, a crowd of followers: it is stored, fanned out to the right inboxes, fetched and merged on the other side, with the media riding a separate edge path.
- 1The whole system
Every piece in one frame. A poster's post is stored and fanned out to follower inboxes; a viewer opens their feed, which is fetched from their inbox, merged for mega-followers, and hydrated with media from the CDN.
- 2A normal post, pushed
A user with a few hundred followers posts. The API stores it, the poster is acknowledged, and the worker writes a pointer into each of those few hundred inboxes. A moment later, each of those followers sees it at the top of their feed without any extra work from them.
- 3A mega-post, pulled
An account with two million followers posts. Nothing is written into two million inboxes — that would be the write explosion the design exists to avoid. Instead, the post is stored once, and each of the two million followers pulls it in when they open their feed.
- 4Opened, merged, served
A follower opens their feed. The API fetches their pre-built inbox, merges in the recent posts of the mega-accounts they follow, hydrates each entry with its media URL, ranks the page, and returns it. The whole read is a fetch plus a merge — the write-time fan-out did the heavy lifting long ago.
In the wild
Push/pull hybrid feeds are the dominant design in social products. The boxes below are anchored to the classes of well-known systems that run each piece — an illustrative mapping, not a claim about any one product's internals.
- 1The pattern, piece by piece
Each box is a standard building block of a large social feed. Anchoring them to the classes of production systems that run that piece turns the abstract pipeline into 'the shape behind the feeds you scroll'.
- 2Why the same split keeps winning
Independent social products converge on the same hybrid because each piece removes a real failure: the push side removes the read-time join, the pull side removes the mega-follower write explosion, the cursor removes pagination drift, and the media pipeline removes origin load. That convergence is the evidence the design is load-bearing.