Skip to content

Part 4 · Blueprint 19

Event Sourcing & CQRS Architecture

15 min reading · Sources · Editorial review pending

A read projection can lag even while the command history is durable.

More
20px
Reading list
Page details

Published date · Last edited:

Draft edition. Editorial review and supplementary reading recommendations are still being completed.

Format: Architecture walkthrough. Prerequisites: the beginner bootcamp. Chapter prerequisites: Message Queues & Event Streams: Kafka vs. RabbitMQ, Consistency Models: CAP, PACELC & Linearizability, Idempotency Keys & Distributed Retries.

Sixty-second mental model

Problem: A read projection can lag even while the command history is durable.

Use: Preserve event interpretation and rebuild projections deliberately.

Design boundary: Keep an append-only domain event history only when reconstructing decisions is worth its complexity. CQRS separates read and write models; it does not require event sourcing. Projection offsets and event schema evolution become operational responsibilities.

Layer 1 · Intuition

Hypothetical scenario: Northwind is designing event sourcing and cqrs. Maya represents product requirements, Dev implementation and Priya reliability. The team must agree what one event means and which component owns the event history.

Think of a bank journal producing different balance statements. Separate the requester from event history, then identify the retained evidence needed after failure.

Authority
The entitled state owner.
Invariant
A rule preserved across concurrency and failures.
CQRS
Command Query Responsibility Segregation: separating command and query models or responsibilities. Analogy: Different desks for making changes and answering questions.
Event sourcing
Using a history of domain events as the authoritative record for reconstructing state. Analogy: Keeping every ledger entry rather than only today's balance.
Snapshot
A recorded state corresponding to a defined point or version. Analogy: A dated photograph of the official files.

Architecture and worked reasoning

Design boundary: Keep an append-only domain event history only when reconstructing decisions is worth its complexity. CQRS separates read and write models; it does not require event sourcing. Projection offsets and event schema evolution become operational responsibilities.

Responsibilities on the request path

Event sourcing and CQRS: responsibilities and request directionKeep an append-only domain event history only when reconstructing decisions is worth its complexity. CQRS separates read and write models; it does not require event sourcing. Projection offsets and event schema evolution become operational responsibilities.Command validatorEvent streamProjection consumerRead model
Keep an append-only domain event history only when reconstructing decisions is worth its complexity. CQRS separates read and write models; it does not require event sourcing. Projection offsets and event schema evolution become operational responsibilities.

Workload and limiting resource

Hypothetical history has 10,000,000 events and replay sustains a selected 5,000 events/s: full replay takes 2,000 seconds, about 33.3 minutes before catch-up. A snapshot at event 9,000,000 reduces the suffix to 1,000,000 events, or 200 seconds under the same simplified rate. Snapshot compatibility still needs testing.

Keep the units attached to each intermediate result. The assumptions above describe a hypothetical exercise, not observed vendor capacity. A real capacity decision also needs a measured service curve, skew, recovery traffic and an explicit admission policy for overload.

Mutation or publication path

Event sourcing and CQRS: mutation boundaryCheck the command against current aggregate state. Then: Append a new domain event under an expected revision. Then: Project committed events with replay-safe progress.Step 1
Check the command against current aggregate state.
Step 2
Append a new domain event under an expected revision.
Step 3
Project committed events with replay-safe progress.
Check the command against current aggregate state. Then: Append a new domain event under an expected revision. Then: Project committed events with replay-safe progress.
  1. Check the command against current aggregate state.
  2. Append a new domain event under an expected revision.
  3. Project committed events with replay-safe progress.

Observation and recovery path

Event sourcing and CQRS: observation boundaryRead a projection with its declared freshness. Then: Use aggregate state for decisions requiring current invariants. Then: Rebuild in isolation before swapping a projection version.Step 1
Read a projection with its declared freshness.
Step 2
Use aggregate state for decisions requiring current invariants.
Step 3
Rebuild in isolation before swapping a projection version.
Read a projection with its declared freshness. Then: Use aggregate state for decisions requiring current invariants. Then: Rebuild in isolation before swapping a projection version.
  1. Read a projection with its declared freshness.
  2. Use aggregate state for decisions requiring current invariants.
  3. Rebuild in isolation before swapping a projection version.

Failure that changes the decision

Hypothetical scenario: A renamed event field silently changes old-event interpretation. Preserve schema version and migration/upcasting rules; rebuilding from bytes is only useful if their historical meaning remains defined.

Attempt this decision before reading the explanation

Can a projection consumer skip an unfamiliar event to keep lag low?

Not if that event can affect its result. Stop/quarantine and repair its interpretation or explicitly exclude the affected aggregate; silent skipping makes a fast but incorrect read model.

Supporting mechanism analysis

Hypothetical scenario: Northwind. Maya represents product, Dev implementation and Priya reliability. The following workflow develops the underlying mechanism.

