Open Instagram, LinkedIn or X and a personalised list of posts appears in under a second. “Design a news feed” is a frequently reported question at Meta, X and LinkedIn, and it's famous for one trade-off: do you do the work when someone posts, or when someone reads? The answer, “it depends who's posting”, is the heart of the design.
Step 1 · Requirements
- Functional: create posts (text, photos, videos); follow people; see a feed of posts from people you follow; like and comment; infinite scroll.
- Non-functional: feed loads fast (a few hundred ms); new posts appear within seconds to a minute (eventual consistency is fine); highly available; read-heavy.
- Out of scope (say it): stories, ads, search, direct messages (that's the WhatsApp design).
Step 2 · Estimates
- Users: 300 million daily active; each opens the feed 10 times a day = 3 billion feed loads/day ≈ 35,000/s (70,000 at peak).
- Posts: 30 million posts a day ≈ 350/s. With ~200 followers on average, push means ~70,000 feed writes/s.
- Feed cache: keep the latest ~500 post IDs per active user × 8 bytes × 300M users ≈ 1.2 TB across a Redis cluster.
- Media: 10 million photos/day × 500 KB ≈ 5 TB/day into object storage.
Step 3 · API
POST /posts { text, mediaIds[] } → { postId }
GET /feed?cursor=<postId>&limit=20 → { posts[], nextCursor }
POST /users/{id}/follow
POST /posts/{id}/likeStep 4 · Design
Push or pull? The central trade-off
🎮 Push, pull or hybrid?
Who posts?
Feed strategy
✅ Pushing to 300 feeds is cheap (under a second), and every follower's feed loads instantly.
Try the film star with each strategy. That one case, the celebrity problem, is why Instagram- and Twitter-style feeds use a hybrid.
| Push (fan-out on write) | Pull (fan-out on read) | Hybrid | |
|---|---|---|---|
| Posting cost | 1 write per follower | 1 write | Push for normal authors only |
| Feed load | Read 1 list: fast | Merge many timelines: slow | Fast list + merge a few stars |
| Celebrity posts | Millions of writes ❌ | Fine | Pulled at read time ✅ |
| Inactive users | Wasted writes | No waste | Skip pushing to long-inactive users |
The pull side is a k-way merge: each followed account's posts are already sorted newest-first, so a max-heap picks the newest across all of them, the same technique as merging sorted lists in DSA:
import heapq
# Each followed account's posts, newest first: (timestamp, post_id)
timelines = {
"riya": [(1700, "riya-3"), (1500, "riya-2"), (1100, "riya-1")],
"arjun": [(1650, "arjun-2"), (1200, "arjun-1")],
"meera": [(1800, "meera-4"), (1600, "meera-3"), (1300, "meera-2")],
}
def build_feed(following, k):
"""Fan-out on read: merge the newest posts of everyone you follow (k-way merge)."""
heap = [] # max-heap by time, using negative timestamps
for user in following:
posts = timelines[user]
if posts:
ts, pid = posts[0]
heapq.heappush(heap, (-ts, pid, user, 0))
feed = []
while heap and len(feed) < k:
neg_ts, pid, user, i = heapq.heappop(heap)
feed.append(pid)
if i + 1 < len(timelines[user]): # push that user's next-newest post
ts, nxt = timelines[user][i + 1]
heapq.heappush(heap, (-ts, nxt, user, i + 1))
return feed
print(build_feed(["riya", "arjun", "meera"], k=5))
# ['meera-4', 'riya-3', 'arjun-2', 'meera-3', 'riya-2']Data model
posts (post_id PK, author_id, text, media_urls, created_at) -- sharded by post_id
follows (follower_id, followee_id) -- both directions indexed
feed:{uid} → Redis list of the latest ~500 post IDs -- the feed cache
media → object storage (photos, videos), served via CDNStep 5 · Scale and reliability
- Caching everywhere: feed lists, post objects, user profiles and like counts each have their own cache. A feed page is ~20 cache reads, rarely a database query.
- Async fan-out: the post request returns immediately; workers fan out from a queue. A delay of a few seconds is acceptable.
- Skip inactive users: don't push to people who haven't opened the app in weeks; build their feed by pulling when they return.
- Counters: likes on viral posts are hot keys. Count in memory, batch updates, and show approximate numbers (“1.2M”).
- Ranking: fetch a few hundred candidates, score them with a model (closeness to author, engagement, freshness), return the top 20.
- Media: upload straight to object storage with a pre-signed URL; serve resized versions from a CDN.
🧠 PATH memory card
Remember the news feed design for good
Picture it · Anchor it · Trace it · Hit-recall it. Four quick steps that turn “I read it once” into “I can explain it in an exam”.
- P
Picture it
Your brain remembers scenes far better than definitions.
← swipe to see the whole picture →
Every hostel room has its own notice board. When a friend writes a note, a helper pins a copy on each of their friends' boards (push). A film star's notes go on one big board in the lobby, and you check it when you look at yours (pull). Your board shows the most interesting notes first.
- A
Anchor it
A silly sentence you can't forget, where every word maps to the concept.
“Push for friends, pull for stars”
- Push for friends→Fan-out on write: add the post ID to each follower's feed cache
- Pull for stars→Fan-out on read for celebrity accounts
- Notice board→Feed cache: a list of post IDs per user
- Best on top→Ranking model over candidate posts
- Keep reading→Cursor pagination: posts older than the last seen
- T
Trace it
Run it by hand on a tiny example once. Doing beats reading.
Riya (300 followers) posts → saved as post 9001 → queue → workers add9001to 300 feed lists. A film star posts post 9002 → saved only. You open the app: read your list[9001, 8990, …], pull the star's latest[9002], merge with the heap, hydrate details from caches, rank → show 20 posts withnextCursor = 8950. - H
Hit-recall it
Test yourself now, then again on the day each card comes back.
Card 1 / 8 · new0 mastered · 0 practisedSay the answer out loud first, then flip and be honest.
Follow-up questions interviewers ask
- “A user unfollows someone?” Filter that author out at read time; clean the cached list lazily.
- “A post is deleted?” Mark it deleted; hydration drops it, so it vanishes from every feed without touching millions of lists.
- “Real-time new posts?” Show a “new posts” pill via a WebSocket or periodic check rather than reshuffling the feed under the user's thumb.
Where to go next
Feeds update within seconds. Chat needs milliseconds and must never lose a message. Next, the real-time classic: design WhatsApp.
Key takeaways
- ✓Store posts once; keep a per-user feed cache of post IDs.
- ✓Push (fan-out on write) for normal authors, pull (fan-out on read) for celebrities: a hybrid.
- ✓Fan-out runs asynchronously from a queue; skip inactive users.
- ✓Hydrate posts from caches, rank candidates, and page with a cursor.
- ✓Media goes to object storage and is served from a CDN.
Frequently asked questions
How does a news feed work?
When someone posts, the post is saved and its ID is added to the feeds of their followers (or fetched later when followers open the app). Each user's feed is a list of post IDs kept in a fast cache. When you open the app, the feed service reads your list, loads the post details, likes and author info from caches, ranks them, and returns the first page. Scrolling asks for the next page with a cursor.
What is fan-out on write vs fan-out on read?
Fan-out on write (push) copies a new post's ID into every follower's feed when it's posted, so reading a feed is a single fast lookup but posting costs one write per follower. Fan-out on read (pull) stores the post once and builds each feed when it's opened by merging recent posts from everyone the user follows, so posting is cheap but reading is expensive.
What is the celebrity problem in news feed design?
With fan-out on write, an account with millions of followers would trigger millions of feed writes for every post, which is slow and overloads the system. The common fix is a hybrid: push posts from normal accounts, but pull posts from celebrity accounts at read time and merge them into the pre-built feed.
How are news feed posts ranked?
Simple feeds are reverse-chronological (newest first). Modern feeds score each candidate post with a ranking model using signals like how often you interact with the author, the post's likes and comments, its type (photo, video), and how recent it is, then sort by score. The system design is the same: gather candidates, score them, return the top ones.
Why use cursor pagination for a feed?
Feeds change constantly. With page numbers (offset), new posts arriving at the top shift everything down, so the next page repeats or skips posts. A cursor says 'give me posts older than this ID', which stays stable as new posts arrive and is also faster for the database.
🧠 Quick check: news feed
1. With fan-out on write, what happens when a user with 300 followers posts?
2. Why not push a film star's post to 20 million feeds?
3. Where are photos and videos stored?
4. New posts keep arriving while you scroll. Which pagination avoids duplicates?
Checking your access…