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).
Push below 1,000 followers. A post by an author above the threshold is stored once and fetched when a follower reads.
- 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. The04_feed.pydocstring 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
- 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.
- 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.
- 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.
- Test the read side where it runs. The one-machine result is a finding about that setup, not about a sharded system.
Quiz
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.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.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.