Highlights

Contents

Part VIII
08

Fan-out on write vs on read

We place repeated work on the write or read path. The wrong placement makes one popular publisher dominate the system. We will compute write fan-out, read fan-out, hybrid feeds, and consistent ranking.

CHAPTER ATLAS / FOLLOW THE READING PATHCHAPTER ATLAS / FOLLOW THE READING PATH1Replication vspartitioning2Sharding and consistenthashing3CQRS and materializedviews4Event sourcing5Transactional outbox andCDC6Saga pattern7Leader election,distributed locks8Fan-out on write vs onread9Case study: combiningpatterns

#Section 29Fan-out on write

A worker can crash halfway through recipient creation. Store its recoverable position or rescan using unique recipient-post identities, so restart fills missing recipients without creating another entry for recipients already processed.

A publisher posts once and many followers will read it. Fan-out on write places a reference in each recipient’s feed when the post is published, making later feed reads cheap.

At the illustrative 100 posts per second and 10,000 recipients per post, workers create 1,000,000 recipient entries per second. Each reference needs an identity that combines recipient and source post, so duplicate deliveries do not create duplicate feed entries. The publication path can commit the post first and fill feeds asynchronously, but then visibility lag is part of the contract. A celebrity post creates a large burst, and deletion or permission changes must invalidate the copied references.

Treat recipient creation as resumable work, because publication can outlive the worker building feeds. Each recipient-post relation has a unique identity; restart can skip those already committed and fill the missing relations. If the publisher’s permissions change before a delayed recipient entry arrives, serving still checks current visibility rather than treating insertion as permanent authorization. The illustrated million entries per second measures reference creation, not content-byte duplication or finished user reads. Keep source post identity in each reference so deletion and rebuild have a target. A popular publisher changes this work suddenly even if publication rate remains steady.

FAN-OUT ON WRITE: 100 POSTS/S × 10,000 FOLLOWERS = 1,000,000 FEED ENTRIES/SFAN-OUT ON WRITE: 100 POSTS/S × 10,000 FOLLOWERS = 1,000,000 FEED ENTRIES/Spublisherworkersreads are cheap; serving still checks current visibility
Figure 20.29Illustrative fan-out on write. Write-time fan-out prepares references for later feed reads. Orange marks the prepared recipient feed that makes later retrieval cheaper.

A copied feed reference does not grant permanent access. Recheck authorization when content visibility changes.

#Section 30Fan-out on read

Many followers never open their feeds. Precomputing entries for them spends write capacity without serving a read. Fan-out on read gathers candidate posts from followed publishers when the user requests a feed.

At the illustrative 50 feed reads per second and 200 gathered candidates per read, candidate processing is 10,000 items per second. This is not automatically cheaper than write fan-out: the scenarios must share publication rates, active readership, candidate sizes, and ranking work. The query needs bounded fetches, timeouts, and a merge rule. The slowest required publisher source can delay the page, so cache or precompute stable candidate lists where useful. A page cursor should represent the ordering boundary, not merely a changing numeric offset.

Follow a read whose candidate owners finish at different times. Merge only the bounded results allowed by the request deadline and declare whether a missing owner permits a partial page. A ranking cursor must encode the chosen continuation context so a later request does not simply skip a moving numeric offset. The illustrated candidate-processing rate omits remote call overhead and the work of ranking each candidate. Batch retrieval by owner where the data model allows it, then account for that owner’s fan-out. Avoid comparing this candidate count with write-time entry creation as though they were interchangeable units of work.

FAN-OUT ON READ: 50 READS/S × 200 CANDIDATES = 10,000 CANDIDATES/S GATHERED AND MERGEDFAN-OUT ON READ: 50 READS/S × 200 CANDIDATES = 10,000 CANDIDATES/S GATHERED AND MERGEDreadergather + mergerankpage + cursorstable boundary
Figure 20.30Illustrative fan-out on read. Read-time fan-out gathers candidates before forming a stable continuation page. Orange marks a page formed with its stable continuation boundary.

If every followee becomes a separate network call, candidate count understates request fan-out. Batch by storage owner and count those requests too.

#Section 31Hybrid fan-out and skew

A normal publisher can be precomputed cheaply while a celebrity has too many recipients for the same policy. A hybrid fan-out combines write-time feeds for one population with read-time retrieval for another.

Classify publishers using measured fan-out cost and read demand. The feed reader merges precomputed entries with recent celebrity posts, deduplicates by post identity, applies permissions, and ranks the combined candidates. A publisher changing class needs a migration boundary so its posts neither disappear nor appear twice. Keep the classification version with the cutover or query both paths during a bounded transition. Hybrid routing solves skew only if the read path remains bounded for users who follow many high-fan-out publishers.

A publisher changing classification can leave old posts in precomputed feeds while new posts arrive only through read-time gathering. During transition, query the appropriate paths and deduplicate by source post identity. Preserve a classification boundary or version so restart can explain which publications were supposed to reach which path. Readers still apply the same permission and pagination contract to the merged candidates. A classifier based only on follower count can miss expensive active readership or overprepare feeds for inactive users. Measure both write fan-out and read gather cost before changing that placement policy.

HYBRID: PUSH ORDINARY POSTS, PULL A CELEBRITY'S AT READ TIME, MERGE BY POST IDHYBRID: PUSH ORDINARY POSTS, PULL A CELEBRITY'S AT READ TIME, MERGE BY POST IDordinarywrite fan-outcelebrityread-time candidatesmergededupe + permissions
Figure 20.31Illustrative hybrid fan-out and skew. Hybrid feeds merge both work paths under identity and permission rules. Orange marks the merge that deduplicates candidates and applies current permissions.

An arbitrary follower threshold without workload evidence can move too much traffic into the read path and harm latency.

#Section 32Ranking, deletion, and stable pages

A feed page ends at a score boundary, and new posts arrive before the next page. Offset pagination skips or repeats entries as the ranked list moves. Stable pagination carries a boundary and a ranking context that allow continuation without pretending the feed never changes.

Use a source identity as a tie breaker and a cursor that captures the chosen order boundary. If ranking depends on mutable scores, decide whether the session uses a snapshot-like ranking version or accepts documented movement. Deleted or unauthorized posts should be filtered without allowing their copies to bypass current policy. A tombstone or source-version check can coordinate derived deletion, but the authoritative permission check must still protect content. Ranking is part of the data contract, not decoration applied after correctness.

Suppose the next-page request arrives after a post above its previous boundary has been deleted. The continuation rule should describe whether it fills the page from later candidates and which ranking context those candidates use. Current authorization can remove entries even when the ranking snapshot stays fixed, so a stable cursor cannot promise immutable access. Use a deterministic source-identity tie breaker for equal ranks and validate the cursor’s scope. A cursor for one user or query should not grant access to another population. Keep ranking stability and content visibility as separate decisions when explaining why an entry disappeared.

PAGE 2 CONTINUES IN THE SAME RANKING CONTEXT AND DROPS ENTRIES DELETED SINCE PAGE 1PAGE 2 CONTINUES IN THE SAME RANKING CONTEXT AND DROPS ENTRIES DELETED SINCE PAGE 1p1p2p3p4page 1cursor (score, id)p5p6p7p8page 2p6 deletednew arrivals before the boundary follow a session policy
Figure 20.32Illustrative ranking, deletion, and stable pages. The next page preserves ordering context while filtering newly inaccessible entries. Orange marks continuation under the same ranking context while excluding deleted entries.

A cursor over a mutable score cannot promise a perfectly stable sequence unless the design preserves the relevant ranking context.