News Feed: Ranking and Cache
The final feed.
Feeds today aren't just newest first. They're ranked, mixing recency with how interesting each post is likely to be for you. And the result must be cached, so it loads instantly.

- Not just newest first
- Ranked for you
- Cached for speed
A simple ranking score
A simple ranking score combines engagement and age. More likes and comments raise the score, and older posts slowly sink. Run it. The two hour old post with forty likes comes first. The viral post from two days ago has sunk below it, despite having far more likes.
posts = [
("new, no likes", 0, 1),
("2h old, popular", 40, 2),
("2 days, viral", 500, 48),
]
def score(likes, hours):
return (likes + 1) / (hours + 2) ** 1.5
for name, likes, hours in sorted(posts, key=lambda p: -score(p[1], p[2])):
print(f"{score(likes, hours):6.2f} {name}")5.12 2h old, popular 1.42 2 days, viral 0.19 new, no likes
The full feed design
Putting it together. A new post is saved, and sent to a fan out service through a queue. The fan out service adds the post ID to each follower's feed list in Redis. When a user opens the app, the feed service reads their list, merges in celebrity posts, ranks them, fetches the post contents from a cache, and returns the top twenty.
- Save post
- Queue
- Fan-out to Redis lists
- Merge + rank
- Return top 20

Summary
The feed is ranked by engagement and age, with lists kept in Redis, filled by a fan out service through a queue. Next, a notification system.
Key takeaways- Rank by engagement and age
- Feed lists in Redis
- Fan-out through a queue