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.

Spring DDD’s CQRS layer is a command bus and a query bus. It is independent of event sourcing: it works with any persistence backend (JPA, plain JDBC, in-memory) and combines with event sourcing on its own when spring-ddd-starter-eventsourcing-jdbc is also on the classpath.

Dependency

The CQRS layer ships with the CQRS starters:

// build.gradle.kts
implementation("de.dwittkoetter:spring-ddd-starter-cqrs-jpa:0.0.1-SNAPSHOT")

The starter pulls in the command bus and query bus from spring-ddd-cqrs. The full-stack bundle spring-ddd-starter-jpa already includes the CQRS layer and combines it with event sourcing. See 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.

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

The read models the framework keeps up to date from your events have a section of their own: what a projection is, how it is fed, and how it scales and is rebuilt. Ad-hoc projections, which answer on demand without storing anything, are part of it: see Ad-hoc projections.

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.