ealeng

Episode 36 of 55 · Real-time

News feed fan-out: one celebrity post wrote 30,000 rows and 58 ms, and the hybrid wrote 0.2 ms

A feed for 50,000 users and 2.44 million follows, built three ways in Postgres. Push (fan-out on write) copies each post into every follower's feed: 77.7 rows per post on average, and 58 ms and about 30,000 rows for one celebrity post. Pull (fan-out on read) stores each post once and merges at read time: one row per post, and reads that were about as fast here. The hybrid pushes below a follower threshold and pulls the big accounts: 42.1, 29.4 or 16.8 rows per post, with 8, 20 or 33 authors pulled per read. The threshold is a price comparison between those two costs.

Lab 15 · Real-Time Delivery and Fan-out10 min read
Video transcript8:12

0:00Cold open

0:01

I built a news feed for fifty thousand users, and copied every post into each follower’s feed.

0:08

One post by a celebrity wrote about thirty thousand rows, one database record per follower. It took fifty-eight milliseconds.

0:18

Twenty-one big accounts wrote forty-six percent of all the rows, mostly because the test had the eight biggest post about seven times as often.

0:29

So I tried the other way: store each post once, and fetch it when someone opens their feed. Opening a feed took under half a millisecond, either way.

0:40

So why do the big social apps end up doing both?

0:46Intro

0:47

Hands-on system design, part thirty-six.

0:51

Today: fan-out, spreading one post to many feeds. On write, on read, and the hybrid that handles celebrities.

1:00Meet Lucía

1:01

I’m Lucía, a platform engineer.

1:04

The follower graph is generated: fifty thousand users, each following forty-nine accounts on average.

1:12

Most accounts are small: the typical one has eleven followers. And five planted celebrities have about thirty thousand each, sixty percent of everyone.

1:23

Here’s how follower counts spread out. Twenty-one accounts have ten thousand followers or more: the five planted celebrities, three more with over twenty thousand, and thirteen between ten and twenty thousand.

1:38

Think of a town’s mail. A post is a letter to everyone who follows you. Your feed is your mailbox.

1:4701 · What you need

1:48

To run it yourself: Docker, for a Postgres database and the client that posts and reads. The quick tests run in a couple of seconds, without Docker.

1:59

No cloud, and nothing to pay for.

2:0402 · Three ways

2:05

There are three ways to build a feed. The question each time: does the work happen when someone posts, or when you read?

2:14

Push, also called fan-out on write: copy each post into every follower’s feed. In the mail picture, a copy in every mailbox. Reading is then one ordered list.

2:26

Pull, or fan-out on read: store each post once. When you open your feed, merge the newest posts of everyone you follow. Like walking past each author’s notice board.

2:39

And the hybrid: push for authors below a threshold of followers. The big accounts are stored once, and you fetch their posts when you read.

2:5003 · Writing

2:51

First, push everything: two hundred thousand posts by random users, plus extra posts by the biggest accounts.

3:00

Each post wrote seventy-seven point seven rows on average: one for the post, and one per follower. That’s close to what the graph predicted. The small gap is because the extra posts went to eight accounts, not the five the model assumed.

3:17

A celebrity’s post wrote about thirty thousand rows, and took fifty-eight milliseconds.

3:24

And the feed table, one row per follower per post, grew to about one point seven gigabytes.

3:3204 · Who wrote it

3:34

From here on, the celebrities are the eight accounts with over twenty thousand followers. So how much of all the writing came from the twenty-one accounts with ten thousand or more?

3:46

Forty-six percent.

3:50

Mostly because the test had the eight biggest post about seven times as often as everyone else. If they had posted at random, it would have been sixteen percent.

4:0205 · In code

4:03

In code: when someone posts, check whether the author has fewer followers than the threshold. If so, push.

4:11

Pushing is one statement: insert a feed row for every follower of the author.

4:18

