Catalog My learning Blog
System Design Interview Prep: Framework and Questions · Phase 1 · Lesson 7 · 5 min read

Design a News Feed: Interview Answer

On this page
  1. The problem: design a social media news feed
  2. News feed requirements and the read-to-write ratio
  3. Fan-out on write vs fan-out on read
  4. The celebrity problem and the hybrid feed
  5. Storing and serving the feed
  6. What a strong news feed answer includes
  7. Check yourself

The problem: design a social media news feed

"Design the home feed for a social app. Users follow others and see their posts, newest first." Clarify and estimate yourself first. Then read on.

News feed requirements and the read-to-write ratio

Functional: follow users; post; open the app and see recent posts from everyone you follow.

Non-functional: the feed is the most-loaded screen, so it must open fast, say under 200 ms. A post may take a few seconds to appear in followers' feeds; nobody notices.

Estimate. 300 million daily users, each opening the feed 10 times a day, and posting once every few days on average:

Code
Feed reads   300M × 10 ÷ 100,000 s   ≈ 30,000 per second average
Posts        about 100M per day      ≈ 1,000 per second
Feed writes  1,000 posts × 200       ≈ 200,000 per second if every post is copied to every follower

Reads outnumber writes about 30 to 1. Remember that ratio; it drives the main decision. The last line, assuming 200 followers on average, is the price of precomputing feeds: each post becomes one small write per follower. A sharded in-memory store handles that, but it's the number the celebrity problem below blows up.

Fan-out on write vs fan-out on read

Fan-out on write (push). When someone posts, add the post id to each follower's stored feed right away. Opening the app is then one fast read of a precomputed list. Cheap reads, expensive writes: a user with 200 followers causes 200 small writes per post.

Fan-out on read (pull). Store each post once. When a user opens the app, fetch recent posts from everyone they follow and merge them. Cheap writes, expensive reads: following 500 people means 500 lookups per feed load, 30,000 times a second.

With 30 reads per write, paying at write time is the better default. Until you meet a celebrity.

The celebrity problem and the hybrid feed

An account with 50 million followers posts. Fan-out on write means 50 million feed updates for one post. Several celebrities posting during a big event can back up the fan-out queue for minutes.

So most large systems use a hybrid:

  • Accounts below a follower threshold, say 100,000, fan out on write as normal.
  • Accounts above it don't fan out. Their posts are stored once.
  • At read time, the feed service takes the user's precomputed feed and merges in recent posts from the few celebrities they follow.

Twitter described this design in 2013: each home timeline was a list of about 800 tweet ids in Redis, filled by fan-out on write, and the company was moving to merging tweets from accounts with huge followings at read time instead (High Scalability, 2013).

StrategyWrite costRead costGood for
Fan-out on writeHigh for big accountsVery lowTypical accounts
Fan-out on readVery lowHigh, every loadHuge accounts
HybridBounded by the thresholdLow, plus a small mergeReal follower distributions
Fan-out on write precomputes each follower's feed so reads are cheap but a celebrity post causes millions of writes. Fan-out on read writes once and pays at every feed load. A hybrid fans out on write for normal accounts and merges celebrity posts at read time.
Fan-out on write precomputes each follower's feed so reads are cheap but a celebrity post causes millions of writes. Fan-out on read writes once and pays at every feed load. A hybrid fans out on write for normal accounts and merges celebrity posts at read time.

Storing and serving the feed

  • The feed store holds a bounded list of recent post ids per user, for example the latest 500, in a fast key-value store such as Redis sorted sets keyed by user. Ids, not full posts, so a viral post isn't copied into millions of feeds.
  • The post service stores post content and serves it by id, heavily cached, because the same popular post appears in many feeds.
  • The fan-out workers consume new-post events from a queue, look up followers and update feeds. The queue absorbs bursts and lets workers retry.
  • Pagination uses a cursor, such as the last post id the user saw, not page numbers. New posts arrive at the top, so page offsets shift and show the same post twice.
  • Inactive users don't need fan-out. Skip users who haven't opened the app in weeks and rebuild their feed with a pull when they return.

A ranking service that reorders by predicted interest is a natural extension if the interviewer asks about personalization. The usual shape: pull a few hundred candidate post ids from the precomputed feed, score each with a model on features such as the author's relationship to the user and the post's early engagement, then return the top results. Ranking reorders the candidates; it doesn't change how they're collected, so the fan-out design stays the same. Present newest-first first; it's simpler and fully valid.

What a strong news feed answer includes

  • The read-to-write ratio, and how it justifies fan-out on write.
  • The celebrity problem stated before the interviewer raises it, and the hybrid.
  • Feeds as lists of ids, with post content served separately.
  • A queue between posting and fan-out, and what happens when it backs up.
  • Edge cases: unfollows, deleted posts (filter at read time), inactive users.

Check yourself

A popular post would be copied into millions of feeds, wasting memory and making edits or deletions hard. Ids keep feeds small, and content is fetched once from a cached post service.

Don't fan out posts from accounts above a follower threshold. Store the post once, and when a follower loads their feed, merge recent posts from the celebrities they follow into their precomputed feed at read time. Normal accounts keep fan-out on write, so most reads stay fast.

Mark it deleted in the post service and filter deleted ids when serving feeds. Optionally clean feed entries lazily in the background.

Reads vastly outnumber writes, often by 100 to 1 or more, because people scroll far more than they post. That justifies doing extra work at write time, precomputing feeds by fan-out, so the much more frequent reads are cheap.

In an in-memory store such as Redis, as a list of the most recent few hundred post ids per user, with the post content in a separate cache and a durable database behind it. Older entries are dropped and rebuilt from the database if a user scrolls far back.