How to design the Facebook news feed
The first sections are free. The deep dives, level expectations and quiz are part of Pro.
On this page
- The problem
- Clarifying questions
- The requirements menu
- Non-functional requirements
- Estimates
- Core entities
- The API
- The high-level design
- What breaks first
- The deep dives
- Entity fields
- Fan-out on write or on read
- Accounts with millions of followers
- Hot posts and the post cache
- Variants
- What each level is expected to show
- The 30-second answer
- Quiz
The problem
A news feed shows each person the newest posts from the accounts they follow. Posts are rare next to feed opens, so the answer turns on when the feed is assembled: on each open, by reading from each followed account, or at post time, by pushing the post id into each follower's ready list. Most accounts have small followings and a few have tens of millions, so the strongest designs push for most accounts, pull for the biggest ones, and then keep a viral post from landing on a single cache node.
Clarifying questions
| Ask | Why it matters | A typical answer |
|---|---|---|
How many users are there, and how often do they open the feed? | The number of feed opens a second decides whether a feed can be assembled on each open or has to be ready before the open. The interviewer gives users, not a request rate, so you derive the rate and say the assumption. | Take 2 billion users. No request rate is given, so derive one: at one open a day each, about 23,000 feed reads a second; at ten opens a day, about 230,000. Alex Xu's walkthrough uses 10 million daily actives instead, which is a much smaller system. |
Is the feed in time order, or ranked? | A ranked feed adds a scoring step, whether it runs on each read or is kept up to date ahead of time, which changes where the work lives. Time order lets the feed be a sorted list of ids. | Newest first. Ranking is out of scope for this round. |
Is there a cap on how many accounts a user follows, or on followers? | A cap on follows bounds a read-time build; a cap on followers bounds a write-time one. Without either, each approach has a tail that breaks it. | No cap in either direction. The biggest accounts have about 90 million followers. Some versions of this question cap friends at 5,000, which makes it a much easier problem. |
How quickly must new posts show up for followers? | If delivery may lag, copying the id into followers' lists can run in the background, and the post request can return as soon as the row is written. | Up to 1 minute of delay is fine. Posting and reading the feed should each answer in under 500 ms. |
Are follows one-way, or mutual friendships? | A mutual friendship is two follow rows written together and is often paired with a cap, as in the book version at 5,000 friends; a one-way follow lets a single account collect tens of millions of followers. | One-way follows. Two-way friendships are out of scope. |
Text only, or do posts carry images and video? | Media changes the post write path (an upload to blob storage, a CDN) but not the feed itself, which carries ids and URLs either way. | Treat posts as text with an optional media URL. Say where media would go and move on. |
Non-functional requirements
| Requirement | What it forces |
|---|---|
Availability over consistency: a post may take up to 1 minute to appear | The post request can return once the row is written and a delivery job is queued; copying the id into followers' lists runs in the background and may lag. Our advice: keep those copies out of the request itself. A follower who reads from a slightly stale cache copy may then see the new item a little late. |
Posting and reading the feed answer in under 500 ms | A feed read has to be a small, fixed number of lookups: one read of a ready list of ids and one batched fetch of those posts, mostly from cache. Walking a follow list of unbounded size on each open doesn't fit, which is the main argument for building feeds ahead of time. |
Supports 2 billion users | Posts, follows and ready feeds are all partitioned by key, and the services in front of them are stateless so they scale by adding instances. A ready feed of about 2 KB per user is about 4 TB in total, which spreads across a cache cluster keyed by user id. |
No cap on how many people a user follows or how many follow them | Neither pure approach survives: a read-time build grows with follows, and a write-time build grows with followers. The design has to treat the two tails differently, pushing for ordinary accounts and pulling for the few with tens of millions of followers. |
Estimates
| Quantity | Working | Result | What it sizes |
|---|---|---|---|
Feed reads a second | 2 billion users × 1 open a day ÷ 86,400 seconds a day ≈ 23,000; at 10 opens a day, ≈ 230,000 | About 23,000 to 230,000 feed reads a second, depending on how often people open the app | The read path. Even the low end is far more than one database can serve by walking follow lists, so the feed service and its stores are read-heavy and horizontally scaled. |
Post lookups a second if feeds are built on open at the follow cap | 5,000 followed accounts × 23,000 feed opens a second = 115 million recent-post lookups a second | About 115 million post lookups a second at the low end of the open rate | Why a read-time build fails at this scale even with a follow cap: the load is multiplied by follows, not by users. |
Ready feed storage | 200 post ids × 10 bytes = 2 KB per user; 2 KB × 2 billion users = 4 TB | About 4 TB for every user's ready feed | The memory of the feed cache cluster. It is a large but ordinary amount, so storage doesn't argue against building feeds ahead of time. |
Feed writes a second for one post from the biggest account, within the freshness target | 90 million followers ÷ 60 seconds = 1.5 million feed writes a second | About 1.5 million list writes a second for one post | The write burst a single celebrity post puts on the fan-out workers and the feed cache, and the reason big accounts are handled differently. |
Fan-out jobs for one celebrity post when a job is split into chunks | 90 million followers ÷ 5,000 followers per chunk = 18,000 chunks | About 18,000 chunks, each a separate job a worker can retry on its own | The granularity of retry when a worker dies mid-fan-out: one chunk is redone, not the whole post. The chunk size is an example, not a target. |
Reads of one hot post if its 90 million followers open within an hour | 90 million followers ÷ 3,600 seconds = 25,000 reads a second of one post id | About 25,000 reads a second landing on a single key | The hot key the post cache has to survive; one cache node holding that one key is the point of failure. |
Core entities
| Entity | What it holds |
|---|---|
User | An account that posts and follows. Carries the flag that says whether its posts are fanned out or pulled. |
Follow | One row per one-way follow, indexed both ways: who a user follows for the read-time merge, and who follows a user for fan-out. |
Post | The content itself, partitioned by post id and indexed by creator and time so a big account's recent posts are one range read. |
Precomputed feed | One ready list per user of about 200 post ids, newest first, held in a cache cluster keyed by user id. |
The API
| Endpoint | Notes |
|---|---|
| Writes the post row and enqueues a delivery job carrying the new id and its author's id, then returns. The response does not wait for fan-out, which is what lets it answer in under 500 ms for an author with 90 million followers. Accept an idempotency key from the client, store it with the post, and return the stored post when a retry repeats the key. |
| The caller comes from the session; |
| The cursor marks the oldest item already returned: its timestamp, with the post id as a tie-breaker so two posts with the same timestamp don't repeat or vanish between pages. The next page is "older than this", which stays stable while new posts arrive at the top. The first page reads the ready list and hydrates the ids from the post cache; once the cursor is older than the ready list reaches, the service builds that page from the post table by creator and time. Cap |
The high-level design
Step 1 of 3
Post, follow and read, end to end
Start with a design that works before it scales. An API gateway fronts two stateless services: the post service writes post rows and follow rows, and the feed service is split out because feed reads are expected to outnumber posts by a wide margin, so the two scale differently. On a feed read, the feed service finds the accounts the user follows, reads each one's latest posts through the index on creator and timestamp, merges them newest first and returns a page. This is the pull model: nothing is written ahead of time, so a user who doesn't open the app costs nothing. The post table is indexed on (creator, created_at), and the follow table on both of its columns.
What breaks first
The deep dives
Fan-out on write or on read. A person who follows a few hundred accounts opens the app. Where does their feed come from, what does that choice cost at 2 billion users, and how much storage does it take?
Accounts with millions of followers. Someone followed by 90 million people publishes. How does that reach their followers within a minute, what happens when a worker dies halfway through, and what does a feed read cost afterward?
Hot posts and the post cache. Something goes viral and, within one second, feeds nationwide want it. What serves it, why don't more cache shards help, and what does a reader see after the author edits it?
Concepts this guide uses
Sources
- Alex Xu, System Design Interview, chapter 12: Design a news feed system (book notes)
- High Scalability: The architecture Twitter uses to deal with 150M active users
- Redis command reference: ZADD (mirror maintained by antirez)
- Redis documentation: Replication
- Amazon SQS developer guide: Visibility timeout
- DEV Community: Redis hot keys, why adding more nodes doesn't always help
- DEV Community: Designing cursor pagination, why offset hits a wall