23. Pub/Sub and Fan-Out
23.4 What are the fan-out strategies?
On the paper round: When a paper comes out, the agency can carry a copy to every doorstep that morning, or keep a pile at the stall and let readers collect when they want. Doorstep delivery is lovely for readers but hard work for the agency; the stall is easy for the agency but makes readers walk.
In the system: Fan-out is the work of getting one item to many recipients. For the bookstore's "New from authors you follow" feed, when an author releases a book, how do followers see it?
Figure: push when you post, pull when you read, or push for most and pull for celebrities.
23.4.1 Fan-out on write
On the paper round: Deliver a copy to every subscriber's doorstep, as soon as it's printed.
In the system: When an author releases a book, write it into every follower's feed immediately (a precomputed list per user, often in Redis or a wide-column store).
- Reading is cheap: opening the feed is one lookup of a ready-made list.
- Writing is expensive: a post costs one write per follower. A typical author with 200 followers: 200 writes, fine. An author with 5 million followers: 5 million writes for one post. At 50,000 feed writes a second, that's 100 seconds of work for one release, and the last followers see it late.
- Wasted work: followers who never open the app still get copies.
23.4.2 Fan-out on read
On the paper round: Keep one pile at the stall. Readers come and collect when they want.
In the system: Store each release once, in the author's list. When a user opens their feed, fetch recent releases from every author they follow and merge them.
- Writing is cheap: one write per post, celebrity or not.
- Reading is expensive: following 300 authors means 300 lookups and a merge on every feed load, and feed loads are far more frequent than posts.
- Always fresh, no wasted work for inactive users.
23.4.3 The hybrid fix for the celebrity problem
On the paper round: Deliver to doorsteps for every ordinary paper. But for the one enormous special edition, leave a pile at the stall, and when readers come to pick up their usual paper, hand them the special one too.
In the system: Use fan-out on write for normal authors and fan-out on read for celebrities (authors above a follower threshold):
- When a normal author posts: push to followers' feeds.
- When a celebrity posts: just store it.
- When a user reads their feed: take their precomputed feed, add recent posts from the few celebrities they follow, merge, and return.
In the chapter's code, 20,000 readers follow 500 authors with a very uneven spread (the top author has about 17,800 followers; the median about 50). Treating the 4 authors with at least 5,000 followers as celebrities, the hybrid does 44% fewer writes than pure push and 66% fewer reads than pure pull, and every user's feed comes out identical to the other two approaches.
This is the well-known approach for social timelines: Twitter's engineers have described fanning out tweets on write while handling very high-follower accounts differently. The threshold is a tuning knob, chosen from follower counts and how often people read.
Memory code: "Doorstep for most, the stall for the giants."