One statement, but the database still writes one row per follower. For a celebrity, thirty thousand.

4:2706 · The hybrid

4:28

Now the hybrid: push only for authors with fewer than ten thousand followers. The twenty-one big accounts are stored once.

4:37

Rows per post fell from seventy-eight to forty-two. And a celebrity’s post took a fifth of a millisecond: it’s one row now.

4:47

The feed table shrank by almost half. Lower thresholds push less again: twenty-nine rows below a thousand, seventeen below a hundred.

4:58

And pulling everything: one row per post, and an almost empty feed table. So far, pull wins or ties on every number. The cost is supposed to show up when you read.

5:1207 · Reading

5:13

Two thousand feed reads, the newest fifty posts each, by random users. Under pull, each read fetches the posts of about forty-nine authors. Under push, it reads one list.

5:26

Push: a quarter of a millisecond, typically. Pull: a third of a millisecond. The hybrids were a little slower, because they run two queries and merge them. The textbook difference barely showed up.

5:41

That’s a finding about the setup. Here it’s one database, and the data is small: each author had only a few posts, so a pull read picks fifty out of a few hundred. Finding them is a quick search through sorted lists, inside one process.

5:57

In a large system, posts are split across many machines, so each author you follow can be a network call. That part, this test didn’t measure.

6:0808 · In code

6:10

Reading a pushed feed is one query: your own rows, newest first, fifty of them.

6:16

Pulling is one query too, but inside it, the database finds the newest posts of each account you follow. On one machine, that’s fast. Across many, each one can be a trip.

6:3009 · The threshold

6:31

So where does the threshold go? It’s a price comparison. Take a lower threshold, a thousand followers: each post costs twenty-nine rows, and each read fetches about twenty authors.

6:44

Multiply rows by what a row costs you, and fetched authors by what a fetch costs, times how many times each post is read. Pick the smaller total.

6:56

And the celebrities make it urgent. At this laptop’s rate, about two microseconds a row, pushing one post to fifty million followers would take about a hundred seconds of database time, if it scaled in a straight line.

7:11

Reads usually far outnumber posts, and a fetch across a network is expensive. So big feeds often push for nearly everyone, and pull just the few biggest accounts.

7:25Back to Lucía

7:26

So, three lessons.

7:29

Push makes every post cost one row per follower. That’s cheap for most accounts, and huge for a celebrity.

7:37

The hybrid pushes the many small accounts, and stores the few big ones once.

7:44

And measure the read side where it really runs. On one machine, fetching forty-nine authors cost about the same as reading one list.

7:54

Next time: serverless functions.

7:57

For today: push the many, pull the few.Lucía: Push the many, pull the few.

8:01Summary

8:03

Push costs a row per follower, the hybrid pulls the big accounts, and the threshold is a price you measure.

