Skip to content

News Feed

intermediate · 35 min · Asked often · Last reviewed 31 Oct 2026 · by TheLearningLoop Team

Key takeaways

  • Fan-out on write makes reads cheap but a celebrity post causes a huge number of writes. Fan-out on read does the reverse.
  • Real feeds are hybrids: push for ordinary accounts, pull for celebrities, merged when the feed opens.
  • Feed caches hold post IDs only and are derived data, so they can be rebuilt and are eventually consistent.

Why a news feed is a classic design question, and what it teaches

A news feed shows each user a ranked list of recent posts from the accounts they follow. It looks simple, but one post may have to reach millions of followers while millions of people open their feed at once. It teaches fan-out, caching, hot keys and the cost of precomputing.

Fan-out means delivering one post to many followers' feeds. A hot key is a single account or item that receives far more traffic than the rest, such as a celebrity's post. Precomputing means building each feed ahead of time, when a post is written, instead of when it is read.

It gives you two things interviewers always ask about:

  • Low read latency: opening the feed should be fast, because people open it far more often than they post.
  • Scalability: one post by an account with a huge audience must not melt the system.

Requirements to confirm first:

  • Functional: publish a post, follow and unfollow, read your feed in order, optionally ranking and media.
  • Non-functional: feed opens in a fraction of a second, new posts show up quickly, millions of users, and a few accounts with a huge number of followers.

Skip the design, or simplify it, when:

  • Users follow few accounts and posts are rare: query the posts of everyone you follow at read time.
  • The feed is the same for everyone (a public timeline): cache one list.

Capacity estimation (illustrative numbers)

Estimates decide the design, so do them before choosing a strategy. These figures are assumptions for practice, not measurements of any real product.

  • Reads: 100 million daily active users, each opening the feed 10 times a day, is 1,000,000,000 opens a day. A day has 86,400 seconds, so that is about 11,600 opens a second on average, and about 35,000 a second at a peak of three times the average.
  • Writes: if 10 percent of those users post once a day, that is 10,000,000 posts a day, about 116 a second. Reads outnumber posts 100 to 1.
  • Push cost: with an average of 200 followers, 10,000,000 posts cause 2,000,000,000 cache writes a day, about 23,000 a second.
  • Pull cost: if each reader follows 200 accounts, 1,000,000,000 opens cause 200,000,000,000 post lookups a day, about 2,300,000 a second. That is 100 times the push cost, which is why push wins for ordinary accounts.
  • A celebrity: one post to 20,000,000 followers is 20,000,000 cache writes. If the worker pool can do 100,000 cache writes a second, the last follower sees the post after about 200 seconds.
  • Cache size: 500 post IDs per user at about 16 bytes each (an ID and a score) for 100,000,000 users is 800,000,000,000 bytes, about 800 GB before replication and overhead. Caching only active users and trimming the list shrinks it.

Try it

One author posts and followers open the feed. Switch the strategy, then change who posts or how often the feed is read.

News Feed

Step 1 of 4 · Press Space to play, arrow keys to step

Before the post

Author1000 followersPost storeemptyFeed cachesemptyReadersidle

Initial state. One author with 1000 followers is about to post. After that, followers open their feed 10 times in total.

tick:
0
feed writes:
0
read queries:
0
total work:
0

1 tick = one step of the simulation.

Things to try:

  • Run Fan-out on write for a normal user. Copying a post to 50 caches is cheap, and reading is one cache lookup.
  • Run the same strategy for a celebrity. One post causes a thousand writes.
  • Run Fan-out on read with 200 feed opens, then try the Hybrid and compare total work.

What to watch for: total work adds feed writes and read queries, counting each as one unit. That is a simplification, because a write is usually dearer than a read.

  • Normal user: push costs 50 + 10 = 60, pull costs 2000, and the hybrid pushes, so it also costs 60.
  • Celebrity: push costs 1000 + 10 = 1010, pull costs 2000, and the hybrid costs 20. Pull is not cheap here either, because each simulated reader follows 200 accounts and pull checks all 200 on every open. The hybrid wins by pulling only the one celebrity, so its figure assumes the reader follows a single celebrity.
  • 200 feed opens: push costs 50 + 200 = 250, pull costs 40000, and the hybrid pushes at 250.

Feed generation strategies

StrategyHow it worksGood whenWeakness
Fan-out on write (push)When someone posts, copy the post ID into every follower's feed cacheMost users, followers in the hundredsA celebrity post causes millions of writes, and inactive users' caches are wasted
Fan-out on read (pull)Store the post once. At read time fetch recent posts from everyone you follow and merge themReaders who follow few accounts, or users who rarely open the feedEvery feed open is expensive and slow
HybridPush for normal accounts, pull for celebrities and merge at read timeReal systems with both kinds of accountsMore code, and a rule for who counts as a celebrity

