News Feed System Design
The interviewer's prompt sounds familiar: "Design the home feed for a photo-sharing social app, like Instagram's feed or Facebook's News Feed. Users follow other accounts and post photos with captions. When someone opens the app, they see a ranked, infinitely scrollable feed of recent posts from the accounts they follow."
Everyone can write the first version. It's one query: fetch the posts whose author is in my follow list, newest first, twenty at a time. It works for a demo with a hundred users. Then the app has 500 million people opening it ten times a day. Every open now merges two hundred timelines, and one slow fetch in two hundred makes the whole page slow. So you flip it and precompute every feed when a post is written. That works until a singer with 80 million followers posts a photo, and one post becomes 80 million writes.
The Trap Built Into a Familiar Question
Two failures come up again and again:
- The Timeline Joiner: builds every feed at read time with one query over the follow list. At 175,000 feed opens a second, that's 35 million timeline reads a second, and about 87% of opens wait on at least one slow fetch. Signal: hasn't noticed that the read path is the product.
- The Broadcaster: precomputes every feed at write time by copying each post into every follower's feed. That's fine for a post with 200 followers. A post from an account with 80 million followers ties up the whole fan-out fleet for 80 seconds, and an awards night with ten such posts leaves a 13-minute backlog for everyone. Signal: designed for the average account and forgot the distribution has a tail.
The passing answer is a hybrid, chosen by arithmetic. Posts from ordinary accounts are pushed, as IDs only, into the precomputed feeds of followers who've been active in the last 30 days. Posts from accounts above 100,000 followers are never pushed. They're pulled from a small celebrity cache and merged in when the feed opens. A light ranking model scores about 500 candidates in 50 milliseconds, and the ranked list is saved for the session so scrolling never shows a post twice. Deletes, blocks and private accounts are checked when a post is shown, not when it's copied, so a stale copy can never leak a post.
Sign in to continue reading
The rest of this lesson is available with a free account. Signing in with Google or Microsoft is free.
Sign in to read the full lesson