Browse patterns / Service architecture / B020
command query responsibility segregation

CQRS (Command Query Responsibility Segregation)

Separates read and write operations into different data models.

Recorded evidence 14/14Compare →Source
01 / UNDERSTAND

Structure and behavior

Follow the arrows step by step.

Reads and writes have separate responsibilities. Separate databases and event sourcing are not required.

Expected benefits

Independent optimization of read and write models can improve performance, scalability, and security.

Trade-offs

Introduces eventual consistency and read lag between data stores.

Not suitable where a write must be visible immediately in the same data, such as bank balances, inventory, or safety-critical control.

02 / APPLY

Operations and adoption guide

Synthesis, distinct from items backed by source excerpts. Validate before applying in production.

When to use

Fits systems where reads and writes have very different loads and require high throughput, low latency, complex domain logic, or strict audit and regulatory controls. Examples include stock trading and e-commerce with separate order and inventory queries.

Scenario

A midsize online shop handles thousands of product queries per second and relatively few order writes. A delay of a few seconds is acceptable when showing stock and order status just after ordering. The write side validates order and inventory commands, persists them to the write DB, and publishes events. The read side updates a separate model for product lists and order lookup. On the post-payment confirmation screen, use read-your-writes to show what was just saved and monitor read-model lag.

Placement principles

  • Use the write model and DB as the authority for command validation and transactions
  • Keep the read model and DB read-only, using replicas or cache as needed
  • Pass domain events from writes to reads through a broker
  • Deploy projection workers or indexers separately to update the read model
  • In Kubernetes, separate write and read services, Redis master and replicas, and Helm charts.

On premises

Deploy containerized command and query services, broker, and read/write stores to internal Kubernetes or VMs. Review network and storage isolation, security, privacy, TCO, and backup and recovery. Monitor replica lag and broker backlog, and standardize deployment and upgrade procedures.

03 / VERIFY

Evidence by criterion (14 total)

Evidence availability at the time of research. Assess source accuracy and currency separately.

Definition and overviewEvidence available

Separates read and write operations into different data models.

Segregate the read and write operations for a data store into separate data models.

learn.microsoft.com original ↗

Problem addressedEvidence available

Addresses the difficulty of optimizing different read and write performance and scaling needs with one model.

As applications grow, it can become increasingly difficult to optimize read and write operations on a single data model.

learn.microsoft.com original ↗

Key components and resourcesEvidence available

May include a web application or microservices and separate read and write stores.

It has basically three components: * A web app written in NodeJS (which can be thought of a microservice)

github.com original ↗

Data and asset placementEvidence available

Separate the authoritative write store from read replicas or read models.

A 'write' datastore (implemented using Redis master instance) * A 'read' datastore (implemented using Redis replica instances )

github.com original ↗

Typical use casesEvidence available

Fits systems with high performance or scaling needs, complex domains, or strict audit and regulatory requirements, such as a stock trading platform.

CQRS is great for a stock trading platform where performance, scalability, and regulatory compliance are paramount.

www.confluent.io original ↗

Example scenarioEvidence available

A user submits a form, sees previous data because the read model lags, and submits again.

the command succeeds, the read model has not caught up, the user's next page load shows old data

hld.handbook.academy original ↗

When to avoid and caveatsEvidence available

Not suitable where a write must be visible immediately in the same data, such as bank balances, inventory, or safety-critical control.

banking balances, inventory counts at checkout, safety-critical controls- don't tolerate eventual consistency well.

gainhq.com original ↗

On premises: development, deployment, and operationsEvidence available

Containerized microservices can run on on-premises Kubernetes.

You can take a containerized microservices-based application and deploy it into an on-premises Kubernetes cluster.

www.techtarget.com original ↗

Cloud: development, deployment, and operationsEvidence available

Containerize the workload, then deploy and test on managed Kubernetes such as OCI Kubernetes Engine.

After you containerize your microservices, you can deploy them to OCI Kubernetes Engine.

docs.oracle.com original ↗

Kubernetes: development, deployment, and operationsEvidence available

Use tools such as Node.js, Redis, and Helm to deploy and operate CQRS components on Kubernetes.

implementation of CQRS (Command Query Responsibility Segregation) architectural pattern on Kubernetes using NodeJS, Redis and Helm.

github.com original ↗

Incident responseEvidence available

The read model updates after the write, so queries may briefly omit the latest change.

the read models are updated after the write, so for a short interval a query may not reflect a change

design.babushkai.com original ↗

BenefitsEvidence available

Independent optimization of read and write models can improve performance, scalability, and security.

This approach allows you to optimize each model independently and can improve the performance, scalability, and security of an application.

learn.microsoft.com original ↗

Drawbacks and trade-offsEvidence available

Introduces eventual consistency and read lag between data stores.

The CQRS pattern typically results in eventual consistency between the data stores.

docs.aws.amazon.com original ↗

Representative tools and technologiesEvidence available

Examples include Kubernetes, Node.js, Redis, and Helm.

on Kubernetes using NodeJS, Redis and Helm.

github.com original ↗

Additional references (21)