Building feeds on demand
After this: Merge the posts of the people someone follows, newest first, with a heap, and show why this costs 20 times more here.
On this page · 6 sections · 3 checks
Fan-out on write made reading cheap and posting expensive: 11,574 inserts every second. What if nothing is copied when someone posts, and each feed is built only when its owner opens the app?
Build the feed when it is opened
When Ben opens the app, the app looks at each account Ben follows, takes their newest posts, and puts them together, newest first. Posting is now one write: save the post. All the work happens at reading time.
This is , also called pull, because the posts are pulled in when someone reads. Putting several lists that are each in order into one list in order is a .
Merging by hand
Ben follows three people. Each person's posts are already in order, newest first; the numbers are the times they were posted, and a larger number is newer.
| Account | Posts, newest first |
|---|---|
| Ana | 9, 4 |
| Cy | 7, 2 |
| Dee | 8 |
To merge, look at the newest post left of each person, and take the newest of those:
- The newest left are Ana 9, Cy 7, Dee 8. Take Ana 9. Ana's next is 4.
- Now Ana 4, Cy 7, Dee 8. Take Dee 8. Dee has no more.
- Now Ana 4, Cy 7. Take Cy 7. Cy's next is 2.
- Then Ana 4, then Cy 2.
Ben's feed is 9, 8, 7, 4, 2. A screen shows the top few, so the merge can stop after three: Ana 9, Dee 8, Cy 7.
A heap does the choosing
With 3 people, comparing their newest posts is quick. With 200, finding the newest would mean looking at all 200 posts every time. A holds exactly the "newest left of each person". Taking the newest out, and putting that person's next post in, each cost about 8 steps instead: one step per level of the heap, with a comparison or two at each. That 8 is about log₂ 200, which is 7.6: the number of times 200 can be halved before it reaches 1. The work grows very slowly as the number of people grows. The heaps tutorial shows how it works inside; here we use it.
import heapq
posts = {"Ana": [9, 4], "Cy": [7, 2], "Dee": [8]} # newest first
heap = []for author, times in posts.items(): heapq.heappush(heap, (-times[0], author, 0)) # each author's newest post
shown = 0while heap and shown < 3: minus_time, author, i = heapq.heappop(heap) # the newest post left print(author, -minus_time) shown += 1 times = posts[author] if i + 1 < len(times): heapq.heappush(heap, (-times[i + 1], author, i + 1))Ana 9 Dee 8 Cy 7
import java.util.*;
record Item(int time, String author, int index) {}
public class Main { public static void main(String[] args) { Map<String, List<Integer>> posts = new LinkedHashMap<>(); // newest first posts.put("Ana", List.of(9, 4)); posts.put("Cy", List.of(7, 2)); posts.put("Dee", List.of(8));
PriorityQueue<Item> heap = new PriorityQueue<>((a, b) -> b.time() - a.time()); for (String author : posts.keySet()) { heap.add(new Item(posts.get(author).get(0), author, 0)); // each author's newest post }
int shown = 0; while (!heap.isEmpty() && shown < 3) { Item top = heap.poll(); // the newest post left System.out.println(top.author() + " " + top.time()); shown++; List<Integer> times = posts.get(top.author()); if (top.index() + 1 < times.size()) { heap.add(new Item(times.get(top.index() + 1), top.author(), top.index() + 1)); } } }}Ana 9 Dee 8 Cy 7
- The heap starts with one item per person: their newest post.
- Each turn takes the newest item out of the heap and shows it.
- Then it puts that person's next post, if there is one, into the heap. The heap never holds more than one post per person.
- It stops after three posts.
Change the 3 in the loop to 5 and run it again: the program prints the whole feed, 9, 8, 7, 4, 2.
What pull costs
Each feed load looks at every account the reader follows. With lesson 1's numbers:
- 100 million loads a day × 200 accounts = 20 billion lookups a day.
- 20,000,000,000 ÷ 86,400 is about 231,481 lookups per second.
- Push needed 11,574 inserts per second. Pull costs 20 times as much.
Why 20? Each follow is one link between two people: it adds one follower to one of them and one followed account to the other. So across everyone, the average number of followers equals the average number followed: 200 and 200. Push does 200 pieces of work per post, pull does 200 per load, and there are 20 loads for every post. The sum assumes that each post reaches the average 200 followers; lesson 4 shows what happens when one does not.
| Push (fan-out on write) | Pull (fan-out on read) | |
|---|---|---|
| Work when someone posts | one insert per follower | one write |
| Work when someone reads | one lookup | one lookup per followed account, then a merge |
| Here, per second | 11,574 | 231,481 |
For this app, push wins by 20 times. In an app where people post more often than they read, the same sum would favour pull.
Check yourself
Ben follows Eve (posts at times 10 and 3), Finn (6 and 5) and Gus (7). Fan-out on read merges them. What is Ben's feed, newest first?
With 10 million people, 0.5 posts and 10 feed loads each per day, and 200 followers and 200 followed on average, why does fan-out on read cost about 20 times more than fan-out on write?
The same merge with other people and times. What does it print?
import heapq
posts = {"Ana": [6, 3], "Cy": [5], "Dee": [8, 1]} # newest first
heap = []for author, times in posts.items(): heapq.heappush(heap, (-times[0], author, 0)) # each author's newest post
shown = 0while heap and shown < 3: minus_time, author, i = heapq.heappop(heap) # the newest post left print(author, -minus_time) shown += 1 times = posts[author] if i + 1 < len(times): heapq.heappush(heap, (-times[i + 1], author, i + 1))import java.util.*;
record Item(int time, String author, int index) {}
public class Main { public static void main(String[] args) { Map<String, List<Integer>> posts = new LinkedHashMap<>(); // newest first posts.put("Ana", List.of(6, 3)); posts.put("Cy", List.of(5)); posts.put("Dee", List.of(8, 1));
PriorityQueue<Item> heap = new PriorityQueue<>((a, b) -> b.time() - a.time()); for (String author : posts.keySet()) { heap.add(new Item(posts.get(author).get(0), author, 0)); // each author's newest post }
int shown = 0; while (!heap.isEmpty() && shown < 3) { Item top = heap.poll(); // the newest post left System.out.println(top.author() + " " + top.time()); shown++; List<Integer> times = posts.get(top.author()); if (top.index() + 1 < times.size()) { heap.add(new Item(times.get(top.index() + 1), top.author(), top.index() + 1)); } } }}Honesty notes
- Explain fan-out on read: nothing is copied when someone posts; the feed is built when the person opens it.
- Merge the posts of the followed accounts newest first with a heap, which holds the newest post left of each account.
- Work out the cost: 100 million loads a day looking at 200 accounts each is about 231,481 lookups per second.
- Say why pull costs 20 times push here: both do 200 pieces of work per event, and there are 20 loads for every post.