Hypothetical scenario. Northwind’s recovery exercise repeats an intent publication, loses an acknowledgement after an effect, and delivers an older product version late. Maya asks the team to explain the final search record from the accepted edits rather than from the number of messages consumed.

Before opening the worked review, predict the final state and the unfinished work. Connect intent, publication, attempts and effects in one ledger. Keep independent projections’ progress separate and retain evidence for outcomes that remain uncertain.

Before you continue: Does consuming every delivery prove that the final projection is correct?

That does not establish which versions were applied or whether an uncertain effect still needs reconciliation. Account for each accepted obligation and the resulting protected state.

This makes the final state explainable despite duplicates and reordering. Use identities and version rules, then count the work that still needs recovery.

Product edits commit with durable downstream intent in the selected local database boundary. Search and activity have independent progress because both require the relevant changes. Image readiness follows its separate verification and publication workflow. The seller’s acceptance response does not claim that every projection is already ready.

Search updates are complete representations identified by product, source version and projection schema version. The selected target contract rejects an older source version from replacing a newer one. This is a required effect-boundary rule to establish in the implementation, not a property inferred from a queue’s name.

Scheduled cleanup has its own occurrence identity and ownership generation. It cannot delete from an obsolete work list merely because a prior lease once existed. The queue pipeline and the cleanup scheduler share recovery vocabulary without turning their different effects into one universal retry policy.

Choose four accepted logical projection updates: product P version 8, product P version 9, product Q version 3 and product R version 1. All belong to the same selected projection schema. The source ordering establishes that P version 9 supersedes P version 8.

For this hypothetical exercise, inspect six deliveries in order. P8 applies, then P9 applies. Q3 applies, but its worker loses the acknowledgement. Q3 is redelivered. R1 applies. Finally, a duplicated publication of P8 reaches the worker. Assume every required protected target operation is available and behaves according to the declared contract.

Before continuing, write the final version for P, Q and R. Decide which two deliveries should not create another version advance. Also name what record would remain unresolved if the target operation timed out instead of returning inspectable completion evidence.

Hypothetical scenario. Use the existing four accepted updates P8, P9, Q3 and R1. The final projection contains P9, Q3 and R1. P8 was a valid historical transition later superseded by P9; absence of P8 as a current row is not lost work. Conversely, a delivery disappearing from the broker is not proof of a target transition.

Give each obligation a state such as pending, protected-complete, superseded-under-contract or unresolved. Attempts retain their own identities and errors. The target version rule explains the late P8 delivery, and the stable operation identity explains Q3's redelivery after its lost acknowledgement. Neither should create a new logical obligation.

Hypothetical worked example. The six deliveries represent four accepted logical operations. P8 and P9 each advance P when first processed. Q3 and R1 advance their respective records. The repeated Q3 recognizes the already established operation. The late P8 cannot overwrite P9 under the selected source-version rule.

There are 6 − 4 = 2 non-advancing deliveries and four version advances in this particular sequence. Final records are P9, Q3 and R1. Three final product records do not imply only three logical updates occurred: P passed through two accepted versions.

The two repeated deliveries also have different causes. Q3 was redelivered after an acknowledgement was lost. P8 was published again by the dispatcher. Protecting only one transport stage would not cover both. Their shared logical identity and target contract connect these paths to the same intended work.

This sequence has 6 ÷ 4 = 1.5 deliveries per logical operation. Suppose, purely for a revised capacity exercise, that the same mix continues with equal-cost attempts. At three attempts per second, logical completion capacity is 3 ÷ 1.5 = 2 per second.

If selected attempt capacity increases to five per second, logical capacity becomes 5 ÷ 1.5 = 10/3 per second. Net margin is 10/3 − 2 = 4/3 per second. The hundred-job backlog then takes 100 ÷ (4/3) = 75 seconds to empty under the stated model. Actual attempt costs and available downstream capacity still need measurement.

The worked sequence assumed inspectable target outcomes. A timeout after dispatch is different: the effect may exist even when the worker lacks its response. Retain the original logical operation and inspect the target’s supported identity or status evidence before selecting the next transition.

Likewise, moving an invalid message to a dead-letter destination does not satisfy the original intent. The ledger should classify it as requiring correction, cancellation under an explicit rule, supersession or another recovery decision. Completion requires the effect’s actual meaning.

Search completion cannot establish activity completion, and neither establishes image readiness. Each required responsibility needs observable progress tied to the original change. The transport can retain one event history while separate consumers fall behind for different reasons.

For a rebuild, inspect the source snapshot or retained history boundary, compatible schema interpretation and removal semantics. Replaying old products without their later withdrawal information can produce a fast but invalid view. The search eligibility rule remains in force during recovery.

