Highlights

Contents

Part I
01

Replication vs partitioning

We name the limit before choosing a pattern. Extra machinery creates new failure states as well as new capacity. We will distinguish resource limits, invariants, replication, and partitioning.

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 1Bottleneck identification

The Heron read route becomes slower as a match grows popular. Saying use CQRS does not identify whether the limit is query work, disk, connections, or network delivery. A bottleneck is a resource or dependency that limits useful completion. Measure the demand on it before introducing a pattern.

Start with the current request flow and sustainable capacity. If reads repeatedly join the same records, a precomputed view may reduce work. If one writer owns all matches, sharding may distribute independent writes. If one match itself is hot, ordinary key hashing cannot divide that match. Every proposed change must state the waiting it removes and the new cost it creates. A cache adds freshness and miss behavior; a projection adds update work and lag; another replica adds replication traffic and failover rules.

Follow the Heron read through acquisition, query, and delivery before assigning a pattern. If the database executes promptly once a slot is available, adding a read index may leave the actual acquisition queue untouched. If repeated joins dominate execution, precomputation can remove that work while creating a projection-maintenance path. Compare the same request population before and after the change, including misses and recovery. A normal-path speedup can increase admitted demand on a slower downstream step. State the resource whose work decreases and the resource whose work increases, then inspect both under the failure workload.

READS ARE SLOW: MEASURE WHETHER IT IS QUERY WORK OR POOL WAIT BEFORE PICKING A PATTERNREADS ARE SLOW: MEASURE WHETHER IT IS QUERY WORK OR POOL WAIT BEFORE PICKING A PATTERNobservedread delay,cause unknownmeasuredquery 30 ms?pool wait 60 ms?choicereduce the measuredwork, then testan index cannot fix a pool queue; precomputation cannot fix lock waits
Figure 20.1Illustrative bottleneck identification. Measured query work and pool wait determine which change can remove read delay. Orange marks the choice that must reduce the measured work.

A design can move the bottleneck downstream. Verify the new dependency and failure workload, not only the faster normal request.

#Section 2Invariants before distribution

Two scorers update the same match and both believe their version won. An invariant is a rule that must remain true across every allowed state transition. For Heron, a command that expects version seven may create version eight only if seven is still current.

Put that rule at the authority that commits the update, not in an API process that merely read an earlier value. A conditional update checks the expected version and changes state atomically. If another command has already committed version eight, the stale command must fail or be reconciled using a domain rule. Replicas, caches, and projections can show the result later, but none should grant permission to violate the write invariant because its view is behind.

Trace the illustrated competing commands from their shared observation of version seven. The first authority check succeeds and commits eight. The later check sees eight while expecting seven, so it must leave the state unchanged. Retrying that distinct command with the new version is a domain decision because the original intended correction may no longer be valid. A retry of the first committed command instead uses its stored result identity. Keeping these cases separate prevents a duplicate request from looking like a concurrent conflict and prevents a conflict handler from blindly applying a stale change.

TWO WRITERS EXPECT v7; ONE COMMITS v8, THE OTHER IS REJECTEDTWO WRITERS EXPECT v7; ONE COMMITS v8, THE OTHER IS REJECTEDwriter Awriter Bv7v8expect 7 → 8expect 7 (stale)reject; retry is a newdecision on v8
Figure 20.2Illustrative invariants before distribution. A stale expected version loses after another writer commits. Orange marks rejection of the writer whose expected version is obsolete.

An application check followed by an unconditional write can lose an intervening update. Expected-version enforcement belongs in the same atomic operation as the state change.

#Section 3Replication and read eligibility

Heron stores another copy of a score. Replication copies logical state to another node. It can support recovery or more reads, but it does not automatically divide the write authority for that score.

A leader commits a version and sends it to followers. An asynchronous follower can return the previous version until it receives and applies the update. Synchronous acknowledgement changes the commit wait and failure availability. A promoted follower must be sufficiently current for the promised recovery rule, and the old leader must stop accepting authoritative writes. Extra copies therefore add a data movement path, visibility states, and an ownership transition. Draw those states separately from the request route.

A copied log entry and an applied score are different follower states. The leader can acknowledge the durability required by the write while the follower still exposes version seven. A reader requiring eight cannot use that follower merely because the process is healthy or its log has received the entry. It waits for application, changes route, or receives the declared pending result. On promotion, check the recovery promise against the follower’s surviving committed history and reject old-owner effects. More copies improve options only when their write acknowledgements, read eligibility, and takeover rules satisfy the intended operation.

A FOLLOWER STILL ON v7 MAY NOT SERVE A READER WHO NEEDS v8, UNTIL IT APPLIES v8A FOLLOWER STILL ON v7 MAY NOT SERVE A READER WHO NEEDS v8, UNTIL IT APPLIES v8leaderv8followerv7v8v8needs v8received in the log is not applied; healthy is not eligible
Figure 20.3Illustrative replication and read eligibility. The follower becomes eligible for a current read only after applying the committed version. Orange marks the follower becoming eligible after applying the committed version.

Acknowledgement semantics determine loss risk. A replica that exists but has not received the latest acknowledged write does not meet a zero-loss failover promise.

#Section 4Partitioning and ownership

The service cannot fit every match on one database node. Sharding assigns different subsets of records to different owners. A request must find its owner before it can read or change authoritative state.

At the illustrative 1,000 reads per second across four perfectly balanced shards, average demand is 250 per shard. That is arithmetic under a balance assumption, not a measured guarantee. A partition key based on match keeps one match local, which simplifies its version invariant but concentrates its popularity. A key based on user can spread viewers while making match-wide updates reach several owners. Decide which operation must remain local, then accept the cost paid by operations that cross the chosen boundary.

The illustrated 250 reads per shard is a mean under equal distribution, not a routing rule. A match-keyed owner keeps its score and version check together while all readers of that match converge on it. A user-keyed representation spreads viewer reads but creates maintenance work whenever a score changes for many users. Those are different state models, so the choice cannot be made by dividing the fleet total alone. Draw the authoritative update separately from any user-keyed projection. This preserves local write correctness while exposing the distribution and lag costs of the alternative read shape.

FOUR SHARDS SPLIT OWNERSHIP; BALANCED DEMAND IS AN ASSUMPTIONFOUR SHARDS SPLIT OWNERSHIP; BALANCED DEMAND IS AN ASSUMPTION1,000 reads/sowner Akey rangeowner Bkey rangeowner Ckey rangeowner Dkey range1,000 ÷ 4 = 250 reads/s mean per shard, if balanced
Figure 20.4Illustrative balanced demand. Four owners split 1,000 reads per second into a mean of 250 reads/s each. A partition key can still concentrate real demand on one owner.

A cross-shard transaction is a new coordination problem. Adding shards without changing the ownership rule can spread storage while leaving the hottest request on one node.