Push vs pull

  • Push moves the cost to write time. Reads are one cache lookup, but a post by a popular account is amplified into a huge number of writes, and followers see it only after the fan-out finishes.
  • Pull moves the cost to read time. Writes are one insert, but each feed open touches many accounts, which is slow when readers outnumber posters.

Reads usually outnumber writes by a large factor, so push wins for ordinary accounts and pull wins for the few accounts that would explode the push.

Each side scales with a different number. Push cost grows with the author's followers. Pull cost grows with the accounts each reader follows, multiplied by the number of feed opens. Push is cheaper for a post when followers are fewer than about (accounts followed minus 1) times the number of opens. With 200 accounts followed and 10 opens that is 1,990 followers, so a 1,000-follower account is still cheaper to push in this model. The celebrity threshold is therefore a tuned setting, not a fixed law.

How a hybrid feed works

  1. A user posts. The post is saved in the post store, and an event goes to a queue.
  2. A fan-out worker reads the author's follower list. If the author has fewer than a threshold of followers, it adds the post ID to each follower's feed cache (a short list of recent IDs).
  3. For celebrity authors, the worker does nothing.
  4. When a user opens the feed, the service reads their cache, fetches recent posts from the celebrities they follow, merges and ranks the lists, and loads post details by ID.
  5. Caches keep only the latest few hundred IDs, and inactive users' caches expire and are rebuilt on demand.

Details that matter in practice:

  • Which followed accounts are celebrities? Step 4 needs this cheaply, so keep a flag on the account, or a small per-user list of followed celebrities, instead of scanning all follows on every open.
  • Follow and unfollow: when a user follows someone, backfill that author's recent post IDs into their cache. When they unfollow, remove that author's IDs, or filter them out at read time.
  • Delete and edit: posts are stored once and feeds hold IDs only, so an edit shows at once. Deleted posts are filtered out when details are loaded.
  • Threshold boundary: an account that crosses the threshold changes from push to pull. Old pushed IDs stay valid, and new posts are merged at read time.
  • Pagination: return a cursor (the last score seen) with each page, so the next page is the next slice of the sorted list.

News feed design vs the alternatives

ConcernHybrid feed servicePure pull from the databaseChronological per-author pages
Fast feed openYesSlow for many followsSlow to merge
Handles a celebrity postYesYesYes
Cost of a normal postSome writesOne writeOne write
Ranking and mixing sourcesYesHardNo
Extra moving partsQueue, workers, cachesNoneNone
Best forLarge social productsSmall productsSimple blogs

Choose pure pull while the product is small, and move to the hybrid when read latency or celebrity posts hurt.

Avoiding a new single point of failure

  • Feed caches: they are derived data, so if one is lost it is rebuilt from the post store and the follow graph.
  • Fan-out workers: run many behind a queue. If they fall behind, new posts appear late but are not lost.
  • Post store: replicate it and partition by author or post ID. Partitioning by author puts a celebrity's posts on one partition that every read merge hits, so cache those recent posts or partition by post ID.

You accept that feeds are eventually consistent: a new post may take a few seconds to reach every follower.

More failure cases to walk through:

  • Queue backlog: fan-out delay is roughly the followers of the author divided by the workers' write speed (about 200 seconds in the estimate above). Add workers, split a huge job into batches of followers, and alert on queue age.
  • Worker crash and retry: a job may run twice. Adding the same post ID with the same score to a sorted set again changes nothing, so retries are safe.
  • Feed cache lost: serve the feed by pulling for that user, then rebuild the cache. Reads are slower for a while but correct.
  • Ranking service down: fall back to newest first, so the feed still opens.

Common mistakes

  • Pushing celebrity posts to everyone. Use pull or a hybrid for large accounts.
  • Storing full posts in every feed. Store IDs only and load details separately.
  • Unbounded feed caches. Keep only the most recent entries.
  • Fan-out in the request path. Do it asynchronously from a queue.
  • Ignoring inactive users. Skip pushing to users who have not opened the app for a long time.
  • Hard-coding a celebrity threshold. Make it a setting you can tune.

How to talk about a news feed in an interview

  • Ask for the scale: followers per account, posts per day, feed opens per day, and whether the feed is ranked.
  • Explain push vs pull with the read to write ratio, and say why celebrities break pure push.
  • Propose the hybrid, with a queue, fan-out workers and per-user feed caches holding IDs.
  • Name the trade-off: precomputing makes reads fast but adds write cost and storage, and gives eventual consistency.

Where you meet news feeds in real systems

Social networks, activity streams, marketplaces and even internal dashboards build a feed from many sources. Here are three drawn out, the first with configuration you can run, then more examples.

Example 1: A feed cache of post IDs

