liveBook · Manning

CQRS

As explained in Microservices Patterns, Second Edition

A distributed query pattern that maintains a read-only queryable replica combining data from multiple services.

CQRS (Command Query Responsibility Segregation) separates read and write models. For a query that needs data from multiple services, it maintains a read-only, query-optimized view rather than querying the owning services at request time. It can also support a single service whose database is poorly suited to a query. section 7.6.2 section 13.2

Provider services publish events when their data changes. View event handlers consume those events and update a view database; query handlers use a repository to read it. The view’s database and schema can be chosen for the query rather than the providers’ write models. section 13.0 section 13.2.1

Figure 13.3 The structure of the CQRS pattern. The Order History Service implements the findOrderHistory() view query by querying the Order History View database, which its view event handlers keep up to date by subscribing to the events published by the four provider services.
QuerySideService

The diagram shows provider events updating a view database and the query reading that database. section 13.2.1

Order history search

An order history query searches a consumer’s orders by restaurant name or menu item. API composition is awkward because not every service can filter on those attributes: the Delivery Service, for example, does not know the order contents. An Order History Service instead maintains a view from provider events, with data already joined for the query. section 12.2.4 section 13.2.1

A pre-joined view can avoid large in-memory joins and query-time calls to provider services, improving efficiency and reducing runtime coupling. Different views can use different database technologies and schemas for their queries. section 13.2.2 section 13.4

The cost is an additional view, event infrastructure, and possibly a service to build and operate. The view is eventually consistent: clients might not immediately see their own writes, and related data might arrive at different times. Event contracts also require coordination between provider and view teams. section 13.2.3 section 13.4

Design and selection guidance:

  • Use CQRS selectively for queries that are inefficient or impractical with API composition; use direct local queries or API composition where they suffice. section 13.2 section 12.2.4
  • Choose a view database for its query operations and other relevant features. Design the schema around the queries and updates, including record grouping, precomputation, and indexes. section 13.4
  • Publish events reliably with database updates, and design handlers for duplicates, out-of-order events, and replay. Treat provider events as contracts that must evolve compatibly. section 13.2.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Details view components, database and schema choices, event handling, and operational trade-offs.
Examples
Illustrates an order history search using a view assembled from provider events.
What you can do after
Evaluate whether a query warrants CQRS and identify key view-design and event-handling decisions.
How it connects
Connects CQRS to API composition, event sourcing, and the transactional outbox.

Also explained in

Compare how 3 books explain CQRS →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Microservices Patterns, Second Edition. The section links point to the parts of the book it draws on.