Skip to content

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
Asked at
Practice this question

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

AskWhy it mattersA 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.

The requirements menu

RequirementScopeWhy

Create a post

Core

The write path. Where the post row lands and what else happens when it's written decides the whole design.

Follow other users

Core

The social graph the feed is built from. It needs lookups in both directions: who a user follows, and who follows a user.

See a feed of posts from followed users, newest first

Core

The read path, and the request expected to dominate traffic. Everything else exists to make this one request fast.

Page back through the feed

Core

Users scroll. The page boundary has to hold still while new posts arrive, which rules out offset paging.

Unfollow an account and have its posts drop out of the feed

Optional

Worth taking if asked: it's free in a read-time build and costs a filter or a list rewrite in a precomputed one. Leave it unless the interviewer raises it, because the core follow path is what the round tests.

Edit or delete posts after publishing

Optional

Take it as a yes and say little: it decides whether cached posts need invalidation, which the hot-post discussion depends on. The feed stores ids, so an edit doesn't touch it.

Posts with images and video

Optional

Media goes to blob storage behind a CDN and the post row keeps a URL. Name that and park it, since it doesn't change the fan-out question.

Live push of new posts into a feed that is already open

Out of scope

The freshness target allows a minute, so a refresh on open or on pull-down meets it. A live push adds a connection per open client for a requirement nobody asked for.

Likes and comments

Out of scope

Counters and threads are their own system with their own hot keys. Mention that a like count would ride along with the hydrated post and stop there.

Private posts and restricted visibility

Out of scope

Visibility can be applied during fan-out or rechecked per viewer at read time, and either way it complicates the precomputed list. Out for this round.

Ranking the feed

Out of scope

A ranked feed replaces the sorted list with a scoring stage over candidates. Say that the precomputed ids would become the candidate set and leave it.

Two-way friendships

Out of scope

A friendship is a pair of follow rows written together. The one-way model is the harder case because it allows enormous follower counts.

Non-functional requirements

RequirementWhat 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

QuantityWorkingResultWhat 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

EntityWhat 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

EndpointNotes

POST /posts { content } → { post_id }

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.

PUT /users/{id}/follow

The caller comes from the session; {id} is the account to follow. Writes one row to the follow table, visible through both indexes, and bumps the followed account's follower count. Whether the new follow's recent posts are backfilled into the caller's ready feed is a product choice; say which you'd pick.

GET /feed?page_size={n}&cursor={timestamp} → { items, next_cursor }

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 page_size on the server.

The high-level design

followsClientAPI gatewayPost serviceFeed servicePost tableFollow table

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.

jobprependmissClientAPI gatewayPost serviceFeed servicePost tableQueueWorkersFollow tableFeed listsPost cache
The whole design

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?

A concept that helps here: Fan-out on write and on readA concept that helps here: Queues and logsA concept that helps here: Sorted sets and leaderboards

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?

A concept that helps here: Fan-out on write and on readA concept that helps here: Delivery semantics: at most, at least, exactly onceA concept that helps here: Idempotency keys

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?

A concept that helps here: Hot keys and hot partitionsA concept that helps here: Replication and read replicasA concept that helps here: Cache invalidation and TTLs

Concepts this guide uses

Sources

  1. Alex Xu, System Design Interview, chapter 12: Design a news feed system (book notes)
  2. High Scalability: The architecture Twitter uses to deal with 150M active users
  3. Redis command reference: ZADD (mirror maintained by antirez)
  4. Redis documentation: Replication
  5. Amazon SQS developer guide: Visibility timeout
  6. DEV Community: Redis hot keys, why adding more nodes doesn't always help
  7. DEV Community: Designing cursor pagination, why offset hits a wall
Sign in to report a mistake