Case study: News feed

Topic 7 of 15 in Week 7: Classic case studies14 min readAdvancedFree course

Overview

The home feed shows recent posts from the accounts a user follows. With hundreds of millions of users and some accounts followed by tens of millions, the core question is when to assemble each user’s feed: when posts are written, or when feeds are read.

Try the exercise first: 45 minutes, then compare.

1. Requirements

Functional

  • Users publish posts (text, images, video).
  • Users see a home feed of posts from accounts they follow, newest or ranked, with infinite scroll.

Non-functional

  • Feed loads fast (p99 under ~200 ms).
  • New posts appear in followers’ feeds within about a minute (eventual consistency is fine).
  • Highly available; extremely read-heavy.

2. Estimates

Text
feed reads : 300M users × 10 = 3B / day ≈ 35,000 / s     (peak ~100,000 / s)
posts      : 500M / day ≈ 6,000 / s                        (peak ~15,000 / s)
fan-out    : average ~200 followers → ~1.2M feed inserts / s on average
feed cache : 300M users × 500 IDs × 8 bytes ≈ 1.2 TB (cache only active users → much less)

The fan-out number is the crux: pushing every post to every follower is a huge write load, dominated by the most-followed accounts.

3. API

Text
POST /v1/posts                 {"text": "...", "media_ids": [...]}   → 201 {post_id}
GET  /v1/feed?cursor=…&limit=20                                       → 200 {posts: [...], next_cursor}

4. Data model

Text
posts:    post_id (Snowflake, time-ordered) | author_id | text | media | created_at
follows:  follower_id | followee_id     (+ reverse index followee → followers)
feed cache (Redis): feed:{user_id} → list of recent post IDs (capped at ~500)

5. High-level design

News feed with hybrid fan-out. An author posts through the post service, which stores the post in the posts database and puts a message on a fan-out queue. Fan-out workers read the follow graph and push post IDs into each follower’s list in a feed cache. A reader calls the feed service, which reads post IDs from the feed cache, pulls recent posts from celebrity accounts from the posts database, then merges and ranks them. Normal users get fan-out on write for fast reads; accounts with millions of followers use fan-out on read, merged at request time. The feed cache keeps only the latest few hundred IDs per active user, and inactive users’ feeds are rebuilt on demand.

6. Deep dives

Fan-out on write (push)

When a post is created, workers look up the author’s followers and prepend the post ID to each follower’s feed list in the cache.

  • Reads are fast: the feed is already assembled; just read a list.
  • Writes are expensive: a post by an account with 50 million followers means 50 million inserts, and its posts arrive late at the end of a long queue.
  • Wasted work for followers who never log in.

Fan-out on read (pull)

When a user opens the feed, fetch recent posts from each account they follow and merge.

  • Writes are cheap: just store the post.
  • Reads are expensive: a user following 2,000 accounts needs 2,000 lookups (or a scatter-gather query) on every feed load.

Hybrid (the usual answer)

  • Use push for normal accounts (most of them).
  • For celebrities (above a follower threshold), don’t fan out. At read time, the feed service fetches recent posts from the handful of celebrities the user follows (cached per celebrity) and merges them with the precomputed feed.
  • Skip inactive users during fan-out; when they return, rebuild their feed by pulling.

What the feed cache stores

Only post IDs (and maybe author ID and timestamp), capped at a few hundred per user. To render the feed, the service hydrates IDs in batches: post bodies from a post cache, author names and avatars from a user cache, counts from a counter service. This keeps the feed cache small and avoids stale copies of post content.

Ranking

Chronological order is simple. Ranked feeds work in two stages:

  1. Candidate generation: gather a few hundred to thousand candidate posts (precomputed feed + celebrity pulls + recommendations).
  2. Scoring: a model scores each by predicted engagement using features such as recency, affinity with the author and content type; take the top results.

Ranking runs at read time (or is partially precomputed) and must fit the latency budget.

Pagination

Use cursor pagination (week 1): the cursor encodes the position in the ranked or time-ordered list, so new posts arriving at the top don’t shift pages and cause duplicates.

Deletes and privacy

When a post is deleted or an account blocks someone, already-fanned-out IDs remain in caches. Filter at read time during hydration (missing or hidden posts are dropped), and optionally clean up asynchronously.

7. Trade-offs and bottlenecks

  • Fan-out workers are the heaviest component; scale them on queue depth and prioritise fan-out to active users.
  • The celebrity threshold balances write load against read-time merging.
  • Feed cache memory: cap list length and keep only active users.

8. Follow-up questions

  • A celebrity posts and 50M users open the app? Their posts are pulled from a hot cache, not fanned out; cache with local replicas.
  • Show likes/comments counts? Fetched separately from an eventually consistent counter service.
  • Unfollow? Filter at read time; remove entries lazily.

Recap

A news feed must choose between fan-out on write (precomputed feeds, fast reads, expensive writes) and fan-out on read (cheap writes, slow reads). The standard design is a hybrid: push post IDs into per-user feed caches for normal accounts, pull celebrity posts at read time, skip inactive users, hydrate IDs in batches, rank candidates and paginate with cursors.

Exercise

Design it yourself (45 minutes)

Set a 45-minute timer and design the home feed for a social network: 300 million daily active users; users follow up to 5,000 accounts, but some accounts have 100 million followers; each user opens the feed 10 times a day; 500 million new posts per day. The feed should load in under 200 ms and show new posts within a minute. Use the framework, then compare.

Text
1. Clarifying questions:
2. Functional requirements:
3. Non-functional requirements:
4. Estimates:
5. API:
6. Data model:
7. High-level design:
8. Deep dives (fan-out strategy, celebrities, ranking, pagination):
9. Trade-offs and bottlenecks:

Summary

  • Fan-out on write precomputes each follower's feed for fast reads; fan-out on read assembles feeds at request time for cheap writes.
  • A hybrid handles celebrities: push for normal accounts, pull and merge for accounts with huge follower counts.
  • Feed caches store only post IDs per user; post bodies and user data are fetched in batches from their own caches.
  • Skip fan-out work for inactive users and rebuild their feeds on demand.
  • Ranking combines candidate generation with scoring, and pagination uses cursors.