Design a News Feed — Fan-Out, Ranking, and Real-Time Updates
The feed is the product#
For social platforms, the feed IS the product. Instagram, Twitter, LinkedIn — users open the app to scroll the feed. If it's slow, stale, or irrelevant, they leave.
Designing a news feed combines data fanout, ranking algorithms, caching, and real-time updates into one system.
Two approaches: push vs pull#
Fan-out on write (push)#
When User A posts, immediately push the post to every follower's feed.
How: User A has 500 followers. On post creation, write 500 entries to 500 individual feed caches.
Pros: Reads are instant — the feed is pre-computed. Cons: Writes are expensive. Celebrities with 50M followers = 50M writes per post.
Fan-out on read (pull)#
When User B opens their feed, fetch posts from everyone they follow and merge them.
How: User B follows 200 people. Query each person's recent posts, merge, rank, return.
Pros: Writes are cheap — one write per post. Cons: Reads are slow — merging 200 timelines in real-time.
The hybrid approach (what everyone uses)#
- Regular users (< 10K followers): fan-out on write. Pre-compute feeds.
- Celebrities (> 10K followers): fan-out on read. Fetch their posts at read time and merge with the pre-computed feed.
This gives fast reads for 99% of users while avoiding the celebrity problem.
Ranking the feed#
Chronological feeds are simple but not engaging. Modern feeds use ranking:
Signals:
- Engagement prediction: ML model predicts probability of like, comment, share
- Relationship closeness: How often do you interact with this person?
- Content type: Do you prefer videos, images, or text?
- Recency: Newer posts get a boost, but great old posts can surface
- Diversity: Don't show 10 posts from the same person
Simple ranking formula:
score = engagement_probability × relationship_score × recency_decay × diversity_bonus
Feed storage#
Per-user feed cache (Redis):
feed:user123 → [post_id_1, post_id_2, ..., post_id_500]
Sorted set in Redis. Each entry is a post ID with the ranking score. Trim to last 500 entries.
Post storage (database):
posts:
id, author_id, content, media_url, created_at, like_count, comment_count
When rendering the feed: fetch post IDs from Redis, batch-load full post data from database/cache.
Real-time updates#
User is scrolling the feed. Someone they follow posts something new.
Options:
- WebSocket push: Server pushes "1 new post" notification. User clicks to load.
- Polling: Client checks every 30 seconds. Simple but wasteful.
- Long polling: Client holds connection open until new content exists.
Most apps use WebSocket for active users and push notifications for inactive users.
Scaling considerations#
| Component | Scale strategy |
|---|---|
| Feed cache | Redis Cluster, partition by user ID |
| Post storage | Shard by author, read replicas |
| Fan-out workers | Kafka consumers, horizontally scaled |
| Ranking service | ML model serving with batched inference |
| Media | CDN with image resizing |
See feed architecture#
On Codelit, search "Twitter" or "social network" in ⌘K to see complete feed architectures — fan-out service, timeline cache, ranking engine, and real-time sync.
Design your feed system: search "Twitter" on Codelit.io and explore the feed architecture.
Explore the Twitter architecture interactively
Try it →Try these templates
Uber Real-Time Location System
Handles 5M+ GPS pings per second using H3 hexagonal geospatial indexing.
6 componentsReal-Time Collaborative Editor
Notion-like document editor with real-time collaboration, conflict resolution, and rich media.
9 componentsNetflix Video Streaming Architecture
Global video streaming platform with adaptive bitrate, CDN distribution, and recommendation engine.
10 componentsContinue learning
Go deeper on system design
As an Amazon Associate I earn from qualifying purchases. Codelit may receive a commission at no extra cost to you.
Building Microservices
Sam Newman · 2021
600 pages on splitting a monolith: service boundaries, data decomposition, migration order.
Software Architecture: The Hard Parts
Neal Ford, Mark Richards, Pramod Sadalage, Zhamak Dehghani · 2021
Works through service granularity, distributed data and saga patterns as full trade-off case studies.
4.6 (731)#2 in Software Design ToolsDatabase Internals
Alex Petrov · 2019
Explains B-trees, LSM trees and consensus protocols so you can predict how a datastore will behave.
4.6 (560)#2 in Management Information SystemsIntroduction to Algorithms
Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, Clifford Stein · 2022
Full proofs and recurrence math behind the algorithms an interview only asks you to recognize.