Part 4 of 8 · 2 min read
Fanout
The big choice.
Fanout is the act of delivering one post to all of the author's friends. There are two models.
Fanout on write (push). When a post is published, the system immediately adds its id to every friend's feed cache. Reading is then fast, because the feed is ready.
- Good: the feed is ready in real time. Reads are one cache lookup.
- Bad: a user with a huge number of friends causes a very large burst of writes, the "hot key" problem. Work is also wasted on people who rarely log in.
Fanout on read (pull). Nothing is prepared. When a user opens the feed, the system fetches recent posts from each friend and merges them.
- Good: no wasted work for inactive users, and no burst when a celebrity posts.
- Bad: reads are slow, since the merge happens while the user waits.
A worked example (made up). A celebrity with 2,000,000 followers posts. Push means 2,000,000 cache writes. At 20,000 writes per second that takes 2,000,000 / 20,000 = 100 seconds, and it competes with everyone else's posts. Pull for the same account costs nothing at write time.
The book's answer is a hybrid. Use push for most users, because fast reads matter most. For accounts with very many followers, do not push. Followers pull those posts when they open the feed, and the service merges them with the pre-built list. Consistent hashing (Level 4) helps spread the hot keys more evenly.
A rule to state out loud: pick a follower threshold (say 10,000, made up). Below it, push. Above it, pull. Revisit the number from measured data.
Fanout
Fanout is delivering one item to many receivers. On write means doing it when the item is created. On read means doing it when a receiver asks.
Example: a post to 300 friends is 300 cache writes under push, or 300 lookups per reader under pull.
Check yourself: 1. Which model suits an inactive user better? 2. Why is push risky for a celebrity? 3. How does the hybrid read path differ from pure push?