Design a news feed

  • Lesson 39 of 40
  • 14 min
  • Intermediate
  • 12 steps

A feed looks like a simple query: posts from the people I follow, sorted by date. At scale the real question is different: do you do the work when someone posts, or when someone reads?

Feed load
60 ms
Lookups per feed
1 list + a few
Writes per post
≈ 70 · active only
Post arrives in
≈ 2 s
feed readersFeed APIFeed APIFeed cacheone list per userFeed cacheQueueQueueFan-outworkersFan-out workersfollow graphposts · Aposts · Bposts · CLucía poststo be designedRequirementsposttext and a photohomeposts from people I followopen the feedunder 200 msnew postmay take a few secondsQuick math200 M users × 10≈ 23,000 feeds/s50 M posts a day≈ 600 posts/sreads per post≈ 40accounts followed200 on averageexample figuresmerges 200 timelines97%97%97%What pull costsfeeds per second23,000lookups per feed× 200lookups per second4.6 Mand every feed waits for the slowest shardfan-out jobmateo → [7310, 7302, 7288 …]hydrate: 20 posts in 1 batch100%jam: 50 M deliveriesmy list + 3 famouspull famous accountsfamous: no fan-outactive in 30 days onlyidle lists expireCursor paginationGET /feed?limit=20→ posts 7310 … 7288next: before=7288GET /feed?before=7288&limit=20stays put while new posts arriveempty · warming uprebuild on open · max 500 at onceDecisionsbuild the feedon write (push)40 reads per postfamous accountspull on read50 M writes per postlistsids, cappedone copy of each postidle usersno fan-outunused memoryscrollingcursorsnew posts shift pages
  • request
  • response
  • event
  • batch
  • waiting in queue
  • slow request
  • cache miss

Step 1 of 12

Concept

The brief: a social network's home screen

Another full design. On this social network people follow other accounts, and the home screen lists recent posts from all of them, newest first.

The demands: the feed must open in under 200 ms. A new post may take a few seconds to show up for followers, and it does not have to show up for all of them at the same moment. Eventual consistency is enough here: a post arriving five seconds late hurts nobody.

Step 2 of 12

Concept

Put numbers on it

Assume 200 million daily users opening the feed about 10 times each: 2 billion loads a day, around 23,000 a second on average. Together they publish 50 million posts a day, around 600 a second. Every figure in this lesson is an example.

For every post written there are about 40 feed reads. And a typical user follows 200 accounts; since every follow has one follower and one followed, the average number of followers per account is also 200.

What changed

  • 23,000 feeds/s · 600 posts/s
  • 200 followed accounts on average

Step 3 of 12

Solution

First attempt: build the feed on request

Keep posts in a table indexed by (author_id, created_at), sharded by author. For Mateo's feed: look up the 200 accounts he follows, fetch the latest posts of each, sort them by date and return the first 20.

This is the pull model, or fan-out on read. Posting costs the minimum, a single insert, and the post shows up in the next feed anyone opens.

What changed

  • Posts sharded by author
  • Feed = 200 lookups, merge, keep 20

Step 4 of 12

Problem

The work lands on the wrong side

Every feed load queries 200 authors spread across several shards and cannot answer until the slowest reply arrives: the fan-out tail you saw in Latency and throughput.

The total weighs even more: 23,000 feeds/s × 200 lookups = 4.6 million lookups a second, almost all of them recomputing what that person saw a minute ago. The expensive work sits on the path that runs 40 times more often.

What changed

  • 23,000 feeds/s × 200 = 4.6 M lookups/s
  • Post shards saturated
  • Feed load: 600 ms → 1.4 s

Step 5 of 12

Solution

Turn it around: deliver the post when it is written

Every user gets a ready-made feed: a list in a cache, newest post first. When Lucía posts, a fan-out worker looks up who follows her and adds the post to each follower's list. This is the push model, or fan-out on write.

Opening the feed becomes one read of one list. The fan-out runs behind a queue (as in Message queues): Lucía gets her answer right away and her followers see the post a second or two later.