RabbitMQ’s confirmation and acknowledgement documentation provides concrete evidence about producer and consumer interactions with the broker. It is useful for locating the Q3 redelivery window. It does not establish that Northwind’s target enforced a source-version rule or that all independent projections finished.

The scheduling side has a similarly bounded contract. Kubernetes CronJob documentation describes its scheduling policy; application cleanup authority and object eligibility still need their own enforcement. A review should connect these mechanisms without expanding their documented claims.

Hypothetical failure scenario. All queue deliveries are acknowledged, but a worker acknowledged one update before its target effect completed. Another accepted edit never left the outbox. Broker depth reaches zero while search misses both obligations.

Layer 3 · Internals

Trade-offs and limits

ApproachProvidesRequires
Authoritative event historyRetains accepted changes for replay and derived views.Event interpretation and correction need durable version rules.
Current-state recordsOffer a direct representation for simple queries and updates.Historical reconstruction requires separate audit or version evidence.

Recovery decision

Hypothetical scenario: A renamed event field silently changes old-event interpretation. Preserve schema version and migration/upcasting rules; rebuilding from bytes is only useful if their historical meaning remains defined.

Not if that event can affect its result. Stop/quarantine and repair its interpretation or explicitly exclude the affected aggregate; silent skipping makes a fast but incorrect read model.

Write your design before revealing the review

Can a projection consumer skip an unfamiliar event to keep lag low?

Evidence you can explain in your own design

Drafts and self-review are stored only in this browser. They are separate from reading completion.

Compare your decision

Not if that event can affect its result. Stop/quarantine and repair its interpretation or explicitly exclude the affected aggregate; silent skipping makes a fast but incorrect read model.

A renamed event field silently changes old-event interpretation. Preserve schema version and migration/upcasting rules; rebuilding from bytes is only useful if their historical meaning remains defined.

Check your understanding

Can a projection consumer skip an unfamiliar event to keep lag low?

Not if that event can affect its result. Stop/quarantine and repair its interpretation or explicitly exclude the affected aggregate; silent skipping makes a fast but incorrect read model.

How would you recover from this failure: A renamed event field silently changes old-event interpretation. Preserve schema version and migration/upcasting rules; rebuilding from bytes is only useful if their historical meaning remains defined.

Start with the exact boundary that failed. Check the command against current aggregate state. Append a new domain event under an expected revision. Project committed events with replay-safe progress. Then recover evidence through the observation path: Read a projection with its declared freshness. Use aggregate state for decisions requiring current invariants. Rebuild in isolation before swapping a projection version. Preserve the distinction between a confirmed state change and an unavailable response.

Which assumption in the worked calculation would a burst or concentrated tenant invalidate first?

Hypothetical history has 10,000,000 events and replay sustains a selected 5,000 events/s: full replay takes 2,000 seconds, about 33.3 minutes before catch-up. A snapshot at event 9,000,000 reduces the suffix to 1,000,000 events, or 200 seconds under the same simplified rate. Snapshot compatibility still needs testing. Recompute the affected resource rather than multiplying unrelated storage and network budgets by the same factor. Keep an append-only domain event history only when reconstructing decisions is worth its complexity. CQRS separates read and write models; it does not require event sourcing. Projection offsets and event schema evolution become operational responsibilities.

Which component in this topology could be removed from a smaller first deployment?

Command validator, Event stream, Projection consumer, Read model name responsibilities, not a requirement for separate processes. Co-locate responsibilities when their transaction, trust and capacity boundaries permit it. Check the command against current aggregate state.

What evidence must a caller have before relying on the observation path?

Read a projection with its declared freshness. Use aggregate state for decisions requiring current invariants. Rebuild in isolation before swapping a projection version. Not if that event can affect its result. Stop/quarantine and repair its interpretation or explicitly exclude the affected aggregate; silent skipping makes a fast but incorrect read model.

45-minute interview rubric

  1. 00–05: scope event and event history.
  2. 05–12: identify the limiting resource and derive the domain workload above, retaining units and assumptions.
  3. 12–28: draw event history, numbered writes and reads.
  4. 28–40: challenge recovery: Preserve event interpretation and rebuild projections deliberately.
  5. 40–45: defend aggregate_id placement, observations and remaining assumptions.

Go deeper

  • Event Sourcing · Primary document · martinfowler.com. Preserve event interpretation and rebuild projections deliberately. Verified 2026-10-26.
  • CronJob | Kubernetes · Primary document · kubernetes.io. Preserve event interpretation and rebuild projections deliberately. Verified 2026-10-26.
  • Consumer Acknowledgements and Publisher Confirms | RabbitMQ · Primary document · www.rabbitmq.com. Preserve event interpretation and rebuild projections deliberately. Verified 2026-10-26.

Decision log

Preserve event interpretation and rebuild projections deliberately.

Preserve event identity and event history ownership.

Revisit changed workload, observations or recovery objectives.