37. Design a News Feed
1
The interview prompt2
37.1 What do you ask first?3
37.2 How big is this, really?4
37.3 What are the doors?5
37.4 What do we store?6
37.5 What does the whole system look like?7
37.6 Fan-out on write or on read?8
37.7 How do you solve the celebrity problem?9
37.8 How does ranking work?10
37.9 How do the feed cache and pagination work?11
37.10 What would you say in the last five minutes?12
Score yourself13
Variations to practise14
Sources15
Code lab37. Design a News Feed
37.5 What does the whole system look like?
3 min read+10 XP
Step 4: High-level design (10 minutes)
Figure: the final design. Feeds hold IDs; the ranker merges, scores and fills in the details at read time.
Posting, step by step:
- The author posts; the post service saves it in the posts store and publishes
post_created(via an outbox, Chapter 23). - Fan-out workers read the event. If the author is a celebrity, they do nothing more. Otherwise, they read the author's followers from the follow graph, skip those inactive for 30 days, and prepend the post ID to each follower's feed cache (trimming to 800).
Reading, step by step:
- The reader opens the app; the feed service reads their cached list of post IDs.
- It pulls recent posts from the few celebrities they follow.
- The ranker scores the merged candidates (37.8), applies rules, and takes the top 20.
- It hydrates them: post bodies, author names and like counts from caches.
- It stores the ranked order as a session snapshot and returns the first page with a cursor (37.9).
Inactive users who come back have no cached feed: build it on read (pull from everyone they follow), then cache it.
Interviewer pushback:
- "A follower is added after a post was fanned out." → New follows backfill: copy the followee's recent posts into the follower's feed when they follow.
- "The feed cache loses a shard." → Rebuild affected feeds on read from the follow graph and posts store, the same path as returning inactive users.
Memory code: "Push IDs to most, pull from the giants, rank at the door."
Offer: "The interesting parts are fan-out, the celebrity problem, ranking, and pagination. Which would you like?"