Topic 099 / Advanced
Fan-out on write vs fan-out on read
Chapter 6.4 · Traffic Control and Workload Shaping
What and why
A feed assembled from many other users' activity can be built two structurally different ways: compute it once per writer and store a ready copy for every reader (fan-out on write), or compute it fresh for each reader at request time by pulling from every followed source (fan-out on read). Fan-out on write turns one write into many — a materialized, per-reader copy, much like a cache entry (→ 87) populated proactively instead of on a reader's miss. Fan-out on read turns one read into many — gathering from every followed source live. Builds on: → 87 (Redis and distributed caching).
Diagram
Text version of this diagram
FAN-OUT ON WRITE FAN-OUT ON READ
───────────────── ────────────────
writer posts ──► for each of N reader requests feed ──► fetch from each of
followers: append to their M followed sources, live ──► merge and
pre-built feed (→ 87, e.g. a sort ──► return
sorted set per follower)
reader requests feed ──► read one
pre-built copy ──► return (cheap)
How it works
- Fan-out on write: when a writer posts, the system immediately pushes a reference to that post into every follower's own stored feed (e.g., a Redis sorted set per follower, → 87); reads are then a single cheap lookup of an already-assembled list.
- Cost shape for fan-out on write: one write becomes N writes (N = follower count) — cheap per follower, but it scales with the writer's audience size, so a writer with millions of followers turns one post into millions of fan-out writes.
- Fan-out on read: nothing is pre-built; a reader's request fetches recent posts from every followed source live, merges and sorts them, and returns the result — writes stay cheap (one write, regardless of audience), but every read now costs M source fetches (M = how many sources that reader follows).
- The hybrid: fan-out on write for ordinary accounts (small audiences, so the per-follower cost is small), fan-out on read for accounts above a follower-count threshold — readers who follow that account merge its posts live at read time, on top of an otherwise prebuilt feed.
- The decision reduces to comparing write cost (writes × followers) against read cost (reads × sources-followed) at the system's actual scale — the right answer depends on the ratio of writes to reads and the audience-size distribution, not on instinct.
Practical example
(numbers illustrative). A platform has 1,000,000 posts/day, average followers per writer = 200, average sources followed per reader = 200, and 10,000,000 reads/day.
- Fan-out on write: 1,000,000 × 200 = 200,000,000 fan-out writes/day, plus 10,000,000 × 1 = 10,000,000 cheap lookups/day — total 210,000,000 ops/day.
- Fan-out on read: writes stay at 1,000,000 (no fan-out); each read fetches 200 sources: 10,000,000 × 200 = 2,000,000,000 source-fetch ops/day — total 2,001,000,000 ops/day.
Fan-out on read does roughly 2,001,000,000 ÷ 210,000,000 ≈ 9.5× more total work at this 10-reads-per-write ratio — exactly why fan-out on write is the default for feeds, and why a single account with, say, 10,000,000 followers (a celebrity) is cheaper to serve with fan-out on read instead: fanning out one of its posts on write alone would cost 10,000,000 writes, while fan-out on read adds just one extra source-fetch per follower's otherwise ordinary read.
When to use it
- Use fan-out on write when the read:write ratio is high and follower counts stay in a bounded range.
- Use fan-out on read for accounts whose follower count is large enough that the write-side cost (followers × posts) would dwarf the read-side savings.
- Avoid committing to pure fan-out on write for a platform with any unbounded-follower accounts without the hybrid escape valve — it's one celebrity account away from a write-cost explosion.
- Failure scenarios: a celebrity's post triggering a fan-out write storm that saturates the write path for everyone else at the same moment (a stampede, → 88, at the write side); fan-out on read's per-request cost growing silently as a reader follows more sources, with no cap, degrading that one reader's latency (→ 100) without warning; a hybrid implemented with a hardcoded threshold rather than a measured one, missing accounts that cross it as they grow.
- Simpler alternative: for a small platform with no runaway follower counts, fan-out on read alone — compute every feed live — may be simpler to build and reason about, deferring fan-out on write until the read cost measurably matters.
Practice
(decision kind — judge 3–5 scenarios and defend the choice, 20 min).
- Recompute the practical example's total-ops comparison with average followers = 500, average sources followed = 50, the same 1,000,000 posts/day and 10,000,000 reads/day, and state which strategy wins at this ratio.
- Judge each of: a 50-follower-average social app, a single account with 50 million followers inside that same app, a direct-message inbox (1 recipient per message), a public company-announcements feed read by every employee — fan-out on write, fan-out on read, or hybrid for each, and why.
- State the follower-count threshold at which, using your own platform's read:write ratio, fan-out on write stops being cheaper — show the arithmetic, not a guess.
Answer key (drill 1): fan-out on write = 1,000,000×500 + 10,000,000×1 = 510,000,000 ops; fan-out on read = 1,000,000 + 10,000,000×50 = 501,000,000 ops — fan-out on read is now slightly cheaper, flipping the worked example's result, because the followers:sources-followed ratio (500:50 = 10:1) now matches the reads:posts ratio (10:1) that previously favored fan-out on write.
Done when your recomputed totals in drill 1 correctly flip the winner relative to the worked example, matching the new ratio.
Key takeaway
Fan-out on write pays at write time per follower, fan-out on read pays at read time per source followed — the right default depends on the write:read ratio and the follower-count distribution, with a hybrid for outlier accounts. Done when you can: compute total operation cost both ways for a given scale and name the hybrid threshold. Used next in → 100 (latency and throughput, what each strategy's cost shape does under real load); Project 13 (the defining decision).
References
None — this topic makes no external claims.
Source directory