CQRS
In an ordinary service one model both handles writes and answers queries: the Order entity validates changes, and then the same entity is joined with five tables to render the order list. While reads and writes look alike, that is fine. CQRS starts where they diverge: writes are rare and guard invariants, reads are massive and need search, facets, and data in the shape of a screen. Then the model is split in two — commands go through a write model, and queries are served by a separate read model built from the write side's changes.
The central trap of this topic is confusing CQRS with neighbouring ideas. It is not event sourcing: a read model can be built from plain outbox events while state lives in ordinary tables. It is not microservices: two models live happily in one process and one database. The second trap is underestimating the cost: a separate read model lags behind writes, projectors must be run, monitored, and rebuilt, and redeliveries must be deduplicated. This topic breaks CQRS into layers — from the definition to when not to use it. Brokers, delivery guarantees, and the outbox are covered in depth in Messaging; here they are only linked.
Topic Map
- CQRS basics — how the method-level CQS rule differs from model-level CQRS, and what commands and queries are.
- Write and read models — degrees of separation from separate queries in one DB to a separate store, and denormalized models shaped per screen.
- Command and query handlers — generic interfaces, transaction and logging decorators, and package layout in a Go service.
- Projections — the projector, a checkpoint in the same transaction as the read model, determinism, and rebuilding from scratch.
- The inbox pattern — consumer-side deduplication through a table of message ids in the same transaction as the effect.
- Read-model lag — eventual consistency, read-your-writes strategies, and lag monitoring.
- When CQRS is not needed — the cost of two models, CRUD cases, and applying it per bounded context.
Common Mistakes and Traps
| Mistake | Consequence |
|---|---|
| Treating CQRS and event sourcing as one pattern | An extra event store where outbox and tables would do |
| Thinking CQRS requires microservices or two databases | Splitting a service for a pattern that lives in one process |
| Changing state inside a query — a view counter in a read handler | Reads stop being safe to retry and cache |
| Returning read-model data from a command | The command depends on screen shape again; the split loses its point |
| Committing the checkpoint or offset before writing the read model | A crash between the steps loses the event forever |
| Applying a non-idempotent increment without deduplication | A redelivery doubles a balance or a counter |
| Handling each message in its own goroutine | One entity's events are applied out of order |
| Rebuilding a read model in the same table | Users see half-filled data |
| Promising users that changes show up instantly | "I saved it and nothing changed" complaints caused by lag |
| Rolling CQRS out across the whole system at once | Two models and projectors in CRUD parts with no payoff |
Interview Relevance
CQRS is a common topic in senior Go backend interviews, especially at companies with an event-driven architecture. The definition is a warm-up; then the interviewer checks whether you understand how the read model stays in step with writes and what that costs.
What is usually checked:
- How CQS differs from CQRS, and why CQRS needs neither event sourcing nor separate services.
- Why a read model is denormalized and why it holds no invariants.
- How a projector works: application order, a checkpoint in one transaction with the data, rebuilding from position 0.
- How to make a consumer idempotent under at-least-once delivery — an inbox or a version check.
- Why a user does not see their change right away and how to give them read-your-writes.
- What handlers and decorators look like in a Go service and where the transaction boundary lies.
- When CQRS does not pay off.
Typical wrong answer: "CQRS is when events go to Kafka and reads come from another database." It mixes CQRS up with a broker and event sourcing and says nothing about the core — different models for different jobs and the cost of keeping them in sync.