Lesson 4 of 9 · 27m
CQRS Pragmatics: Separate Models Only When the Mismatch Is Real
Understand when command and query models diverge, how to implement simple CQRS, and why event sourcing is not a prerequisite.
CQRS has hurt a lot of teams because the acronym travels better than the tradeoff. At its useful core, CQRS says: the model optimized for changing state need not be the same model optimized for reading state. That is all. It does not require microservices, event sourcing, Kafka, or a diagram with twelve arrows.
Start with the mismatch
Commands answer "may this change happen?" They need invariants, concurrency control, and consistent aggregate state. Queries answer "what should the screen show?" They need joins, filtering, sorting, and denormalized convenience. If both are simple and the same rows serve both well, do nothing. A repository method is not a crime.
The mismatch becomes real when screens need shapes the write model cannot provide cheaply, or when command invariants are buried under reporting concerns.
Simple CQRS is just separate paths
class CloseInvoiceHandler:
def __init__(self, invoices: InvoiceRepository):
self.invoices = invoices
def handle(self, command: CloseInvoice) -> None:
invoice = self.invoices.get_for_update(command.invoice_id)
invoice.close(actor=command.actor, at=command.at)
self.invoices.save(invoice)
class InvoiceListQuery:
def __init__(self, db: Connection):
self.db = db
def for_organization(self, organization_id: str, cursor: str | None):
return self.db.execute(
'''
SELECT invoice_id, status, amount_minor, currency, customer_name, due_at
FROM invoice_list_projection
WHERE organization_id = %s
ORDER BY due_at ASC, invoice_id ASC
LIMIT 50
''',
(organization_id,),
).fetchall()No framework is required. The write path protects invariants. The read path is a query optimized for the consumer.
Projections are derived data, not truth
A projection can be rebuilt from authoritative state or events. Treat it accordingly. Give it a schema version, a rebuild strategy, and operational visibility.
CREATE TABLE projection_checkpoint (
projection_name text PRIMARY KEY,
last_event_position bigint NOT NULL,
updated_at timestamptz NOT NULL DEFAULT now()
);When the projection is wrong, do not patch rows by hand unless you are in incident mode. Fix the projector and rebuild. Hand edits create untracked state that will lie to you later.
Event sourcing is a different decision
Event sourcing means the authoritative state is the event history. CQRS only means read and write models differ. You can use CQRS with ordinary state-based tables. You can technically use event sourcing without CQRS, though most systems separate query projections because replaying history per request is unacceptable. Do not accept event sourcing merely because someone wanted CQRS; it changes auditing, privacy, schema evolution, and debugging forever.
Consistency language saves arguments
If the query path is eventually consistent, say exactly how stale it may be and what users see immediately after writing. A read-your-own-write requirement is not negotiable in many workflows. Sometimes the answer is to return the command result directly instead of re-querying a lagging projection.
Command response: 202 Accepted with operation_id
List page: projection, typically fresh within 2 seconds
Receipt page: authoritative read model keyed by invoice_id
Support tooling: authoritative write-store query, not customer projectionThose distinctions belong in the design, not in tribal memory.
Field notes
- Do not put business validation in projections; they are consumers, not policy owners.
- If every query joins six write tables and every command updates one row, the pain is probably a missing aggregate, not missing CQRS.
- Version projection rebuilds. A migration that changes projection shape without a rebuild plan is an outage waiting for a deploy.
- Be explicit about pagination ordering.
updated_atis unstable under retries and clock precision. - CQRS doubles operational concepts. If the team cannot explain consistency lag in plain language, simplify.
Was this lesson helpful?