Design a News Feed (Twitter / Facebook)
The definitive fanout question. Push vs pull, the celebrity problem, and why the correct answer is a hybrid with an explicit threshold.
#1. Requirements
Functional
- Users post text (plus optional media).
- Users follow other users.
- A user's home feed shows recent posts from people they follow, newest first.
- Feed is paginated and infinitely scrollable.
Non-functional
- Feed load must be fast — this is the app's core interaction. Target p99 under ~200 ms.
- Read-heavy by a large factor.
- Eventual consistency is acceptable: a post appearing a few seconds late is invisible to users.
- Highly available; a stale feed beats no feed.
Cut explicitly: ranking/ML ordering (assume reverse-chronological, and say real systems rank), ads, DMs, search, notifications. Cutting scope out loud is scored.
Clarifying questions:
- Reverse-chronological or ranked? (Huge difference — say you'll assume chronological and note where ranking would change things.)
- Follower distribution — do we have celebrity accounts with tens of millions of followers?
- How far back does the feed go? Do we need the full history or just recent?
#2. Estimation
500M DAU, average 200 follows, 2 posts/user/day, 50 feed loads/user/day.
Writes: 500M × 2 / 10⁵ = 10,000 posts/s avg → 30,000 peak
Reads: 500M × 50 / 10⁵ = 250,000 feed loads/s → 750,000 peak
Read:write ≈ 25:1
Post storage: 500 B × 1B posts/day = 500 GB/day → 180 TB/yr
FANOUT COST (the number that matters):
10,000 posts/s × 200 avg followers = 2,000,000 feed writes/s
One celebrity post @ 100M followers = 100M writes from ONE actionWhat this decided: 2M feed-writes/s is large but tractable with a partitioned cache. The 100M-writes-from-one-post figure is what kills naive push and forces the hybrid. Compute this number explicitly in the interview — it's the justification for the entire design.
#3. API
POST /v1/posts { text, media_ids } → 201 {post_id}
GET /v1/feed?cursor=...&limit=20 → posts + next_cursor
POST /v1/users/{id}/follow → 204Use cursor pagination, not offset. Offset pagination means "give me items 21–40". On a feed that's constantly gaining items, that breaks: if 3 new posts arrive while you read page 1, everything shifts down by 3, so page 2 starts with 3 posts you've already seen. The cursor instead encodes a position — "posts older than post ID 98231" — which stays correct no matter what is added above it, and the database can jump straight to it (O(1)) instead of counting past every earlier item.
#4. The core decision: fanout
Fanout is what happens to one post on its way to many timelines. The only real question in this design is when that work happens: at the moment someone posts, or at the moment someone opens the app. Everything else follows from that choice.
#Fanout on write (push)
When a user posts, immediately write the post ID into every follower's precomputed feed list.
Pros: reads are trivially cheap — a single cache read, ~1 ms. Since reads outnumber writes 25:1, you're moving work to the cheaper side of the ratio.
Cons: writes are expensive and unbounded. A celebrity post is 100M writes. It also wastes enormous work on inactive users — writing into the feed of someone who hasn't opened the app in a year.
#Fanout on read (pull)
Store posts once. At feed load, query the posts of everyone the user follows and merge.
Alice loads feed ──► fetch recent posts from each of her
200 followees, merge, sort, take 20Pros: writes are O(1) — one insert. No wasted work, no celebrity problem at all.
Cons: reads are expensive — a 200-way scatter-gather (send 200 queries out in parallel, one per followee, then wait for and combine all the answers) on every feed load, at 750K reads/s. And feed load is the most frequent, most latency-sensitive operation in the product. Reads outnumber writes 25:1, yet this puts all the work on the read side — precisely backwards.
#The hybrid (the answer)
ON POST:
if author.follower_count < THRESHOLD (~10,000):
PUSH — fan out to all followers' feed caches
else:
DON'T fan out. Store the post; mark author as "celebrity"
ON FEED READ:
1. read precomputed feed from cache (push portion)
2. fetch recent posts from the few celebrities this
user follows (pull portion)
3. merge by timestamp, return top NWhy this works: almost all users are below the threshold, so almost all posts are pushed and reads stay cheap. The handful of accounts that would cause 100M-write storms are pulled instead — and since each user follows only a few celebrities, the pull portion is a small, bounded fetch (say 5 accounts, not 200).
Refinements worth mentioning:
- Only fan out to active users. Someone who hasn't opened the app in 30 days doesn't need a materialized feed; build it on demand when they return. This can cut fanout volume by more than half.
- Cache celebrity posts aggressively. Every follower requests the same few posts, so the hit rate approaches 100%.
- The threshold is tunable, and it's a knob you'd tune from production data rather than pick a priori.
#5. Storage design
posts (Cassandra — write-heavy, append-only, time-ordered)
partition: user_id clustering: created_at DESC
→ "recent posts by user X" is a single-partition range scan
follows (sharded SQL or KV)
(follower_id, followee_id) — index both directions:
"who do I follow" (for pull) and
"who follows me" (for fanout)
feed_cache (Redis — the materialized feeds)
key: feed:{user_id}
value: sorted set of post_ids scored by timestamp
TRIMMED to ~800 entriesThe trim is important. Nobody scrolls past a few hundred items; storing unbounded feeds would dominate memory. Beyond the cached window, fall back to generating the feed on demand — slower, and it's an interaction ~0.1% of sessions ever have.
Feed cache memory:
500M users × 800 post_ids × 8 B = 3.2 TB
→ but only ACTIVE users need one. Say 20% active in 7 days:
100M × 800 × 8 B = 640 GB → ~20 Redis nodes at 32 GB ✓That calculation — and the decision to materialize only active users — is exactly the kind of scoping that makes an expensive design affordable.
#6. High-level design
POST ──► ┌───────────┐ ┌─────────┐
│Post service│──►│Cassandra│ (source of truth)
└─────┬─────┘ └─────────┘
│ publish
▼
┌──────────┐ ┌───────────────┐
│ Kafka │───►│Fanout workers │
└──────────┘ └───────┬───────┘
look up followers
if not celebrity → write to feed caches
│
▼
FEED ──► ┌───────────┐ ┌──────────────┐
READ │Feed service│──►│Redis feed │ push portion
│ │ │cache │
│ │ └──────────────┘
│ │──► celebrity posts (pull portion, cached)
│ │──► hydrate post content + author + counts
└───────────┘Why fanout is async via Kafka: the user's post must return in ~50 ms. Fanning out to 200 followers takes longer than that, and the user doesn't need it to have happened. Publishing an event and returning immediately is the right call — and it gives you retry and buffering for free.
Hydration is the step that is easy to overlook: the feed cache holds only post IDs. Rendering needs post text, author name and avatar, like/comment counts, and whether this user liked it. That's a multi-get against several services — batch it, cache it, and be aware this is where tail latency amplification bites: a feed page has to wait for the slowest of its many lookups, so even if each service is slow only 1% of the time, a page that makes 20 lookups is slow far more often than 1%.
#7. Deep dives
#Fanout worker throughput
2M feed-writes/s sounds alarming, but each one is a tiny Redis ZADD (add one item to a sorted set), and they can be pipelined: send hundreds of commands in one network trip instead of waiting for a reply after each.
Per post: look up followers (1 read), then N sorted-set adds
Batch with pipelining: ~50K ZADDs/s per Redis node
2M / 50K = 40 nodes — consistent with the 20-node memory
estimate plus headroom ✓Partition fanout work by follower ID so each worker writes to a known set of Redis shards. A worker then has many writes queued for the same shard and can send them together in large batches, instead of many small batches scattered across every shard.
#Ordering and consistency
Feeds are eventually consistent, and that's fine — except for one case users do notice: their own posts. A user who posts and doesn't immediately see it in their feed reports a bug.
Fix with a targeted read-your-writes guarantee (you always see your own changes, even if others see them later): merge the user's own recent posts from the primary store into their feed at read time. Cheap, and it solves the only staleness anyone perceives.
#Handling unfollows and deletes
Removing a post means removing it from potentially millions of feed caches — another unbounded fanout. Better: filter at read time. Keep a small set of deleted post IDs (or check post existence during hydration) and drop them when assembling the response. Hydration already fetches post content, so a deleted post naturally disappears.
Same for unfollows: don't scrub the cache, filter at read.
#Ranking
If asked to move beyond chronological: retrieve a candidate set (the merged feed, a few hundred items), then rerank with a model scoring engagement probability. The architecture barely changes — you fetch more candidates and add a ranking step before returning. Precompute features offline; keep the online scoring within your latency budget. Naming "retrieve then rerank" is enough to show you know how it works.
#8. Failure modes & follow-ups
| Failure | Behaviour |
|---|---|
| Fanout workers lag | Feeds go stale by minutes. Degraded, not broken. Alert on consumer lag measured in time ("the oldest unprocessed post is 3 minutes old"), not in message count — users feel delay, not queue length. |
| Redis feed shard dies | Those users' feeds rebuild on demand (pull path) — slower but functional. Design the pull path to always work, so it doubles as the fallback. |
| Cassandra shard down | Posts by users on that shard unavailable; feed renders without them (partial results beat errors). |
| Celebrity posts | Never fanned out by design; served from a near-100%-hit-rate cache. |
Follow-ups:
- "What if a normal user suddenly goes viral?" — the threshold must be dynamic, evaluated at post time against current follower count, and an account can cross it between posts. Also worth handling: an account near the threshold that flips back and forth. Its posts would switch between being pushed and being pulled, so make sure the read-time merge finds both kinds and doesn't show a post twice or miss one.
- "How would you add stories/ephemeral content?" — different shape: small, expiring, viewed once. Pull works better; TTL handles cleanup.
- "Feed for a user following 5,000 accounts?" — the merge cost at read time grows; cap the pull portion and rely more heavily on push for these users.
- "Multi-region?" — feed caches are regional and rebuildable, so don't replicate them. Replicate posts and follows; regenerate feeds locally. Feeds are derived data — they can always be rebuilt from posts and follows — and recognizing that derived data shouldn't be shipped across regions is a good instinct.
#Check yourself
What is the one number that justifies the news-feed design?Show answer
Fanout on write versus fanout on read?Show answer
What is the hybrid fanout design?Show answer
Name two refinements to the hybrid.Show answer
How is the feed cache stored and bounded?Show answer
What is hydration, and why does it matter?Show answer
How do you handle deletes and unfollows?Show answer
Why use cursor pagination for feeds?Show answer
#What to carry forward
- Compute the fanout number first — it justifies the whole design.
- Hybrid fanout: push for normal authors, pull for celebrities, merged at read.
- Materialise feeds only for active users and trim them; fall back to the pull path.
- Fan out asynchronously via a queue; fix read-your-writes for the author's own posts.
- Filter deletes and unfollows at read time; regenerate derived feeds per region.