Design: News Feed
AdvancedA news feed (Facebook, Twitter/X) aggregates posts from followed users and ranks them for display. The core trade-off is fan-out on write (pre-compute feeds) vs fan-out on read (compute at request time).
Overview
A news feed shows a personalised, ranked list of posts from people and pages a user follows. The two main approaches are: Fan-out on write (push model) — when a user publishes a post, it is immediately pushed to all followers' precomputed feed caches. Reading the feed is fast (just read the cache), but writing is expensive for users with millions of followers (celebrity problem). Fan-out on read (pull model) — when a user opens their feed, the system fetches recent posts from all followed users, merges, and ranks them in real-time. Writing is cheap but reading is slow. Hybrid approach (used by Twitter/X, Facebook): fan-out on write for regular users (fast read), fan-out on read for celebrities (avoid writing to millions of feeds). Feed ranking uses algorithms considering recency, engagement (likes, comments), relationship strength, and content type.
Fan-Out on Write vs Read
Fan-out on write pre-computes feeds (fast reads, expensive writes). Fan-out on read computes at request time (cheap writes, slower reads). Most systems use a hybrid.
// Fan-out on write (push model)
// User posts → push to all followers' feed caches
//
// Alice (1000 followers) posts "Hello World"
// → Write to 1000 feed caches (one per follower)
// → Bob opens feed → read from pre-computed cache (fast!)
//
// Problem: Celebrity with 10M followers → 10M writes per post! 😱
// Fan-out on read (pull model)
// User opens feed → fetch posts from all followed users → merge + rank
//
// Bob follows 500 users → opens feed
// → Query 500 users' recent posts → merge → rank → return
// → Slow if following many users
// Hybrid (Twitter/X approach)
// Regular users (< 10K followers): fan-out on write
// Celebrities (> 10K followers): fan-out on read
//
// Bob's feed = pre-computed cache (regular follows)
// + real-time merge (celebrity follows)
// Feed cache (Redis sorted set per user)
// Key: feed:bob
// Members: post_ids, scored by timestamp
ZADD feed:bob 1711700000 "post:123"
ZADD feed:bob 1711699000 "post:456"
ZREVRANGE feed:bob 0 19 // top 20 posts, newest firstFeed Ranking
Raw chronological feeds are replaced by ranked feeds that consider engagement, recency, relationship strength, and content type. ML models predict the probability a user will engage with each post.
// Feed ranking signals
// 1. Recency: newer posts score higher (decay function)
// 2. Engagement: posts with more likes/comments score higher
// 3. Relationship: posts from close friends score higher
// 4. Content type: video > image > text (per-user preference)
// 5. Diversity: avoid showing 5 posts from same user in a row
// Simple ranking formula
score = recency_weight * time_decay(post.created_at)
+ engagement_weight * log(post.likes + post.comments + 1)
+ affinity_weight * user_affinity(viewer, poster)
+ content_weight * content_type_boost(post.type)
// ML-based ranking (production systems)
// Training data: user interactions (like, comment, share, hide, time spent)
// Model predicts: P(user engages with post)
// Posts sorted by predicted engagement probability
// Pagination: cursor-based
// First page: top 20 by score
// Next page: cursor = last post's score → fetch next 20 below that scoreKey Points to Remember
- 1Fan-out on write: fast reads, expensive writes — good for users with few followers.
- 2Fan-out on read: cheap writes, slower reads — good for celebrities with millions of followers.
- 3Hybrid approach: fan-out on write for regular users, fan-out on read for celebrities.
- 4Feed ranking considers recency, engagement, relationship strength, and content type.
- 5Redis sorted sets are ideal for pre-computed feed caches — scored by timestamp or ranking score.
Interview Questions
Sign in to ask AriaWhat is the difference between fan-out on write and fan-out on read?
How does the hybrid fan-out approach handle the celebrity problem?
How would you rank posts in a news feed?
Design the feed generation system for a social network with 500M users.
How do you handle feed consistency when a user unfollows someone?
Ask Aria about Design: News Feed
Your personal AI tutor — ask anything about this concept
Revision Status
Personal Notes
Sign in to save personal notes for this topic.
Discussion
Sign in to join the discussion.