CQRS

CQRS (Command Query Responsibility Segregation) separates an application’s write side from its read side — commands change state through one model, queries read it through another model optimised for reads. The read models are kept in sync by projecting changes from the write side.

Spring DDD’s CQRS layer is a command bus, a query bus, and a projection dispatch and catch-up runtime. It is independent of event sourcing: it works with any persistence backend (JPA, plain JDBC, in-memory) and combines with event sourcing via the bridge starter when needed.

CQRS overview: commands change state through the write model; projections build the read models that queries read
Figure 1. The write side and the read side, kept in sync by projections

Dependency

The CQRS layer ships with whichever projection-store starter you choose:

// build.gradle.kts — JSON read models over JDBC (no Hibernate)
implementation("de.dwittkoetter:spring-ddd-starter-cqrs-jdbc:0.0.1-SNAPSHOT")

For JPA-@Entity read models, use spring-ddd-starter-cqrs-jpa instead. Either starter pulls in the command bus, query bus, and projection dispatch runtime from spring-ddd-cqrs. The universal bundles spring-ddd-starter-jdbc and spring-ddd-starter-jpa already include the CQRS layer (and combine it with event sourcing via the bridge starter). See Choosing your starters.

Configuration

The spring.ddd.cqrs.enabled property is the master switch for the whole layer. Setting it to false disables the entire CQRS infrastructure. The command pipeline (spring.ddd.cqrs.command.enabled) and query pipeline (spring.ddd.cqrs.query.enabled) are subordinate sub-switches that default to true and only take effect when the master switch is enabled; the projection runtime is likewise subordinate to the master switch.

For the full property reference, see the Appendix → Configuration Properties.

In this section

Commands

Defining commands and handlers, dispatching via CommandGateway, the transaction model, and the auto-managed aggregate command pattern.

Queries

Defining queries and query handlers, QueryGateway, and return-type resolution.

Projections

Read-model projections, catch-up and live dispatch, error policies, and projection repositories.

Commands and queries can also be routed to an external system instead of a local handler, selected by message type and without changing call sites. Routing is a cross-cutting messaging concern rather than a CQRS-only one — see Routing.