What changed

  • Feed cache: one list per user
  • Queue and fan-out workers
  • Follower lookup
  • Feed load: 200 lookups → 1

Step 6 of 12

Technique

Store ids, not posts

Each list keeps only post ids, 8 bytes each, and only the ~800 most recent. On read, the service asks for the page's 20 posts at once, in parallel to the shards: that is hydration.

Since each post lives in a single place, editing or deleting it is one change. A deleted post simply does not come back when hydrating; nobody has to chase it through millions of lists.

What changed

  • Lists hold post ids, at most 800
  • Posts fetched in one batch on read
  • Edit or delete: in one place

Step 7 of 12

Problem

Case: fifty million followers

A singer with 50 million followers posts. That one post turns into 50 million cache writes. Even at 100,000 writes a second, delivering it takes 500 s, over 8 minutes, and meanwhile everyone else's posts wait behind it in the queue.

It is the hot key you saw in Sharding: one key far heavier than the rest overwhelms a design built for the average case.

What changed

  • An account with 50 M followers posts
  • 50 M writes for a single post
  • The jammed queue delays everyone

Step 8 of 12

Solution

Hybrid: push for almost everyone, pull for the famous

Treat each kind of account differently. Regular accounts keep fan-out on write. Accounts above a threshold, say 100,000 followers, get no fan-out.

When Mateo opens his feed, the service reads his ready-made list, fetches the recent posts of the few famous accounts he follows (each one a single timeline, heavily read and always cached) and merges the two. A few extra lookups per read instead of 50 million writes per post.

What changed

  • Over 100,000 followers: no fan-out
  • Their posts are merged on read
  • Posts arrive in ≈ 2 s again

Step 9 of 12

Trade-off

Case: feeds nobody reads

Of Lucía's followers, maybe only a third opened the app this month. Fanning out to the rest fills memory with lists nobody will look at.

So skip them: fan out only to people active in the last 30 days and let the other lists expire. With 200 followers on average, that leaves about 70 writes per post. In exchange, someone returning after weeks waits a little longer the first time: their feed is built once with pull, cached, and push carries on from there.

What changed

  • Fan out only to active followers
  • Idle lists expire
  • Returning users: feed built by pull, once

Step 10 of 12

Technique

Scrolling: cursors, not page numbers

Mateo scrolls down for more. «Page 2» would mean «skip 20, give me 20», but since he loaded the first page three new posts arrived at the top: he would see three posts twice.

Use a cursor (as in REST, RPC and GraphQL): «give me 20 posts older than 7288». The position is tied to a post, not to a count, so whatever arrives at the top cannot shift it.

What changed

  • GET /feed?before=7288&limit=20

Step 11 of 12

Concept

Case: the feed cache is wiped

A faulty deploy wipes the feed cache. Every list is gone and yet nothing important is lost: posts and the follow graph are the source of truth; the lists are derived data that can be computed again.

The risk is a stampede: millions of users rebuilding by pull at the same time. So a feed is rebuilt only when someone opens the app, with a cap on rebuilds running at once (500 in the example), and a shorter feed is served while the cache warms up.

What changed

  • Feed cache wiped
  • Feeds rebuilt on open, from posts and follows
  • Rebuilds capped to protect the posts

Step 12 of 12

Summary

The finished design

Every decision comes from a figure: the feed is built on write because it is read 40 times more; famous accounts are read by pull because 50 million writes per post do not fit; lists hold ids so each post exists only once; idle users get no fan-out because their list would take memory nobody uses; and scrolling uses cursors because new posts shift the pages.

A feed ranked by relevance adds one stage (score the candidates before returning them), but the plumbing underneath is the same.

You will learn

  • Estimate the reads and writes of a feed
  • Fan-out on read versus fan-out on write
  • Why celebrity accounts break push and how a hybrid fixes it
  • What to keep in each list, and for whom
  • Cursors and rebuilding derived data