Back to blog

Five million home feeds: how do you build the page everyone opens?

Thắng Đoàn
Thắng Đoàn

FotoFlow added following, and the home screen now shows recent posts from everyone you follow, newest first. Five million daily users open it about ten times each, and the requirement is blunt: the feed must feel instant, under 200 ms. A new post may reach followers a few seconds late, and nobody is harmed. Deciding what does not have to be immediate is what makes the design possible.

Numbers first

Five million users, ten opens a day: about 580 feed loads a second. They write 50,000 posts a day, roughly one every two seconds. Reads outnumber writes about 500 to 1, and a typical user follows 200 accounts. Those two numbers, 200 sources and reads dominating, drive everything next.

Build it on read, or on write

The first draft is pull, or fan-out on read: posts sit in a table indexed by (author_id, created_at), and opening the feed looks up the 200 followed accounts, fetches each one's latest posts, merges by time and returns the top 20. Posting stays one insert, but every feed load now fans out to 200 authors and waits for the slowest: 580 loads a second times 200 lookups is over 100,000 lookups a second, mostly recomputing what the same person saw a minute ago. When reads far outnumber writes, move the work to write time.

Flip it: push, or fan-out on write. Each user has a ready-made feed list in a cache, and when Ana posts, a worker behind a queue adds the post id to each follower's list. Opening the feed becomes one read of one list. The lists store only post ids, the newest 800 or so, and the posts themselves are fetched in one batch at read time, so an edit or delete happens in one place instead of being chased through millions of lists.

The celebrity problem, and the hybrid

A pop star with 50 million followers posts one photo. One post becomes 50 million cache writes; even at 100,000 writes a second, delivery takes over eight minutes while everyone else's posts wait behind it in the queue. Averages hide this: the typical account has 200 followers, a few thousand have millions, and they post at the busiest moments. Neither pure strategy survives real data: large designs end up hybrid, split along the skewed dimension: ordinary users get push, accounts above about 100,000 followers are not fanned out at all. When Alice opens her feed, the API reads her list and also pulls the latest posts of the handful of celebrities she follows, then merges.

Two more refinements. A third of Ana's followers have not opened the app this month; fanning out to them fills memory with lists nobody reads, so push only to followers active in the last 30 days and rebuild a lapsed user's feed by pull when they return. And scrolling uses a cursor, "20 posts older than post 9017," not page numbers: three new posts at the top would shift "skip 20, take 20" and show repeats.

What this does not solve

A bad deploy flushes the feed cache, and every list is gone. Know which of your data is the truth and which is a convenience: posts and the follow graph are the truth, feed lists are derived data that can be rebuilt. The danger is the rebuild stampede, so rebuild lazily when a user actually opens the app, cap how many rebuilds run at once, and serve a thinner feed until the cache warms. A ranked feed, scoring candidates before returning them, adds one stage on top of the same plumbing, which is also the honest limit: this design delivers recency, not relevance.

Your feed shows posts from 200 accounts. Which 10 of them would you actually notice were missing?
The cache holding every user's feed is wiped right now. What is the user experience over the next ten minutes?

Feeds tolerate lateness; chat does not. Next in the series: Ten million users want direct messages: could you design the chat system?

Sources

Instagram Engineering: Feed

Netflix Technology Blog

Apache Kafka

Share:

Recommended for you

Enjoyed this article?

Subscribe for new articles. No spam. Unsubscribe anytime.

No pasting. This is the challenge.0/200

Typed live, never pasted.