Design a news feed or timeline
Build a home timeline for 200 million daily users. The core decision is when to do the work, at post time or at read time, and what to do about accounts with millions of followers.
The problem
A home timeline looks like a single query: show me the newest posts from everyone I follow. Early Twitter ran it almost that way, looking up the followed accounts and merging their recent tweets on every page load. As the service grew, those reads became the bottleneck. Twitter moved to precomputing each user’s timeline, inserting every new tweet into the cached timelines of the author’s followers. In talks around 2012 and 2013 its engineers described the result: about 4,600 tweets a second on average became several hundred thousand timeline writes a second, in exchange for cheap reads at around 300,000 timeline requests a second.
Then the other end broke. A user with tens of millions of followers turns one tweet into tens of millions of writes. Twitter’s answer was a hybrid, in which the largest accounts are handled at read time.
That history is the interview question. The interviewer wants to see you find the read-heavy shape of the workload, choose where to do the work, and then notice for yourself that the choice fails for the biggest accounts.
Requirements
Functional
- A user publishes a post with text and optional images or video.
- A user follows and unfollows other users.
- A user reads a home feed of recent posts from the accounts they follow, newest first.
- The feed scrolls: older posts load page by page.
Out of scope unless asked: ranking by relevance (treated as an extension below), comments and likes beyond a counter, search, adverts, notifications.
Non-functional
- Feed read latency: p99 under 200 ms at the server.
- Freshness: a post reaches most followers’ feeds within 5 seconds. Brief staleness is acceptable, so this is an eventually consistent system.
- Availability: 99.99% for reading the feed. A slightly stale feed is far better than an error.
- Durability: an acknowledged post is never lost.
- Scale: 200 million daily active users, most following a few hundred accounts, a few accounts with over 100 million followers.
Capacity estimates
| Quantity | Working | Result |
|---|---|---|
| Feed reads | 200M users x 10 opens a day / 86,400 | about 23,000 per second, 70,000 at peak |
| Posts | 40M a day / 86,400 | about 460 per second |
| Fan-out inserts | 460 posts x 200 followers on average | about 92,000 per second |
| Feed cache | 200M users x 500 ids, about 10 KB each with overhead | about 2 TB of memory |
| Post storage | 40M a day x 1 KB | 40 GB a day, about 15 TB a year |
| One celebrity post | 100M followers / 92,000 inserts per second | about 18 minutes of total fan-out capacity |
Three conclusions follow. Requests are read-heavy at about 50 feed reads per post, so the read path should be a cache lookup. The write path multiplies: 460 posts a second become 92,000 inserts, so fan-out must be asynchronous and horizontally scalable. And the average hides a tail that a single design cannot serve. An account with 100 million followers needs different treatment from one with 200.
API
POST /v1/posts
Authorization: Bearer <token>
Idempotency-Key: 5b1e...
{ "text": "Shipping day", "media_ids": ["m_91"] }
201 Created
{ "post_id": "1790312345678901248", "created_at": "2026-03-01T09:30:00Z" }
GET /v1/feed?limit=20&cursor=eyJvbGRlcl90aGFuIjoiMTc5MC4uLiJ9
200 OK
{ "items": [ { "post_id": "...", "author": { "id": "...", "name": "..." }, "text": "...", "media": [ ... ] } ],
"next_cursor": "eyJvbGRlcl90aGFuIjoiMTc4OS4uLiJ9" }
POST /v1/media returns a pre-signed URL, and the client uploads the file directly to object storage. Following is PUT and DELETE on /v1/users/{id}/followers/me. The cursor is opaque to the client. Inside, it names the oldest post already shown.
Data model
| Store | Key | Contents |
|---|---|---|
| Posts | post_id |
author, text, media ids, created time |
| Posts by author | author_id, then post_id descending |
recent posts for one author |
| Follow graph | follower_id, followee_id, and the reverse |
who I follow, who follows me |
| Feed cache | user_id |
up to 500 post ids, newest first |
Post ids are 64-bit and time-ordered, in the style of Twitter’s Snowflake (unique ids), so sorting by id sorts by time and no second index is needed.
The follow graph is stored in both directions because the two paths ask opposite questions. Fan-out asks “who follows this author?”. The feed service asks “whom does this reader follow?”. Each direction is a simple range read in a wide-column or sharded relational store.
The feed cache is a Redis sorted set per user. The member is the post id and the score is the creation time in milliseconds. Do not use the 64-bit id as the score: sorted set scores are double-precision floats, which hold integers exactly only up to 253.
High-level architecture
Publishing. The gateway routes POST /v1/posts to the post service. It writes the post to the post store, fills the post cache, and publishes a “post created” event to a Kafka topic. It responds as soon as the post is durable. Fan-out happens afterwards.
Fan-out. Workers consume the event, page through the author’s follower ids from the graph store, and add the post id to each follower’s sorted set, trimming it to 500. See fan-out for the general pattern.
Reading. The gateway routes GET /v1/feed to the feed service. It reads a page of post ids from the user’s feed cache, looks up which celebrity accounts the user follows, and asks the post service for those accounts’ recent posts. It merges the lists by time and calls the post service once more to hydrate ids into full posts with a batch get. Deleted posts and blocked authors are dropped at this step.
Media. Images and video never pass through the application servers. The client uploads to object storage with a pre-signed URL, workers produce resized versions, and the app loads them through a CDN. A post holds media ids only.
Deep dives
Fan-out on write, on read, and the hybrid
Fan-out on read (pull). Store each post once. At read time, fetch recent posts from every followed account and merge. Writes are trivial. Reads are expensive: a user following 500 accounts needs up to 500 lookups, or a scatter-gather across shards, on each of 23,000 requests a second. Most of that work is thrown away, because the result is the same as it was a minute ago.
Fan-out on write (push). Do the merge once, when the post is created. Each follower’s feed is already assembled, and a read is one cache lookup. The cost moves to the write path, in proportion to the follower count, and it is spent even for followers who never open the app.
Where push breaks. At 200 followers a post costs 200 inserts. At 100 million followers it costs 100 million, which is about 18 minutes of this system’s entire fan-out capacity. During that time every other post is late.
Step 1 of 8
Two people post at the same moment. One has 300 followers. The other is a celebrity with 100 million. Both posts are stored first, keyed so that an author's recent posts can be read quickly.
Read the steps as text
- Two people post at the same moment. One has 300 followers. The other is a celebrity with 100 million. Both posts are stored first, keyed so that an author's recent posts can be read quickly.
- For the regular author, the post service publishes an event. Fan-out workers load the 300 follower ids and insert the post id into each follower's cached feed. That is 300 small writes, done in well under a second.
- Pushing the celebrity's post the same way would mean 100 million inserts for one post. If the whole fleet manages about 90,000 inserts a second, that is around 18 minutes of work, and every other post waits behind it.
- So the system does not push it. The post service compares the author's follower count with a threshold and publishes no fan-out work for the celebrity. The post just waits in the store.
- A reader opens the app. The feed service reads the reader's precomputed list of post ids from the feed cache. This list holds posts from ordinary accounts only.
- The feed service also looks up which celebrities this reader follows, usually a handful, and fetches their most recent posts by author. Millions of readers ask for the same few lists, so they stay hot in a cache.
- It merges the two lists by time, turns the post ids into full posts, and returns one page. The reader cannot tell which posts were pushed and which were pulled.
- Push keeps reads cheap for the many accounts with few followers. Pull avoids enormous write bursts for the few accounts with many. The threshold between the two is a tuning knob, set by how much fan-out work the write path can absorb.
The hybrid. Push for ordinary accounts. For accounts above a follower threshold, publish no fan-out work. At read time the feed service fetches the recent posts of the few celebrities the reader follows and merges them with the pushed list. This is affordable because a user follows few celebrities, and each celebrity’s recent-posts list is requested by millions of readers, so it is always in cache.
Two refinements save more work. Skip followers who have been inactive for weeks: their feed is rebuilt by pull if they return. And split a large follower list into batches of about a thousand, each its own task, so a failed batch is retried without repeating the rest. Inserts are idempotent, because adding the same post id to a sorted set twice leaves one entry.
Feed storage and pagination
The feed cache holds ids only. A post then has one authoritative copy, so an edit or a delete touches one record and not millions of feeds. Hydration costs one batch get against the post cache, which has a very high hit rate because recent posts are read by many followers.
A cap of 500 entries keeps memory bounded. Few users scroll past 500 posts, and for those who do the feed service falls back to the pull path against the post store.
The cache is not the source of truth. If a node is lost, or a feed has expired, the feed service rebuilds it by pull and writes it back. That is why the pull path must exist even in a push design.
For pagination, the cursor encodes the id of the oldest post on the previous page, and the next request reads entries older than that. New posts arriving at the top do not disturb it. An offset would: if five posts arrive while the user reads, “items 20 to 39” now repeats five posts they have seen.
Ranking as an extension
Most large feeds are ranked, not chronological. The fan-out design stays; its output changes meaning. The cached feed becomes a candidate set of a few hundred recent post ids. On each request a ranking service fetches features for the candidates (recency, how often the reader interacts with the author, early engagement on the post), scores them with a model, applies rules such as “no more than two posts in a row from one author”, and returns the top page.
Ranking makes pagination harder, because order no longer follows the id. A common approach is to rank once per session, keep the ordered list for a few minutes, and let the cursor point into that snapshot.
Bottlenecks and failure modes
- Fan-out lag. A burst of posts builds a backlog in Kafka and feeds go stale. Alert on consumer lag, scale workers on it, and serve online followers before the rest.
- Feed cache node lost. Replicas cover most cases. Otherwise feeds are rebuilt on demand by pull. Limit concurrent rebuilds, or a lost node causes a stampede on the post store.
- The author cannot see their own post. Fan-out is asynchronous, so add the post to the author’s own feed in the write request, or have the client insert it locally.
- Deletes, unfollows and blocks. Removing ids from millions of feeds is slow, so filter at hydration and clean up lazily.
- New follow. The feed has nothing from the new account until it posts again. Backfill a few of its recent posts asynchronously.
- A viral post. One post id is hydrated by millions of readers. The post cache absorbs it, and counters such as likes are updated asynchronously and cached for a few seconds.
- Duplicate events. Kafka redelivers after a worker crash (delivery semantics). Sorted set inserts are idempotent, so a replay is harmless.
Trade-offs
| Decision | Chosen | Alternative | Why |
|---|---|---|---|
| Feed assembly | Hybrid push and pull | Pure push or pure pull | Cheap reads for most users, no write storms; two code paths to maintain |
| Feed contents | Post ids only | Full posts in each feed | One copy of each post; needs a hydration step |
| Fan-out timing | Asynchronous through a log | Synchronous in the post request | Posting stays fast; followers see posts seconds later |
| Pagination | Cursor | Offset | Stable when new posts arrive; cannot jump to an arbitrary page |
| Feed store | In-memory cache, rebuilt on loss | Durable store per user | Fast and cheap to lose; a cold start is expensive |
45-minute interview script
A pacing guide. Interviewers differ, so treat the timings as a default and follow their lead.
- 0:00 to 5:00
Requirements 5 min
Confirm the two operations (publish a post, read the home feed). Ask whether the feed is chronological or ranked, how many accounts a user follows, and how large the biggest accounts are. State the latency target for feed reads.
- 5:00 to 9:00
Capacity estimates 4 min
Derive feed reads per second, posts per second and fan-out inserts per second. Size the feed cache. Say the conclusion, which is that reads dominate requests while fan-out dominates writes.
- 9:00 to 12:00
API 3 min
Create post, get feed with a cursor, and a media upload that goes straight to object storage. Explain why the feed uses a cursor and not an offset.
- 12:00 to 16:00
Data model 4 min
Posts, the follow graph in both directions, and the per-user feed as a capped list of post ids in a cache. Point out that the feed cache holds ids only.
- 16:00 to 25:00
High-level design 9 min
Draw the write path through the post service and the event log to the fan-out workers. Then the read path through the feed service. Then media. Narrate one post from publish to appearing in a follower's feed.
- 25:00 to 34:00
Deep dive: fan-out and celebrities 9 min
Compare fan-out on write and on read with numbers. Show why a 100-million-follower account breaks pure push, then give the hybrid and the merge at read time. Mention inactive users.
- 34:00 to 39:00
Deep dive: feed storage and pagination 5 min
Capped sorted set per user, hydration by batch get, rebuilding a lost feed by pulling, and cursor pagination. Add ranking as an extension if asked.
- 39:00 to 43:00
Failure modes and trade-offs 4 min
Fan-out lag, feed cache loss, deleted posts and unfollows, read-your-own-writes for the author.
- 43:00 to 45:00
Wrap-up 2 min
Summarise push for most, pull for the few, ids in the cache. Name ranking and notifications as the next things to build.
Key takeaways
- A feed can be assembled when a post is written (fan-out on write) or when the feed is read (fan-out on read). Feeds are read far more often than posts are written, so precomputing usually wins.
- Pure fan-out on write fails for accounts with millions of followers, because one post becomes millions of cache writes.
- The hybrid pushes posts from ordinary accounts and pulls posts from celebrity accounts at read time, then merges the two lists.
- The feed cache stores post ids only, capped at a few hundred per user. Content is hydrated at read time, so edits and deletes need no fan-out.
- Keep the pull path even if you mostly push. It rebuilds lost or expired feeds and serves returning inactive users.
- Paginate with a cursor that names the last item seen. Offsets shift when new posts arrive and show duplicates.
Check yourself
0 of 5 answered
Flashcards
These 6 cards are in your review deck. Tap a card to see the answer.
Fan-out on write versus fan-out on read?
On write, each new post is inserted into every follower's precomputed feed, so reads are one lookup. On read, the feed is assembled from each followed account's posts when requested, so writes are cheap and reads are expensive.
What is the hybrid fan-out model?
Push posts from ordinary accounts into follower feeds. Do not push posts from accounts above a follower threshold. Pull those at read time and merge them with the cached feed.
What does the per-user feed cache contain?
A capped list of post ids, a few hundred at most, ordered by time. No content. Posts are hydrated with a batch get at read time.
How do you rebuild a feed that was lost from the cache?
Fall back to fan-out on read. Fetch recent posts from the accounts the user follows, merge them, return the page and write the result back to the cache.
Why use a cursor for feed pagination?
The feed changes between requests. A cursor such as "posts older than id X" is stable when new posts arrive, while an offset shifts and repeats items.
How does ranking fit on top of a chronological feed design?
The cached feed becomes the candidate set. A ranking service scores a few hundred candidates per request with features such as recency and affinity, and the top results are returned.