Design a news feed (like Twitter or Instagram)
Show each user a feed of recent posts from the people they follow.
Why interviewers ask it
It tests the fan-out trade-off — do the work when posting or when reading — plus caching and the celebrity problem.
Functional requirements
- Create posts; follow and unfollow users.
- View a personalised feed (newest first, or ranked), with pagination.
Non-functional requirements
- The feed loads fast (under about 200 ms); very read-heavy.
- Eventual consistency is fine: a post can appear a few seconds later.
From version 1 to the final design
- Version 1: fan-out on read: query the posts of everyone you follow, sorted by time.
- Version 2: fan-out on write into per-user feed lists in Redis.
- Version 3: a hybrid that handles celebrities differently.
- Version 4: ranking, media through a CDN, and skipping inactive users.
The design, step by step
- Fan-out on write (push): when someone posts, add the post ID to each follower's feed list. Reads are fast.
- Fan-out on read (pull): build the feed at request time from the followed users' recent posts. Writes are cheap; reads are slow.
- Hybrid: push for normal users; for celebrities with millions of followers, pull their posts at read time and merge.
- Post content comes from a cache and media from a CDN; a ranking service can reorder the feed.
Deep dives
- The celebrity problem: pushing to 50 million followers for every post is far too expensive → use the hybrid.
- Each user's feed list keeps only the latest few hundred post IDs.
- Skip pushing to inactive users; build their feed when they log in.
- Ranking: gather candidates → score them with a model → re-rank for variety.
Common mistakes
- Push-only fan-out that ignores celebrities: one post from an account with millions of followers means millions of writes.
- Storing whole posts in every feed instead of post IDs.
- Offset pagination on a feed that keeps changing.
Not true: Fan-out: push each new post to the followers' feeds, for celebrity accounts too.
In fact: One post from an account with 50 million followers would mean 50 million writes. Push for ordinary accounts, and fetch celebrity posts when the feed is read.
In the interview
I'd use a hybrid fan-out. When a normal user posts, workers push the post ID into each follower's feed list in Redis, so reading is a fast cache lookup. Celebrity posts aren't fanned out; their recent posts are fetched at read time and merged in. Post bodies come from a cache and media from a CDN.
Part of Unit 8: High-level design questions in the free System Design course.