CQRS and materialized views
We build different shapes for commands and reads. Without explicit freshness rules, a fast projection can return misleading state. We will separate models, derive views, process updates, and recover lag.
#Section 9CQRS separates models
A scorer update needs a version check, while a viewer wants a compact score card. Forcing both through the same representation can make validation or reading harder. Command Query Responsibility Segregation, or CQRS, uses distinct models for updates and queries.
The command model represents allowed changes and enforces the invariant. The query model represents an answer shaped for its readers. They may share a database; separate models do not require separate services or event sourcing. If the read model is stored separately, add a propagation path and define its freshness. Martin Fowler describes this model distinction and warns against applying it indiscriminately in CQRS. Start with one database and separate query code unless measured needs justify a distributed read store.
For Heron, the command representation needs the expected version and valid score transition, while the read representation needs the card fields presented to the viewer. Separate these representations in code first and preserve a single transaction where that meets the workload. Moving the card to another store adds a commit-to-view delay, a consumer checkpoint, and a rebuild source. Those costs need a read-work or isolation benefit. A command handler should not validate against the faster card if that card can lag. The distinction is valuable when it clarifies the invariant and access pattern, regardless of the number of deployed services.
Using two endpoint names over the same unchanged logic does not explain a useful model separation. State what representation differs and why.
#Section 10Materialized view maintenance
Every score-card read recomputes a join across match and team records. A materialized view stores a previously computed answer so reads can retrieve it without repeating that work.
After the authoritative update, compute the score card and store its source version. Reads fetch the card and can decide whether its version meets the request. This shifts work from each read to each relevant update. The saved read work is paid for with update processing, additional bytes, freshness delay, and a rebuild procedure. A database-managed view and an asynchronous service projection have different update and failure mechanics. Explain which one is used rather than treating materialized as a guarantee of immediate freshness.
The figure’s source has reached eight while its card still represents seven. An ordinary viewer may accept that card under the stated freshness allowance; the scorer asking for its just-committed result cannot. When an older update arrives after the card reaches eight, the projection must not overwrite it. For replacement events, a guarded source-version write can enforce that rule; for incremental changes, require the necessary contiguous history or deduplication. A failed projector therefore changes read eligibility before it changes the authoritative score. Keep that distinction visible so an operator can rebuild the view without treating it as the only surviving truth.
A mutable card with no source version cannot tell whether an out-of-order update overwrote a newer result.
#Section 11Projection updates and duplicate delivery
The relay delivers the same score event twice. A projection is derived state computed from source records or events. Applying an increment twice changes its result unless the update is duplicate-safe.
For a score-card replacement, compare event version with stored version and accept only a newer valid result. For a derived count, record event identity with the effect in the same transaction, or use another repeatable update rule. Commit projection state and processing checkpoint atomically where the storage model permits it. An acknowledgement after the effect can be lost, causing redelivery; the consumer must then see that the event was already applied. A broker delivery guarantee alone cannot make an arbitrary external update exactly once.
Consider a projection update followed by a failed insert of its deduplication identity. If those operations have separate commits, restart can apply the update again because its evidence of completion is absent. Insert the identity and change the projection inside the same local transaction, rejecting an already present identity without another effect. For a replacement card, an atomic version comparison may supply the repeatable rule instead. Record consumer progress only after the protected effect boundary. A broker acknowledgement then releases delivery responsibility, but the local rule still protects a replay caused by an uncertain or lost acknowledgement.
Global sequence numbers are not implied by per-partition offsets. Keep the version rule scoped to the aggregate or partition whose ordering it describes.
#Section 12Lag, catch-up, and read-your-writes
The projector pauses for ten seconds while Heron creates 20 events per second. The computed backlog is 200 events. On recovery the projector applies 100 per second while 20 new events arrive, leaving 80 per second of spare capacity; catch-up takes 2.5 seconds.
Projection lag measures how far derived state trails its source. Give the scorer the committed source version and let a subsequent read require at least that version. The read can wait within a deadline, query the authority, or return an explicit pending state. It must not pretend that an old projection confirms the write. Catch-up capacity, not nominal consumer capacity, determines recovery while traffic continues.
In the illustrated recovery, the projector’s 100-event service rate includes new work as well as old work. The ongoing 20-event input consumes part of that capacity, leaving the stated 80 for the backlog. If a read waits for its returned write version, impose a deadline and observe whether the version advances before that deadline. A poison event can stop progression even when total processing attempts appear high. Surface that blocked position and choose a repair policy that preserves event meaning. The arithmetic drain time assumes useful ordered processing continues; failed attempts do not count toward the spare completion rate.
Waiting forever for a projection turns a lagged read store into an unbounded request queue. Use a deadline and a documented fallback.