ProgramGuru

CQRS Pattern

CQRS: start from the problem

See the responsibilities

CommandValidates and changes authoritative state
SourceKeeps the business invariant
ProjectionBuilds a useful read representation
QueryReads under a freshness contract
Command: Validates and changes authoritative state → Source: Keeps the business invariant → Projection: Builds a useful read representation → Query: Reads under a freshness contract

Order placement needs strict business validation, while a dashboard wants a convenient summary of many orders. Forcing both through one model can couple write rules to expensive read formatting.

This is a data and messaging pattern lesson. Follow the responsibilities, inspect the small JavaScript example and compare the design choices. The example is local teaching code; its boundary is explained before you apply the idea to a real service.

What you’ll learn

  • Explain the problem CQRS addresses and the roles involved.
  • Follow the example and distinguish this pattern from nearby alternatives.
  • Choose when the benefit justifies the extra responsibilities and testing.

How the responsibilities work

Command Query Responsibility Segregation separates the responsibilities used to change state from those used to read it. It can start with separate code models over one store; separate databases and asynchronous projections are optional architectural choices.

Our model has an authoritative balance and a delayed dashboard copy. The command changes the source, and projection later refreshes the read model. This illustrates one possible asynchronous design, not a requirement for every CQRS implementation.

Follow a small example

Read the JavaScript from top to bottom and compare the expected output. The values are chosen to make this pattern’s responsibility visible. This example runs in one local execution context; it does not create remote services, durable storage or production access controls.

JavaScript · local teaching example

let authoritativeBalance = 10;
let dashboardBalance = 10;
function addCredit(amount) { authoritativeBalance += amount; }
function project() { dashboardBalance = authoritativeBalance; }
addCredit(5);
console.log(authoritativeBalance + ' | ' + dashboardBalance);
project();
console.log(dashboardBalance);

Expected output

15 | 10
15

Choose a design and inspect the consequence

Compare the design choices

The dashboard may lag by a few seconds.
Reads and writes are simple with no conflicting needs.

Compare the visible consequences. With JavaScript enabled, choose an option in each scenario to assemble your decision record.

This is an exploratory design exercise. It does not run infrastructure, measure capacity or replace the lesson’s quiz.

Use the consequences to explain your decision, including the simpler alternative.

Apply the pattern to two situations. Select one option in each situation to build a decision record. Every option and its consequence stays visible, and you can change your choices.

When it helps—and what it costs

Use it when write rules and read shapes genuinely have different needs. Begin with the smallest separation that solves the problem.

Separate projections bring lag, replay, deletion and repair responsibilities. Commands must validate against the appropriate authority rather than a stale dashboard. Extra models can be unnecessary for straightforward CRUD operations.

Distinguish the neighbouring patterns

Event Sourcing stores authoritative event history and can work with CQRS, but CQRS does not require it. Denormalization concerns copied or precomputed read data and can appear in many designs.

Follow event history versus responsibility separation, copied read models, authoritative write constraints to compare the specific responsibility before combining patterns.

For further reading, use Microsoft’s CQRS guidance. Compare it with this original example and the guarantee required by your application.

Check your pattern reasoning

Does CQRS require separate databases?
Choose one answer. You can try again.

Answer and explanation

No; responsibilities can be separated over one store.

Physical separation is an additional design decision.

Check your pattern reasoning

Where should a command check a current balance invariant?
Choose one answer. You can try again.

Answer and explanation

At the appropriate authoritative write boundary.

A lagging read model can be unsuitable for approval.

Compare the pattern boundaries

Which statement distinguishes CQRS from Event Sourcing?
Choose one answer. You can try again.

Answer and explanation

CQRS separates command/query responsibilities; Event Sourcing makes event history authoritative.

They can be combined but neither name implies the other.

Name the responsibility before choosing the pattern

For CQRS, describe the problem, the participating roles and the observable contract. Use the example as a starting point and test the boundaries identified in the trade-offs. A pattern name alone does not establish correctness or performance.

Return to the design-pattern guide to compare families, or apply the idea alongside authoritative write constraints.

Reaching the recap and completing the quizzes are separate milestones.

    Next lesson: Event Sourcing Pattern →