Stackbook Logo
architecture-descriptionestablished · high operational burden

CQRS (Command Query Responsibility Segregation)

Also known as: command-query-separation, cqrs-pattern

Intent

Separate read and write models to optimize each for its specific concerns: writes for consistency, reads for query performance.

Problem

Single model serves both writes (consistency, validation) and reads (joins, projections, denormalization) — compromises both.

Forces

  • Write model needs: transactions, invariants, normalization
  • Read model needs: denormalization, pre-computed joins, caching
  • Read/write scaling requirements differ (reads >> writes typically)
  • Complex queries on normalized write model are slow

Solution

✓ When to Use

  • Read/write patterns differ significantly
  • Complex queries on normalized data
  • Different scaling needs (read replicas vs write primary)
  • Multiple read representations (UI, reporting, API)

✗ When Not to Use

  • Simple CRUD with straightforward queries
  • Strong consistency required between read/write
  • Team not ready for eventual consistency
  • Overhead not justified by query complexity

Pros

  • +Each model optimized for its job
  • +Independent scaling of read/write
  • +Read model evolution without write impact
  • +Natural fit with event sourcing

Cons

  • Eventual consistency between write and read
  • Complexity: two models, synchronization, projection lag
  • Duplicated logic risk (validation in both?)
  • Operational burden: monitor projection health

Cost Profile

Infrastructure

Medium — separate read/write stores, projection workers

Operational

High — monitor lag, handle projection failures

Cognitive

Medium — model separation, consistency boundaries

Failure Modes

  • Projection lag → stale reads (user sees old data)

  • Projection failure → read model corrupt, needs rebuild

  • Write model invariants not enforced in read model

  • Dual-write inconsistency if not using event sourcing

Real-World Examples

Alternatives

  • traditional-layered
  • event-sourcing
  • materialized-views

Related Patterns

  • event-sourcing
  • materialized-view
  • read-replica
  • outbox-pattern

Competency Domains

data statedistribution communicationreliability opseconomics evolution