← All patterns
Data consistency · Architecture pattern

CQRS & materialized views

Separate commands from queries and build read models for the questions users ask.

The problem

A feed query repeatedly joins write-oriented records. The read shape and write invariants need different models.

When it fits

Read and write requirements differ enough to justify separate interfaces/models and the extra update coordination.

The whole architecture

Architecture diagram

Separate command and query interfaces use an asynchronously maintained read model in this variant.

Blue arrows are API/data calls; purple arrows carry committed projection input.

100%

Scroll to pan. Select a component for its role.

cqrs materialized views architectureSeparate command and query interfaces use an asynchronously maintained read model in this variant. Blue arrows are API/data calls; purple arrows carry committed projection input.Command modelQuery modelclient → command: commandclient → query: querycommand → write: commitwrite → feed: committed changesfeed → projector: applyprojector → read: maintain viewquery → read: querycommandquerycommitcommitted changesapplymaintain viewqueryA successful command and a fresh projected query are different guarantees.ClientsCommands and queriesThe write model enforces the business operation.Command APIValidate writeinvariantsThe query contract should define freshness and read-your-write behavior.Query APIRead-oriented contractCQRS can also use shared physical storage; separate stores are an option.Write storeAuthoritative recordsProjection input must correspond to committed changes.Committed feedChange position /identityReplayed or out-of-order records need an explicit application policy.ProjectorVersion-aware updatesThis store can be rebuilt from an appropriate source boundary.Read modelDerived query stateCQRS does not inherently require event sourcing.Projection operationsLag, checkpoints, rebuild and read-your-write policy

Select a component, or focus it and press Enter, to read its role.

Walk through the design

Follow one operation

Select a step to highlight its participants. This conceptual walkthrough is separate from challenge scoring.

Arrows show the conceptual relationships used by this walkthrough.

Command APIActive in this stepWrite storeActive in this stepProjectorWaitingRead modelWaiting
Step 1 of 3

Accept a command

Validate an invariant and commit authoritative state through the command interface.

Trade-offs to make explicit

  • CQRS can use one database; separate storage and event sourcing are optional choices.
  • Materialized views speed a query by maintaining derived data. Their freshness and rebuild cost become explicit concerns.

Failure modes to plan for

  • A delayed projection can hide a successful write from the next query. Define read-your-write behavior.
  • Out-of-order changes or replayed deletes can corrupt a projection without version and tombstone handling.
Before you build

Explain your decision

After a command succeeds, should the UI wait for projection progress, read authoritative state, or show a pending state?

Use Plan & estimate inside practice to record your assumptions and trade-offs.

Connect concept to practice

Related challenges

Browse challenges →

These links open the original challenges. Existing round unlocks, completions and points apply.

The celebrity post

Practice query-oriented materialization and push/pull/hybrid trade-offs.

Open challenge →

Rebuild search while writes continue

Practice rebuilding derived state from a snapshot and committed changes.

Open challenge →

Further reading

Microsoft · CQRS ↗

Original PractiseGrid explanations and conceptual diagrams, with primary references for further study. Dedicated scored exercises can be added as coverage grows.