Design a news feed
Balance fan-out on write and fan-out on read for a social timeline.
- Compare fan-out on write with fan-out on read.
- Handle celebrity accounts with a hybrid approach.
- Paginate a feed with cursors and store media efficiently.
Requirements. Users publish posts, follow other users, and view a home feed of posts from people they follow, newest first. Non-functional: loading the feed must be fast (it is the most frequent request), availability matters more than perfect freshness, and a few seconds of delay before a post appears is acceptable. That tolerance is a hint: the feed can be eventually consistent.
Push, pull, or both
- Fan-out on write (push): when someone posts, write the post id into a precomputed feed for each follower (for example a Redis sorted set per user). Reading a feed is one fast lookup, but posting costs one write per follower.
- Fan-out on read (pull): at read time, fetch recent posts from everyone the user follows and merge them. Posting is cheap; reading is slow and expensive.
- Hybrid: push for normal accounts; for celebrities with millions of followers, skip fan-out and merge their recent posts in at read time.
The scale of the problem: a post from someone with 2 followers costs 2 feed writes. A post from an account with 1 million followers costs 1,000,000.
The feed cache stores post ids, not whole posts; the feed service then hydrates ids from a post cache, so an edited post is fixed in one place. Fan-out runs asynchronously through a queue. Paginate with a cursor (“posts older than id X”) instead of an offset, so new posts do not shift pages and cause duplicates. Images and videos go to object storage served through a CDN, and posts store only their URLs. Ranking by relevance is a common extension.
Practice: compare fan-out strategies
Try it
Up to 15 XPPush, pull or both?
This network has 10 million normal accounts with 200 followers each, and 1,000 celebrities with 20 million followers each. Everyone posts once a day and follows 300 accounts, 5 of them celebrities.
Compare the three strategies. What does one celebrity post cost with push? What does one feed load cost with pull?
- Feed writes a day
- 22,000,000,000
- Writes for the biggest single post
- 20,000,000
- Timelines merged per feed load
- 1
Reads are as cheap as they can be, but one celebrity post creates a storm of writes.
Put it together: a mock design
Try it
Up to 15 XPPresent a design out loud
Talk through your design as you would in the room. Name the hard problem before the interviewer does.
“Design the home feed for a social network.”
Testing: Fan-out
The question stays hidden until you start. Answer it out loud as you would in the room.
Key takeaways
Push makes reads cheap; pull makes writes cheap; the hybrid handles celebrities.
Cache post ids per feed and hydrate them from a post cache.
Use cursor pagination and serve media from object storage through a CDN.
Lesson quiz
6 questions · pass with 5 correct · up to 50 XP
Passing this quiz completes the lesson and keeps your streak going. Questions you miss come back in review sessions later.
Questions about this lesson
Stuck? Ask. Figured something out? Share it. Explaining is one of the best ways to learn.
Loading posts…