Design a news feed - the home screen of a social app that shows the posts of the people you follow, newest first, for 100 million readers a day. Each feed is a copy in a cache, filled in the background when somebody posts. The staff-level signal is to push small authors into the feeds and pull celebrities at read time, to page with a cursor that names a post, and to treat every feed as something that can be repaired and rebuilt.
Written by a Staff+ Engineer and an Engineering Manager
12+ years of experience in distributed systems and real-time messaging. Tech lead for large-scale chat applications serving billions of users. Over 12 years of industry experience with 6 years in engineering leadership, building and scaling tech infrastructure teams that deliver end-to-end large-scale distributed systems.
Finished this lesson?
Sign in to save your progress across devices.
Tip: use ← and → to move between lessons.
Was this lesson helpful?
Rehearse this exact question with the author
45-minute mock with a Staff+ engineer who interviews at your target company. Premium members save $20.
What to Design?
Could you answer these today? Tick what you already know.
The happy path is quick to draw: store the post, look up the followers, show their posts sorted by time. The interview is decided by four questions about what happens when an author is huge, a list moves while it is read, a follow changes, or a cache is empty. Each one is a deep dive later on this page, with three designs side by side: the one that sounds right, a better one, and the one that holds.
Swipe to see all columns
| The four questions, and the answer that sounds right | |||
|---|---|---|---|
| # | The interviewer asks | The answer that sounds right | What it misses |
| DD1 | maya has 10 million followers and posts. leo posts one second later. When does leo's post reach alice? | At once: the fanout runs in the background, so nobody waits | The author does not wait. Everyone behind her in the queue does: 2 million inserts take 400 seconds. Do not copy a celebrity's post at all. Keep it once and merge it in when a feed is read |
| DD2 | alice reads page 1. Three new posts arrive. She scrolls. What is on page 2? | ||
The same three habits answer all four:
Do not spend your questions on what a post looks like. Spend them on what the feed promises.
Swipe to see all columns
| Scope Decisions | ||
|---|---|---|
| Question | Agreed answer | Why it matters |
| Which feed? | The home feed only: posts of the accounts you follow, newest first | No ranking model, no ads, no discovery. The hard part is getting posts to feeds, not ordering them |
| How fresh? | A normal author's post may take a few seconds to appear. We fix that at 5 | The post can be copied into feeds in the background, after the author has an answer (Phase 2) |
| What about huge accounts? | An author with millions of followers must not slow down anybody else | |
Out of scope (say this out loud): ranking and recommendations, uploading and serving photos and video, ads, likes and comments, push notifications, more than one region, and editing a post after it is published.
FR1 — Publish a post. Text, with an optional link to a photo or video. The author gets an answer at once.
FR2 — Follow and unfollow. A user follows other users. The follow has a direction: alice follows leo does not mean leo follows alice.
FR3 — Read the home feed. The posts of the accounts the reader follows, newest first.
FR4 — Page through it. The next page continues exactly where the last one ended.
Three words used on this page. A reader is the user who opens a feed. An author is the user who wrote a post. Fanout means copying one new post into the feeds of the author's followers.
The key principle behind all four
Swipe to see all columns
| Five facts and the conclusions they do not supportRead each row as: the fact on the left does not, by itself, imply the claim in the middle. | ||
|---|---|---|
| Fact | Does not imply | Why the gap exists |
| The post is stored | It is in the followers' feeds | 201 means the post row is safe. The fanout runs afterwards |
NFR1 — Fast reads. A feed page answers in under 200 ms for 95% of the requests, at 300,000 reads a second.
NFR2 — Fresh enough. A normal author's post is in the followers' feeds within 5 seconds. The reader may see a slightly old feed: this is eventual consistency, and it is fine here.
NFR3 — Fair. An author with millions of followers never delays the posts of other authors.
NFR4 — Available. Feed reads keep working when a cache node or a worker fails. A lost cached feed is rebuilt. It is never shown as empty.
NFR5 — Follow changes show soon. A follow or unfollow is visible in the feed within 10 seconds.
| Capacity Thought Experiment | |
|---|---|
| Parameter | Value |
| Users | 500 million, 100 million active a day |
| Feed reads | 300,000 a second at peak |
| Posts | 100 million a day: about 1,160 a second on average, 5,000 a second at peak |
| Reads to writes | 60 to 1 |
| Followers | 200 for an average author. 10 million for the largest |
The number that identifies the real system is the gap between an average post and one post of a celebrity:
leo, 200 followers 40 inserts 8 milliseconds
maya, 10,000,000 followers 2,000,000 inserts 400 seconds, if her post is copied like leo's
Reads outnumber writes 60 to 1, so the read must be cheap: that is why feeds are prepared in advance. But preparing is a write for every follower, and one author can have 10 million. The whole design is about getting the first without paying the second. Worker speed, the number of workers and the 20% share of cached feeds are illustrative assumptions.
Interviewer signal: candidates who start with a ranking model have picked a different question. Candidates who say "reads must be one lookup, so I copy posts into feeds, and I must decide what to do when the copies are 10 million" have found this one.
Three ideas shape everything:
Posts and follows are stored in a database. That is the truth, and it is never lost. A reader's feed is a copy: a short list of post IDs in a cache, there to make the read fast. Anything can happen to the copy. It can be late, it can be repaired, it can vanish. The reader still gets a feed, because the copy can always be built again from the truth.
There are two ways to build a feed. Push (fan out on write): when an author posts, copy the post into every follower's feed. Reads are one lookup, writes are one per follower. Pull (fan out on read): copy nothing, and collect the posts of everybody the reader follows when they open the app. Writes are free, reads are expensive. Neither works alone. The design pushes for authors with few followers and pulls for authors with many.
A feed is never still. Posts arrive on top while somebody reads further down, posts leave after an unfollow, and the whole list can be rebuilt between two pages. So nothing may depend on a position in the list. A page is found by naming the last post the reader saw.
The core recognition
This is a read-optimised copy that is kept up to date in the background. Publish, fan out, read, repair: the first writes the truth, the second and fourth fix the copy, the third only reads it.
| Design Decisions | |
|---|---|
| Design choice | Reason |
| Store the post, answer, fan out afterwards | The author waits for one insert, never for the followers |
| The post row and its event in one transaction | A stored post can never lose its fanout |
Two records carry the design: the
poststable and the cached feed. The first is the truth, the second is the copy.
One row per post. It never changes after it is stored.
Swipe to see all columns
| posts | ||
|---|---|---|
| Field | Type | Comment |
post_id | STRING | Key, for example post_7001 |
author_id | STRING | Who wrote it. Indexed together with created_at |
One sorted list per reader, in the cache. It holds post IDs, not posts.
Swipe to see all columns
| feed:alice (a sorted set in the cache) | ||
|---|---|---|
| Part | Value | Comment |
| Key | feed:alice | One per reader who was active in the last 24 hours |
| Member | A post ID, for example post_7001 | Adding the same ID again changes nothing |
| Score | The time of the post | The list is always sorted by it |
Swipe to see all columns
| Supporting records | ||
|---|---|---|
| Record | What it holds | Why it exists |
| Follow | follower_id, followee_id (unique together), created_at. Indexed both ways | By followee_id: who gets a new post. By follower_id: whose posts rebuild a feed |
An outbox is a table of events that still have to be sent. Because the event is stored together with the post, it cannot be lost. A small relay reads the table and publishes each event to the event log.
Swipe to see all columns
| Storage | ||
|---|---|---|
| Store | Data | Consistency |
| Database (PostgreSQL) | Posts, follows, hot authors, the outbox | The source of truth. A post that got 201 is never lost |
| Event log (Kafka) | "A post was published", "a follow changed" | At least once, in order per author. A worker may see an event twice |
| Feed cache (Redis) | Feeds, author timelines, hot follows, post bodies, unfollow filters | A copy. It may be a few seconds late, and it can be rebuilt |
| The calls | |
|---|---|
| Call | What it does |
POST /v1/posts | Publishes a post: content, optional media_url, and the header Idempotency-Key. Answers 201 with post_id and created_at as soon as the post is stored, before any fanout. The same key again returns the same post |
GET /v1/feed?limit=20&cursor= | One page of the reader's home feed, newest first, with next_cursor. No cursor means the top of the feed. |
The reader always comes from the login token, never from a parameter.
An idempotency key is a random value the app creates once per post. If the app sends the same request twice, because it never saw the answer, the server recognises the key and returns the post it already stored.
A cursor is a small token that says where the last page ended. The app does not read it. It sends it back to get the next page.
Present the design as four short stories, each in its own colour on one whiteboard: Phase 1 publish (black), Phase 2 fan out (blue), Phase 3 read (green), Phase 4 follow and unfollow (purple). The number on an arrow is the same number in the animation and in the step table.
Swipe to see all columns
| The four phases | |||
|---|---|---|---|
| Phase | Colour | Story | What it leaves behind |
| Phase 1: Publish a post (FR1) | Black | leo posts. The post and its event are stored together, and leo gets 201 | One post row and one event |
| Phase 2: Fan out | Blue | leo's post is copied into the feeds of his followers. maya's post is not copied: it goes into one list | post_7001 in 40 feeds, post_7002 in one author timeline |
How to read the picture
post_7001 at 12:00:00. maya has 10,000,000 followers and publishes post_7002 at 12:00:05. alice follows both and opens the app at 12:00:10.Each phase below is an interactive walkthrough of the same picture. It plays on its own. You can pause it, step with the arrow keys, and click any component to read its role.
Goal: store the post safely, answer in milliseconds, and make sure the fanout will happen.
/v1/postsPhone app (leo) → Post serviceNothing is written in this step.
Swipe to see all columns
| Phase 1 step by step (black arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | leo taps Post. The app calls the Post service with the text and the idempotency key k_91. | Nothing yet |
| 2 | The Post service stores the post and its event in one transaction. | posts row post_7001 (leo, 12:00:00). outbox row: PostPublished post_7001 |
Why it is built this way
Because the author would wait for their followers. For leo that is 8 milliseconds. For an author with 9,000 followers it is close to half a second, and any slow cache node makes Post feel broken. The request should do the one thing that must not be lost, and nothing else.
Goal: get the post into the feeds of the followers within 5 seconds, without letting one author hold up the others.
PostPublished { post_7001, leo }Event log → Fanout workersNothing is written in this step.
Swipe to see all columns
| Phase 2 step by step (blue arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | The fanout worker of partition 12 reads the event: post_7001 by leo. | Nothing |
| 2 | leo is not a hot author. The worker reads his followers from the database: 200 users, alice among them. | Nothing |
| 3 | For each of the 200, the worker adds post_7001 to the feed, if that feed exists in the cache. 40 feeds exist. It takes 8 ms. | |
Why it is built this way
author:maya until one of her followers opens a feed (DD1).From the freshness target. A post must be in the feeds within 5 seconds, and one worker handles one partition in order. An author with 10,000 followers has about 2,000 cached feeds, which is 0.4 seconds of work: nobody behind them waits long. An author with a million followers would block the partition for 40 seconds. The threshold is a starting value. Lower it and more readers pay for a merge. Raise it and more authors wait in line. Tune it by watching how late posts arrive.
Goal: answer a page with a few cache reads, and never touch the database on the way.
/v1/feed?limit=20Phone app (alice) → Feed serviceNothing is written in this step.
Swipe to see all columns
| Phase 3 step by step (green arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | At 12:00:10 alice opens the app. It asks the Feed service for 20 posts. | Nothing |
| 2 | The Feed service reads the newest 21 entries of feed:alice: one more than a page, to know whether there is a next page. On top: post_7001. | Nothing. The 24 hours of the feed start again |
| 3 | ||
Why it is built this way
The Feed service reads it from the database by its ID and puts it into the cache. This is one indexed lookup for one post, and the next 39 readers of the same post find it in the cache. It is the only time a feed read goes to the database, and it gets rarer the more popular a post is.
Goal: answer the tap at once, and make the feed match the new follow list within 10 seconds.
/v1/users/dave/followPhone app (alice) → Follow serviceNothing is written in this step.
Swipe to see all columns
| Phase 4 step by step (purple arrows) | ||
|---|---|---|
| Step | What happens | Data written |
| 1 | At 12:10:00 alice taps Follow on dave's profile. The app calls the Follow service. | Nothing yet |
| 2 | The Follow service stores the follow and its event in one transaction, and answers 201. | follows row (alice, dave). outbox row: FollowChanged |
Why it is built this way
hotfollow:alice.Because the feed keeps only its newest 500 entries, and most of what is in it is a few days old. Posts of dave from last year would sort far below everything else and be cut off at once. 7 days is enough to make the follow visible. Whoever wants dave's whole history opens his profile, which reads his posts straight from the database.
The deep dives are the four questions from the top of the page. Each one compares three designs for the same scenario: a Bad one, which is usually the answer that sounds right; a Good one, which still leaves a gap; and a Great one, which holds. The answers start covered. Read the scenario, decide what you would build, then click each card.
Swipe to see all columns
| Deep Dive Map | |||
|---|---|---|---|
| Deep Dive | Topic | Scenario | NFR |
Scenario: at 12:00:05 maya publishes post_7002. She has 10,000,000 followers, and 2,000,000 of them have a feed in the cache. One second later leo publishes post_7003. His events are in the same partition of the event log as hers. alice follows both. When does each post reach her?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Fan out on write, for everybody. Every post is copied into the feed of every follower. A read is then one lookup, always.
Where it breaks. maya's post is 2,000,000 inserts. At 5,000 a second the worker of her partition needs 400 seconds. leo's event sits behind hers in the same partition, so his post reaches alice at about 12:06:46. The target was 5 seconds. Every author in that partition waits with him, and that is one author in 64. maya posts ten times a day, and she is not the only celebrity. Running the fanout in the background protects the author from waiting. It does not protect the queue.
The idea. Fan out on read, for everybody. A post is one row. When alice opens the app, the Feed service collects the newest posts of the 200 accounts she follows and merges them.
What it fixes. The celebrity. maya's post is one insert, like anybody's. Nothing waits behind it.
What it still misses. Every read now pays. 300,000 reads a second times 200 followed accounts is 60,000,000 lookups a second, and most of them find nothing new since the last time. A reader who follows 2,000 accounts waits ten times as long. Reads outnumber writes 60 to 1, and this design moved all the work to the larger side.
The key idea. Copy a post only when the copies are few. Keep a celebrity's post once, and let each reader fetch it when they read.
How it works
author:maya. The partition moves on at once.author:maya.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
PostPublished { post_7002, maya }Event log (partition 12) → Fanout workersSELECT follower_id FROM follows WHERE followee_id = mayaFanout workers → Database10,000,000 usersNothing is written in this step.
Swipe to see all columns
| DD1 side by side | |||
|---|---|---|---|
| Bad: push everything | Good: pull everything | Great: push small, pull big | |
| Cache inserts for maya's post | 2,000,000 | 0 | 1 |
| leo's post reaches alice after | About 400 s | Her next read | About 1 s |
Check it against the scenario
| maya at 12:00:05, leo at 12:00:06 | |
|---|---|
| Situation | Outcome with the Great option |
| maya's post | One insert into author:maya. Every follower sees it at their next read, at most a second after she posted |
| leo's post | Read from the partition right behind hers. In alice's feed at about 12:00:07 |
| alice opens the app | One range on feed:alice, one on author:maya, merged: post_7003, post_7002, then the rest |
| A reader who follows 8 hot authors | |
The trade-off to name. Two paths instead of one, and a threshold somebody has to watch. A read is no longer a single lookup for readers who follow hot authors. And there is a list of hot authors that workers and the Feed service must agree on, give or take 30 seconds.
It makes her post faster and everybody else slower. 2,000,000 inserts on 64 workers take about 6 seconds, and during those 6 seconds no worker fans out anything else: every post in the system is late. It also leaves the waste: 2,000,000 copies of one ID, for readers who can fetch it once. Splitting work helps when the work is needed. Here the better answer is not to do it.
"Reads outnumber writes 60 to 1, so I precompute: each active user has a cached feed of post IDs, and a fanout worker inserts a new post into the followers' feeds after the publish is acknowledged. That breaks for an author with 10 million followers: one post becomes millions of inserts and blocks the queue for everyone behind it. Pulling everything at read time fixes the write and makes every read hundreds of lookups. So I split by follower count. Below a threshold I push. Above it I write the post once, to a per-author timeline, and the feed service merges the timelines of the few hot authors a reader follows into their cached feed at read time. Both lists are sorted, so the merge is cheap. The threshold lives in a table and is tuned from fanout lag."
Follow-up: "What is the threshold?" — The mechanism matters more than the number. I start around ten thousand followers, because that is well under a second of work for one worker, and I move it by watching fanout lag and merge latency.
Scenario: at 12:00:10 alice reads page 1: 20 posts. The last one is post_7020 from 11:58:07. post_7019 was published in the same second. While she reads, 3 new posts land on top of her feed. At 12:00:40 she scrolls down. What is on page 2?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Paging by position, the way a search result does it: offset=20&limit=20.
Where it breaks. Three new posts arrived, so every post moved down three places. Positions 21 to 23 of the list as it is now are the posts that were 18, 19 and 20 a moment ago. alice sees them twice. It also fails the other way round: if 3 posts are removed after an unfollow, everything moves up, and 3 posts are never shown on any page. A position means nothing in a list that moves.
The idea. Page 1 returns next_cursor = 11:58:07. Page 2 asks for posts older than that.
What it fixes. The moving list. New posts on top do not change which posts are older than 11:58:07. No repeats.
What it still misses. A tie. post_7019 is from 11:58:07 too. It is not older than the cursor, so it is skipped, and no later page will show it. Change the rule to "11:58:07 or older" and post_7020 is shown twice instead. With 5,000 posts a second, two posts in the same second are normal, also inside one feed. A time alone cannot say which of them was the last one seen.
The key idea. Name the exact post where the page ended. Then "the next one" has one meaning, whatever happens to the list.
How it works
next_cursor = (11:58:07, post_7020), packed into a token the app does not read.post_7019 has the same time and a smaller ID. It is the first post of page 2.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
/v1/feed?offset=0&limit=20Phone app (alice) → Feed service → Feed cachepositions 1 to 20. The last one is post_7020Nothing is written in this step.
Swipe to see all columns
| DD2 side by side | |||
|---|---|---|---|
| Bad: a position | Good: a time | Great: a time and a post | |
| What the cursor says | Skip 20 | Older than 11:58:07 | Older than post_7020 of 11:58:07 |
| 3 new posts arrive | 3 posts shown twice | Fine | Fine |
Check it against the scenario
| Page 1 at 12:00:10, page 2 at 12:00:40 | |
|---|---|
| Situation | Outcome with the Great option |
| Three new posts land on top | Page 2 is not affected. alice sees them when she pulls down to refresh, which is a read without a cursor |
| post_7019 and post_7020 share a second | post_7020 ends page 1, post_7019 starts page 2 |
| bob's posts are removed between the pages | Page 2 simply does not contain them. Nothing else moves |
| A post of 11:57:30 is fanned out late, at 12:00:20 | It is older than the cursor, so it appears on page 2, in its place |
| alice reaches entry 500 | |
The trade-off to name. No page numbers and no "jump to page 50": a cursor only knows "next". For an endless scroll that is no loss. And the cursor is tied to one order. If the feed is ever ranked instead of sorted by time, the cursor needs the rank and a snapshot of the list.
So the server can change it. Today it holds a time and a post ID. Tomorrow it may hold a version number or a hint for the database path. If the app only passes the token back, none of that needs a new release of the app. It also keeps clients from building their own cursors and asking for things the server never promised.
"A feed changes while it is being read, so I do not page by offset. New posts on top shift every position, and the reader sees the tail of page one again. I use a cursor that encodes the creation time and the ID of the last post returned, as an opaque token. The next request reads entries strictly older than that pair: an earlier time, or the same time and a smaller ID. The ID breaks ties, so two posts from the same second are neither skipped nor repeated. The same bound is applied to the hot-author timelines that I merge in. And because the cursor refers to a post and not to a position in the cache, it stays valid when the cached feed is evicted and rebuilt between two pages."
Follow-up: "How does the reader see the new posts?" — Pull to refresh is a request without a cursor. It starts from the top again.
Scenario: at 12:10:00 alice unfollows bob and follows dave. Three of bob's posts are in her cached feed, and his post_7050 of 12:09:59 is still being fanned out. dave has published 5 posts in the last 7 days. What does her feed show a few seconds later?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Delete the row (alice, bob), insert the row (alice, dave). Future posts follow the new list.
Where it breaks. The past is still in the cache. bob's 3 posts are copies in feed:alice, and they stay until 500 newer posts have pushed them out, which takes days. Nothing of dave is there: alice followed him and sees no change until he posts again. And at 12:10:02 the fanout of bob's post_7050, which had read his follower list before the unfollow, drops one more of his posts into her feed.
The idea. After changing the row, the Follow service builds feed:alice again from the database: the recent posts of all 200 accounts she follows.
What it fixes. The feed is right when the request returns. bob is gone, dave is in.
What it still misses. The cost. The follow button waits about 80 ms longer, to change one author out of 200. With 2,000 follow changes a second, that is 400,000 author lookups a second on the database. A new user who follows 30 suggested accounts in a minute triggers 30 full rebuilds. And bob's post_7050 still lands at 12:10:02, after the rebuild.
The key idea. Three jobs with three speeds. Store the change now. Hide what must not be seen at the next read. Fix the copy when a worker gets to it.
How it works
unfollowed:alice, for 10 minutes.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure. A dashed box belongs to one design only.
/v1/users/bob/followPhone app (alice) → Follow service → Database204/v1/users/dave/followPhone app (alice) → Follow service → Database201(alice, bob)delete(alice, dave)new rowSwipe to see all columns
| DD3 side by side | |||
|---|---|---|---|
| Bad: only the rows | Good: rebuild in the request | Great: hide, then repair | |
| bob's old posts | Stay for days | Gone when the request returns | Hidden at once, removed in seconds |
| dave's recent posts | Missing until he posts | There when the request returns | There after about 2 s |
Check it against the scenario
| 12:10:00: unfollow bob, follow dave | |
|---|---|
| Situation | Outcome with the Great option |
| alice refreshes at 12:10:01 | bob's 3 posts are still in the cached feed, and the Feed service leaves them out. She does not see them |
| The repair job runs | bob's 3 posts are removed, dave's 5 are added, at about 12:10:02 |
| post_7050 lands at 12:10:02 | The filter hides it. The second removal, a minute later, takes it out |
| alice follows bob again at 12:15 | bob is taken off the filter, and the follow event adds his recent posts again |
| The follow event is delivered twice | |
The trade-off to name. For a few seconds the feed and the follow list do not agree: dave's posts are missing for about 2 seconds. That is what NFR5 allows. And the read path now does one more small lookup per page, the filter.
Because of posts that are on their way. A fanout that read bob's followers just before the unfollow can still write into alice's feed afterwards, and if the workers are behind, that can be many seconds later. The filter is the guard for that window, and 10 minutes is far longer than any fanout should ever lag. It costs almost nothing: one small set per reader who unfollowed somebody recently.
"A follow change has to fix a cached feed, not only a row. I do not do that in the request. The follow service commits the row with an outbox event and returns. A repair worker consumes the event: for a new follow it backfills the followed author's posts of the last seven days, capped at 500, into the follower's feed. For an unfollow it removes that author's recent post IDs. Until that has happened, the feed service filters at read time against a short-lived set of recently unfollowed authors, which also covers a fanout that was already in flight. Hot authors need no repair at all, because their posts are merged at read time. All of these writes are idempotent, so a redelivered event is harmless."
Follow-up: "Do you remove every old post on unfollow?" — Only what can still be in the cached feed: the recent ones. Anything older has already aged out of the 500 entries.
Scenario: erin has not opened the app for 3 weeks. Her feed left the cache long ago. At 12:20:00 she opens the app. In the same second kai, whom she follows, publishes post_7100. What does she see, and is kai's post in it?
Three ways to build it. Decide what you would do first. The answers are covered: click a card to reveal it.
The idea. Every post is copied into the feed of every follower, active or not. The cache is the feed, so there is never a miss.
Where it breaks. 500 million feeds of 25 KB are 12.5 TB of cache, and 80% of it is for people who do not come today. Every post costs 200 inserts instead of 40. And the cache is now the only place where a feed exists. When a cache node dies, 1,000,000 feeds are gone. Those readers open the app and see an empty screen, and nothing in the system knows how to fill it again.
The idea. A feed lives for 24 hours after the reader's last visit. A follower with no feed gets no insert. When a reader comes back and the feed is missing, the Feed service reads the newest 500 posts of the accounts they follow from the database, writes the feed and answers.
What it fixes. The size and the loss. 2.5 TB instead of 12.5, 40 inserts instead of 200, and a missing feed is rebuilt in about 80 ms.
What it still misses. A gap in time. kai's post_7100 was stored just after erin's rebuild query had run. His fanout looked for feed:erin just before the Feed service wrote it, found nothing, and skipped her. Now the post is in neither: not in the query result, not added by the fanout. erin never sees it. There is no error and no retry. It is simply missing.
The key idea. Close the gap by changing the order. Make the feed exist before reading the database, so the fanout and the rebuild overlap instead of missing each other.
How it works
feed:erin with one placeholder entry. From this moment the feed exists.post_7100 lands.The three designs side by side, in one picture:
Play the three options. Switch between them with the tabs at the top of the figure.
add the post to feed:<follower>Fanout workers → Feed cacheleo's postnew rowSwipe to see all columns
| DD4 side by side | |||
|---|---|---|---|
| Bad: every feed, for ever | Good: rebuild on a miss | Great: mark, then fill | |
| Feeds in the cache | 500 million, 12.5 TB | 100 million, 2.5 TB | 100 million, 2.5 TB |
| Inserts for leo's post | 200 | 40 | 40 |
Check it against the scenario
| erin opens the app at 12:20:00 | |
|---|---|
| Situation | Outcome with the Great option |
| kai's post is stored before the rebuild query | The query finds it. If his fanout also adds it, it is still there once |
| kai's post is stored after the rebuild query | The feed already exists, so his fanout adds it |
| erin follows 3 hot authors | Their timelines are merged in as on every read. Nothing about them is rebuilt |
| The Feed service dies between the mark and the fill | The feed holds the placeholder and a few new posts. The next read sees a feed that was never filled and fills it |
| A node and its replica are both lost | |
The trade-off to name. The first page after a long break is slow: 100 ms instead of 10. A reader who comes every two days pays it every time, so the 24 hours are worth tuning. And the rebuild reads the main database, which is why it needs a limit.
Only with care. A replica is a little behind the main database. A post that was stored a moment before the mark may not be on the replica yet, and its fanout ran before the feed existed: the same hole, opened again. Either read the main database, or wait until the replica has caught up to the moment of the mark before running the query. The order "mark, then read" only helps if the read really sees everything stored before the mark.
"The feed cache is a derived view, so I only keep it for active users and I fan out only into feeds that exist. That cuts the cache and the fanout work to about a fifth. When a returning user has no cached feed, the feed service rebuilds it from the follow table and the post index. The subtle part is a post published during the rebuild: the query has already run, and the fanout skipped the user because no feed existed. So I create the feed key with a placeholder first, then run the query and add the results. Fanout and rebuild then overlap, and because inserts into the sorted set are idempotent, the overlap is harmless. For node loss I rely on replicas first, and I rate-limit rebuilds so a wave of misses cannot starve the write path."
Follow-up: "What does the user see during the rebuild?" — The page built from the query result, merged with the hot authors as usual. Never an empty feed.
| If the interviewer keeps going | |
|---|---|
| Question | Short answer |
| Moderation removes a post | Set is_visible to false on the post row and drop its cached body. The Feed service leaves out a post that is not visible. No feed is rewritten |
| An author crosses the threshold | Their old posts stay in the feeds and age out. New posts go to their timeline. The merge drops a post it meets twice |
| An author falls back under it | Use two numbers: hot above 10,000, normal again below 8,000. Keep reading their timeline for 7 days after the change |
| Ten posts of one author in a row | A spread rule in the merge: at most two in a row from one author, then somebody else |
For the patterns behind this page, see Fan-Out and Feed Generation, Scaling Reads and the Caching building block. The Notification System question meets the same celebrity from the other side: there every follower must be told, so the work is split instead of skipped.
The question is a feed sorted by time. A candidate who opens with a ranking model spends the interview on a part that was out of scope.
The author then waits for their followers, and a slow cache node makes posting fail. Store the post, answer, and fan out from an event.
Millions of inserts block the queue for everybody behind them. Keep the post once and merge it in at read time.
It removes the celebrity problem and makes every read hundreds of lookups. Reads are 60 times more frequent than writes.
New posts move every position, so the reader sees posts twice. Page with a cursor that names the last post.
Two posts from the same second cannot be told apart, so one is skipped or shown twice. Add the post ID.
When a node is lost, the feeds on it are gone and the reader sees an empty screen. The cache is a copy that can be rebuilt from posts and follows.
Deleting a follow row removes nothing from a cached feed, and adding one puts nothing in. Hide at read time and repair in the background.
The shape of a strong answer: store the post with its event and answer at once. Copy it into the cached feeds of the followers in the background, but only into feeds that exist, and only when the author is small. Keep a celebrity's posts in one list and merge that list in when a feed is read. Page with a cursor that names a post. After a follow change, hide first and repair second. And when a feed is missing, make it exist, then fill it.
The feed is a copy
Posts and follows are the truth. A cached feed can be late, repaired or lost, and the reader still gets a feed, because it can always be built again.
Push the small, pull the big
leo's post is 40 inserts. maya's would be 2,000,000. One threshold decides which path a post takes, and one merge at read time brings them together.
Every cache write is safe to repeat
Adding a post that is already there changes nothing. So an event may arrive twice, a repair may overlap a fanout, and a rebuild may overlap both.
| What the interviewer expects at each level | |
|---|---|
| Level | A strong answer shows |
| Junior | Explains what a feed is and why it is prepared in advance. Knows the difference between fanout on write and fanout on read, and why one database query per read does not survive 300,000 reads a second |
| Mid-level | Sees that fanout on write breaks for very large accounts and proposes the hybrid with a threshold. Explains cursor paging and why offsets fail. Explains how a lost feed is rebuilt |
| Senior | Drives the hybrid with confidence: where the threshold lives, how the merge works, what each side costs. Covers the failure cases unasked: a lost cache, workers that fall behind, follow and unfollow repair, and what to monitor to see them first |
These aren't toy problems — they reflect real infrastructure challenges at companies that serve a feed to hundreds of millions of people. If you want to walk through this design in a mock interview or get feedback on your own approach, book a session with us. We're here to help you sharpen your thinking, build your confidence, and land your next offer.
Pick an answer for each question, and you see at once whether it is right. Show explanation opens the answer, why it is right, and why each of the other options fails.
Question 1 of 10
leo taps Post. When may the Post service answer 201 Created?
Question 2 of 10
leo has 200 followers. 40 of them have a feed in the cache. How many cache inserts does his post cause?
Question 3 of 10
maya has 10 million followers and publishes a post. What does the fanout worker do?
Question 4 of 10
Why not skip the fanout and collect the posts when a feed is read, for every author?
Question 5 of 10
alice read page 1 with offset 0. Three new posts arrive on top. She asks for offset 20. What goes wrong?
Question 6 of 10
The cursor is only the time of the last post: 11:58:07. post_7019 was published in the same second. With the rule "older than the cursor", what happens?
Question 7 of 10
alice unfollows bob. What keeps his posts out of her feed at her very next read?
Question 8 of 10
alice follows maya, a hot author. What does the repair job copy into alice's feed?
Question 9 of 10
erin's feed is missing. Why does the Feed service create it with a placeholder before it reads the database?
Question 10 of 10
A cached feed holds 500 entries of 50 bytes. 100 million readers have one. How large is the feed cache?
| Posts 21 to 40: skip 20, take 20 |
| The list moved by three, so three posts show twice. A cursor must name the last post she saw, by time and ID. A time alone breaks when two posts share a second |
| DD3 | alice unfollows bob. When are his posts gone from her feed? | At once: we delete the follow row | Her feed is a copy, and his posts are in it. One more is on its way. Hide him when the feed is read, and remove the copies in the background |
| DD4 | erin comes back after three weeks. Her feed is not in the cache. What does she see? | We rebuild it from the database on a miss | A post published during the rebuild falls between the query and the write, and is lost without an error. Make the feed exist first, then fill it |
| One post must not become millions of writes (DD1) |
| May a feed be lost? | The cached feed may be lost. The reader must still get a feed | The cache is a copy. Posts and follows can rebuild it (DD4) |
| Follow and unfollow? | Visible within a few seconds. We fix that at 10 | A cached feed must be repaired, not only the follow row (DD3) |
| Paging? | Endless scroll, 20 posts a page, nothing twice and nothing skipped | The list changes while it is read (DD2) |
| alice follows maya | maya's post is copied into alice's feed | A hot author's posts are kept once and merged in at read time |
| The follow row is deleted | The posts are gone from the feed | The feed holds copies, and one more copy may be on its way |
| Page 1 was posts 1 to 20 | Page 2 is posts 21 to 40 | New posts arrived on top, so every position moved |
| The feed is not in the cache | The reader has no feed | The cache is a copy. The database can build it again |
| The newest 500 post IDs, about 50 bytes each: 25 KB |
| All cached feeds | 100 million feeds x 25 KB = 2.5 TB |
| Fanout | 40 inserts for an average post (only followers with a cached feed): 200,000 a second at peak |
| One fanout worker | 5,000 inserts a second. 64 workers: 320,000 a second |
| Post storage | About 1 KB per post: 100 GB a day |
| One cached feed per reader, only post IDs |
| A read is one small range, and a feed is 25 KB |
| Copy only into feeds that exist | 80% of the followers will not open the app today |
| Hot authors are not copied | One post is one insert, whatever the follower count |
| Merge hot authors at read time | A reader follows only a few of them, and both lists are already sorted |
| A cursor of time and post ID | It names a post, so it survives new posts, removed posts and a rebuilt feed |
| Hide at read, repair in the background | An unfollow shows at once, and the follow button never waits |
content |
TEXT |
| The text of the post |
media_url | STRING | A link to a photo or video, or empty |
created_at | TIMESTAMP | The time of the post. Orders every feed |
is_visible | BOOL | False when moderation has removed the post |
| Size | The newest 500 entries | Older entries are cut off after every insert |
author_id, follower_count, since |
| The list of authors with more than 10,000 followers. Their posts are not copied |
| Author timeline | author:maya in the cache: the newest 500 post IDs of one hot author | What a feed read merges in for a hot author |
| Hot follows | hotfollow:alice in the cache: the hot authors alice follows | Tells a feed read which author timelines to merge |
| Post body | post:7001 in the cache: the text of a post by ID | A page of 20 IDs becomes 20 posts without touching the database |
| Unfollow filter | unfollowed:alice in the cache: authors she unfollowed in the last 10 minutes | Hides their posts until the copies are removed |
| Outbox | event_id, topic, payload, published | An event written in the same transaction as the row it is about |
next_cursorPOST /v1/users/{userId}/follow | Follows a user. Answers 201 when the row is stored, 409 if it already exists. The feed is repaired in the background |
DELETE /v1/users/{userId}/follow | Unfollows a user. Answers 204, or 404 if there was no follow. The posts are hidden at once and removed in the background |
GET /v1/users/{userId}/posts?limit=20&cursor= | One user's own posts, newest first, straight from the database index |
| Phase 3: Read the feed (FR3, FR4) |
| Green |
| alice opens the app: her feed and maya's timeline are merged into page 1, then page 2 |
| Nothing. A read writes no data |
| Phase 4: Follow and unfollow (FR2) | Purple | alice follows dave and unfollows bob. Her feed is repaired in the background | A feed that matches her new follow list |
| The Post service answers 201 Created with post_7001, about 20 ms after the tap. No follower has the post yet. |
| Nothing |
| 4 | The outbox relay publishes the event to the event log. The key is the author, so leo's events stay in order: partition 12. | One event in partition 12. The outbox row is marked published |
| 5 | leo's phone never saw the 201 and sends the request again with k_91. The key is known: the answer is the stored post_7001. | Nothing. Still one post |
feed:alice| 4 | The worker commits its position in the partition, about 1 second after the post. | The position moves past leo's event |
| 5 | At 12:00:05 maya publishes post_7002. The same worker reads her event. | Nothing |
| 6 | maya is a hot author: 10,000,000 followers. The worker adds the post to her own timeline. No follower's feed is touched. | author:maya: post_7002 added |
hotfollow:aliceauthor:maya| Nothing |
| 4 | It merges the two lists, which are both sorted by time, and keeps the newest 20: post_7002 first, post_7001 second. Then it reads the 20 post bodies. | Nothing |
| 5 | The app gets 20 posts and a cursor that names the last one: post_7020 from 11:58:07. About 10 ms. | Nothing |
| 6 | alice scrolls. The app sends the cursor. Both lists are read again, older than the cursor. Page 2 starts with post_7019. | Nothing |
| The outbox relay publishes the event: alice now follows dave. |
| One event |
| 4 | A worker reads the event. dave is not a hot author, so it reads his posts of the last 7 days from the database: 5 posts. | Nothing |
| 5 | The worker adds the 5 posts to feed:alice, each at its own time. About 2 seconds after the tap. | feed:alice: 5 posts of dave added |
| 6 | alice unfollows bob. The row is deleted and the answer is 204. bob is put on her unfollow filter, so the Feed service hides his posts at once. A worker then removes his 3 posts from her feed. | follows row (alice, bob) deleted. unfollowed:alice: bob. feed:alice: 3 posts removed |
| DD1 |
| An author with 10 million followers posts |
| A celebrity post, and a small post one second behind it |
| NFR1, NFR2, NFR3 — Fast, fresh, fair |
| DD2 | New posts arrive while alice scrolls | Page 1, three new posts, page 2 | FR4 — Nothing twice, nothing skipped |
| DD3 | alice unfollows bob and follows dave | Copies in a cached feed, and one more on its way | NFR5 — Follow changes show soon |
| DD4 | erin's feed is not in the cache | A reader returns while an author she follows posts | NFR4 — Available |
| 1 range |
| 200 lookups |
| 1 range, plus 1 per hot author followed |
| At 300,000 reads a second | 300,000 ranges | 60,000,000 lookups | Well under a million small ranges |
| Who pays for a celebrity | Every author behind her | Every reader, always | Her followers, a little, on each read |
| 9 small ranges, sent together in one round trip |
| An author grows from 9,000 to 11,000 followers | Their old posts stay in the feeds and age out. New posts go to their timeline. The merge drops a post it sees twice |
| Does not matter |
| One is skipped, or one is shown twice |
| Fine |
| The feed is rebuilt between pages | Positions mean something else | Fine | Fine |
| Jump straight to page 50 | Possible | Not possible | Not possible |
The cached feed ends. next_cursor is empty, or older pages are read from the database with the same cursor |
hotfollow:alice| The follow button waits for |
| One row |
| One row and a rebuild over 200 authors |
| One row |
| Database work per change | None | 200 author lookups | 1 author lookup |
| A post of bob that is on its way | Lands in the feed | Lands in the feed | Hidden, then removed |
| Adding the same 5 posts again changes nothing |
| Writing the filter fails | bob's posts show for the second or two until the repair job has removed them. The follow row is still right |
| An old feed, still there |
| Rebuilt in 80 ms |
| Rebuilt in about 100 ms |
| A post published during the rebuild | Does not apply | Can be lost for ever | Lands in the marked feed |
| A cache node dies | 1,000,000 empty feeds | 1,000,000 rebuilds hit the database | The replica takes over. Rebuilds are limited |
| Readers of those feeds get a rebuild, at most 2,000 a second. The others wait a moment for their turn. Nobody gets an empty feed |
| What do you monitor? |
| Fanout lag, from stored to in the feed. Consumer lag over 30 seconds. Cache misses, merge time, and the backlog of repairs |
| A ranked feed instead of newest first | The cached feed becomes the list of candidates, and a ranker orders the top of it. That is another interview. Say so and stay with fanout |
| Photos and video | The feed carries IDs and links. Upload, processing and delivery are their own system: see the [Instagram](/learn/system-design/questions/instagram) page |
| Warming | Before the morning peak, rebuild the feeds of readers who usually come at that hour |
| The outbox relay falls behind | Posts are safe and feeds are late. Alert on the age of the oldest unpublished row |
| Why is the log keyed by author? | So one author's posts are fanned out in order. The price is that a slow event holds up its partition, which is why hot authors are not pushed |
| A reader who follows 5,000 accounts | Push does not care: the cost is per author, not per reader. The rebuild is the expensive part, so it stays capped at the newest 500 posts |