Key takeaways

  • ✓Push, fan-out on write, copied each post into every follower's feed: 200,200 posts wrote 15.6 million rows, an average of 77.7 per post, close to the 79.8 the follower graph predicted.
  • ✓One celebrity post wrote about 30,000 rows and took 58 ms at the median, across 235 celebrity posts, because the database writes one row per follower; with a threshold of 10,000 followers the same kind of post wrote one row and took 0.19 ms.
  • ✓Pull, fan-out on read, stored each post once: 1 row per post and an 8 KB feed table, against 1,654 MB for push, and each read then merged the newest posts of about 49 authors.
  • ✓Every strategy read a feed in under half a millisecond at the median (0.26 ms for push, 0.31 ms for pull), because on one database with a small dataset, merging 49 authors costs about what reading one list costs; the lab did not test a setup where that merge crosses a network.
  • ✓The hybrid trades write rows for read fan-in: a threshold of 10,000 followers gave 42.1 rows per post and 7.9 authors pulled per read, 1,000 gave 29.4 and 20.4, and 100 gave 16.8 and 33.1 (the pulled-author figures are the model's prediction).
  • ✓The threshold is a price comparison: rows per post times what a row costs, plus authors pulled per read times what a remote fetch costs, times how often each post is read; the lab measured the write side and the model predicted the read side.

A feed is a list of posts from the people you follow. Building it means choosing when the work happens: when someone posts, copying the post into every follower's feed, or when someone opens their feed, collecting the posts of everyone they follow. This chapter builds the feed both ways, and a hybrid, in Postgres, and measures what each one costs.

The examples come from Lab 15 (Real-Time Delivery and Fan-out) of the system design labs, script 04_feed.py, which runs the same workload once per strategy. The lab's follower graph comes from feed_model.py, which generates 50,000 users and 2.44 million follows, with most accounts having a handful of followers and five planted celebrities with about 30,000 each. The run used the same laptop setup as the earlier chapters, an Apple M4 Max with Docker Desktop and 16 virtual CPUs, and each strategy is one recorded run. The lab's README puts the script at about seven minutes.

Two ways to build a feed

The question for every design is: does the work happen when someone posts, or when someone reads?

  • Fan-out on write, also called push. When an author posts, copy the post into every follower's feed. Each follower then has a ready list, and reading it is one query. The mail picture is a copy in every mailbox.
  • Fan-out on read, also called pull. Store each post once, under its author. When a follower opens their feed, fetch the newest posts of every author they follow and merge them. The picture is walking past each author's notice board on the way home.

In the lab, a "row" is one database record. Under push, a post is one row for the post itself plus one feed row per follower, and the lab counts those feed rows as the write cost. Under pull, a post is one row, and the cost moves to the read.

The lab also has a third option in between, the hybrid, covered below. The three differ only in where the feed rows are written.

What a celebrity costs under push

Push makes the cost of a post proportional to the author's follower count. For most accounts that is cheap. The graph's typical account has about eleven followers, so a post writes about a dozen rows. For a celebrity it is not cheap: the five planted celebrities each have about 30,000 followers, which is 60% of the 50,000 users, so a post by one of them writes about 30,000 rows.

The lab measured this directly. Under push, the 235 celebrity posts in the run each took 58 ms at the median. The ordinary posts, 199,965 of them, took 0.32 ms at the median and 2.14 ms at the 99th percentile. The celebrity cost is the same mechanism as the ordinary one, repeated 30,000 times in one statement: insert a feed row for every follower of the author. One statement does not make it one write.

The feed table, one row per follower per post, grew to 1,654 MB in the lab. That is the storage side of the same cost.

The hybrid: push for most, pull for the very large

The hybrid picks a threshold, a follower count T. An author with fewer followers than T is pushed. An author with T or more is not pushed: the post is stored once, and their posts are fetched when a follower reads. When a post is written, the code only checks whether the author is above the threshold, and then either inserts feed rows or does nothing.

The lab tried thresholds of 10,000, 1,000 and 100 followers, plus the two extremes: a threshold of infinity (push everyone, which is pure push) and a threshold of 0 (push no one, which is pure pull).

Try it · Where the threshold goesrecorded run, 04_feed.py

Push below 1,000 followers. A post by an author above the threshold is stored once and fetched when a follower reads.

Rows per post, of 77.7
Rows per post
29.4
Celebrity post
0.19 ms
Authors pulled per read
20.4
Read p50
0.34 ms
Read p99
1.71 ms
Ordinary post p99
1.46 ms
Total rows written
5,886,605
Timeline table
589 MB

200,200 posts (200 by celebrities), then 2,000 feed reads of the newest 50, for 50,000 users and 2,440,000 follows. One run per strategy. Authors pulled per read is the model's prediction, not a measurement.

With a threshold of 10,000, the 21 accounts with 10,000 or more followers were not pushed. A celebrity post then wrote one row and took 0.19 ms, and the feed table shrank to 870 MB. A lower threshold pushes less again: 589 MB at 1,000 and 319 MB at 100.

The price is on the read side. Every author above the threshold has to be fetched when a follower opens their feed. The model predicts about 7.9 such authors per read at 10,000, 20.4 at 1,000 and 33.1 at 100, against 48.8 for pull and 0 for push.

The workflow and the results

The 04_feed.py script builds the graph, sets the threshold, writes 200,200 posts and then makes 2,000 feed reads for random users, reading the newest 50 posts each time. It runs this once per strategy, and it saves its results to data/results/04_feed.json. The script also computes the model's prediction for each threshold, so the write costs can be compared with what the graph implies.

Strategy Rows per post Predicted Post p50 / p99 (ordinary) Celebrity post Authors pulled per read (predicted) Read p50 Read p99 Timeline table
Push all (threshold infinity) 77.7 79.8 0.32 / 2.14 ms 58 ms 0 0.26 ms 2.32 ms 1,654 MB
Hybrid, push below 10,000 42.1 41.9 0.30 / 1.70 ms 0.19 ms 7.9 0.42 ms 1.38 ms 870 MB
Hybrid, push below 1,000 29.4 29.4 0.32 / 1.46 ms 0.19 ms 20.4 0.34 ms 1.71 ms 589 MB
Hybrid, push below 100 16.8 16.7 0.32 / 0.90 ms 0.20 ms 33.1 0.45 ms 0.95 ms 319 MB
Pull all (threshold 0) 1 1 0.19 / 0.44 ms 0.20 ms 48.8 0.31 ms 1.05 ms 8 KB

Three things stand out.

  • The write cost matched the graph. Push wrote 77.7 rows per post where the model predicted 79.8, within about 3%. The gap is small and comes from how the celebrity posts were spread, covered in the caveats.
  • Celebrity posts were 38% of the rows. The 200 celebrity posts were 0.1% of the 200,200 posts but wrote 38% of the 15.6 million rows. The 21 accounts with 10,000 or more followers wrote 46% of all the rows, mostly because the test had the eight largest accounts post about seven times as often as the others. If everyone had posted at random, those 21 accounts would have written 16%. That figure comes from the video's own check of the same run, not from the lab's results file.
  • Reads did not separate. Every strategy's read p50 was between 0.26 and 0.45 ms, and the 99th percentile was between 0.95 and 2.32 ms. Pull was not slower than push, and the hybrids were a little slower than both, because they run two queries and merge the results.

Why reads looked free

The textbook prediction is that pull reads get slower as the follower count grows, because each read merges more authors. The lab did not see that, and the reason is the setup. On one database, each author has only a few posts, so pulling 49 authors means finding each author's newest posts in sorted lists held in one process. That is a quick search, and it costs about the same as reading one list.

In a large system, each author's posts can live on a different machine, so each author becomes a network call, and the merge is only as fast as the slowest one. The lab's README makes the same point: this is the case where pull gets expensive. The lab did not test it.

The reads in the lab also fetched the newest 50 posts out of a few hundred candidates. A ranked feed, which needs far more candidates before it sorts them, would make the merge heavier even on one machine.

Choosing the threshold

The hybrid's threshold is a price comparison, not a constant. For each threshold, two quantities matter:

  • The write side: rows per post. Under push, this is the author's follower count, and it is paid once per post.
  • The read side: authors pulled per read. This is paid every time a follower opens a feed.

The price of each is what it costs on your system: a row written on one machine costs about 2 µs in this lab's setup, and a remote author fetch costs far more on a network. Multiply each by how often it happens, since a post is usually read many times, and pick the threshold with the smaller total.

The arithmetic makes the celebrity case urgent. At the lab's rate of about 2 µs per row, pushing one post to 50 million followers would take about 100 s of database time, if the work scaled in a straight line. A celebrity who posts several times a day would produce that cost every time. The lab did not run that case; this is arithmetic from the measured rate.

So the hybrid's usual shape is: push to almost everyone, and pull the few largest accounts. Reads usually far outnumber posts, which is why big feeds tend to push for most authors and pull for only a small number.

Caveats

  • One run per strategy. Each row in the table is a single recorded run on one laptop, with the machine's load average at 8.7 during the run. The p99 figures are noisy, and the lab does not claim precision for them.
  • The read side is a model, and the pulled-author counts are predictions. The run did not count the authors fetched per read. The 7.9, 20.4, 33.1 and 48.8 figures come from feed_model.py, and the lab reports them next to the measured write costs, which matched the model within about 3%.
  • The reads were on one database, in memory. The test did not put each author's posts on a separate shard, so the read result says nothing about a network fan-in of 49 calls.
  • The celebrity posts were spread over eight accounts, not five. The results come from tools/feed.py, which gives the 200 celebrity posts to the eight accounts with 20,000 or more followers. The 04_feed.py docstring and its prediction assume the five planted celebrities. That is most of the small gap between the predicted 79.8 and the measured 77.7 rows per post.
  • No ranking, no deletes, no unfollows. The feed is the newest posts in time order. A ranked feed, deleted posts, and unfollowing (which must remove a pushed post or stop a pull) were not part of the test.
  • Not covered: multi-machine reads, and what happens when an author crosses the threshold while their followers are reading.

The checklist

  1. Count the write cost before choosing push. Under push, a post costs one row per follower, so a 30,000-follower account costs about 30,000 rows per post.
  2. Price the read side the same way. Under pull, each read fetches one list per author followed. The cost is small on one machine and can be a network call per author on many.
  3. Use a threshold, and measure it. Choose the follower count where the write saved is worth more than the read fan-in added, weighted by how often each happens.
  4. Test the read side where it runs. The one-machine result is a finding about that setup, not about a sharded system.

Quiz

Try it · Check your understanding0 of 3 answered
  1. 1.An account with 30,000 followers posts once. Under push, roughly how many rows does that post write, and what does pull write for the same post?

  2. 2.Pull made the write cheap, but its reads were no slower than push in the lab. Why did the textbook cost not show up?

  3. 3.Lowering the hybrid threshold from 10,000 followers to 1,000 changed the numbers in which direction, and what does that mean for the choice?

Check your understanding

Try each question before opening its answer.

A post by an account with 30,000 followers writes about 30,000 rows under push. Why is the lab's celebrity post 58 ms, not a single write?

Because push inserts one feed row for every follower of the author, and the database still writes each row. The insert is one statement from the application's point of view, but it writes 30,000 rows. The lab measured 58 ms at the median across 235 celebrity posts, which is about 2 µs per row.

With the hybrid at 10,000 followers, the same kind of post took 0.19 ms. What changed?

The author sits above the threshold, so the post is not pushed. It is stored once, as one row. The cost moves to the read: each follower who opens their feed fetches that author's newest posts. The model predicts about 7.9 such authors per read at this threshold.

Pull wrote one row per post and read no slower than push. Why does the textbook say pull reads should get slow?

Because the textbook assumes each author's posts are a network call away, and a read merges one list per author. On one database with a small dataset, merging about 49 lists costs about the same as reading one list, so the cost did not show up. The lab did not test a sharded setup, where it would.

Lowering the threshold from 10,000 to 100 pushed fewer rows per post. What does it cost, and how do you choose?

It costs read fan-in: the model predicts 33.1 authors pulled per read at 100, against 7.9 at 10,000. You choose by pricing each side: rows written per post against authors fetched per read, weighted by how often each post is read and what each unit costs on your system. With many reads per post and expensive remote fetches, a high threshold is the usual answer.

Next up: Chapter 37, serverless functions and cold starts. The feed here was one database. The next chapter moves work into functions that start on demand, and measures what the first request costs when nothing is warm.

Learning something useful?

New labs, articles, and videos on evaluating and testing AI systems come out regularly. Follow along so you don't miss the next one.