Each user's feed is a short list of post IDs, ordered by time or score, kept in a sorted set. The follow graph is a table. Open the example below to see the picture and the commands.

Config: feed cache and follow tableSetup 1 table and 5 commandsCode no code changes
Post savedpost storeEvent queuedfan-out jobWorker copies IDto each followerFeed readone cache lookup
A new post is saved once, then a worker adds its ID to every follower's feed. Opening the feed is a single read of that list.
-- Who follows whom. The primary key stops duplicate follows, and the index finds followers quickly.
CREATE TABLE follows (
  follower_id bigint NOT NULL,
  followee_id bigint NOT NULL,
  created_at  timestamptz NOT NULL DEFAULT now(),
  PRIMARY KEY (follower_id, followee_id)
);
CREATE INDEX follows_followee ON follows (followee_id);

INSERT INTO follows (follower_id, followee_id) VALUES (1, 100), (2, 100), (3, 100);

-- The fan-out worker reads the author's followers (in batches in a real system).
SELECT follower_id FROM follows WHERE followee_id = 100 ORDER BY follower_id LIMIT 1000;

The Redis commands were run against a real Redis 7 server: adding to a sorted set, trimming to the newest entries, reading newest first and setting an expiry. The SQL was run against a real PostgreSQL 16 server. Names and sizes are examples. One detail the commands do not show: updating a sorted set with ZADD does not extend an existing expiry, so refresh it with EXPIRE whenever the user opens the feed or a post is pushed, otherwise an active user's cache would still expire after a week.

Example 2: Merging a celebrity at read time

A user follows ordinary accounts and one celebrity. Ordinary posts were pushed into the feed cache. The celebrity's posts are fetched when the feed opens and merged in.

UserFeed serviceFeed cachePost storeRanking
The feed service reads the pushed IDs from the user's cache, pulls the celebrity's latest posts from the post store, then merges and ranks both lists.

Example 3: The cost of a celebrity post

One post is cheap to store. Pushing it is cheap for 50 followers and costly for a million.

50 followers501,000 followers10001,000,000 followers1,000,000
A normal account causes tens of writes, but an account with a million followers would cause a million. The hybrid skips the push for the large one.

More real-world scenarios

The scenarios below are illustrative patterns, not descriptions of any named company's internals.

4. Social network timeline

Friends' posts are pushed into each user's feed, with ranking applied at read time using recency, closeness and engagement.

5. Professional network updates

Updates from connections and followed pages are mixed with sponsored items, which are inserted at read time.

6. Short-video feed

Recommendations rather than follows decide the list, but the same pattern applies: a candidate list per user, built ahead of time and re-ranked on open.

7. Marketplace activity

Sellers you follow list new items. A pull query over a small follow list is enough, with a cache for hot pages.

8. Team activity streams

A workspace shows what changed in projects you watch. Events go into a per-user stream, and old entries are trimmed.

9. Email-style notifications digest

A daily summary is built by pulling events once per user per day, which is cheaper than pushing each one.

10. News sites

A front page is the same for everyone, so one cached list serves millions, with personalised blocks added at the edge.

Tools and managed services

KindNameNotes
CacheRedisSorted sets for per-user feeds
QueueApache Kafka, RabbitMQCarry post events to fan-out workers
DatabasePostgreSQL, Apache CassandraPost store and follow graph
Search and rankingElasticsearch, OpenSearchCandidate retrieval and scoring
ManagedAmazon ElastiCache, Amazon DynamoDBHosted cache and key-value store

The takeaway

Whenever reads outnumber writes by a large factor, precompute the result at write time, except for the few accounts so large that precomputing would cost more than computing on read.

Interview questions

Try answering out loud before you reveal hints. Interviewers care about how you reason, not just the final answer.

  1. 1.

    Should the feed be built when a post is written or when it is read?

    medium
  2. 2.

    A celebrity with 20 million followers posts. What goes wrong with fan-out on write, and what do you do?

    hard
  3. 3.

    What do you store in each user's feed cache?

    easy
  4. 4.

    Pushing to every follower wastes work on people who never open the app. How do you reduce it?

    medium
  5. 5.

    A user posts and refreshes immediately but does not see their post. How do you handle that?

    medium
  6. 6.

    Estimate the feed load for 100 million daily active users who open the feed 10 times a day and where 10 percent post once a day.

    medium
  7. 7.

    A user follows a new account. What do they see in their feed, and what happens when they unfollow?

    medium
  8. 8.

    An author deletes a post that was already pushed into a million feeds. What do you do?

    medium
  9. 9.

    The fan-out queue is hours behind after a viral post. What do you do?

    hard

Finished this lesson?

Progress is saved on this device. Create a free account to keep it everywhere.

Topics:News feedTimelineFan outPush vs pullCelebrity problemCachingCase study

↑ Back to top

Next lesson