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.8 How does ranking work?
3 min read+10 XP
A ranked feed is built in four stages:
Figure: candidates → score → rules → hydrate.
- Candidates: the post IDs from the feed cache plus celebrity pulls (a few hundred).
- Score: a simple, explainable model is affinity × type weight × freshness:
- affinity: how much you interact with this author (likes, comments, profile visits).
- type weight: some post types tend to get more engagement.
- freshness: halves every 12 hours (a decay, like Chapter 34's trends). Facebook's early feed ranking was publicly described in roughly this shape; modern feeds use machine-learned models predicting likes, comments and time spent, but the pipeline is the same.
- Rules: diversity (not five posts in a row from one author), remove posts already seen, demote low-quality or reported content.
- Hydrate: fetch post bodies and counts for the final 20 only.
In the chapter's code, a close friend's 3-hour-old photo ranks 2nd of 302 candidates; in plain time order it would be buried below newer posts from people the reader barely knows.
Keep ranking cheap at read time: precompute affinities offline, score a few hundred candidates per request, and cache the result for the session.
Memory code: "Gather a few hundred, score them, keep the best twenty."