liveBook · Manning

Chapter 13: Using the CQRS pattern to implement system queries

What you will learn in chapter 13 of Microservices Patterns, Second Edition

The introduction explains why some queries need a query-specific CQRS view instead of querying data-owning services, and identifies the resulting tradeoff.

After this chapter you will be able to

What it builds on

Section by section

Introduction

The introduction explains why some queries need a query-specific CQRS view instead of querying data-owning services, and identifies the resulting tradeoff.

Key terms: API Composition, API composer, runtime coupled, commands, CQRS pattern, view database, provider services, events, eventually consistent, view

13.1 When API Composition isn’t the solution

This section introduces two query problems that API Composition cannot solve efficiently: distributed queries and single-service queries limited by the service’s database.

13.1.1 Distributed queries that can’t use the API Composition pattern

The section uses a consumer’s order-history query to show why API composition becomes inefficient when filtering, sorting, and pagination depend on data spread across services.

Key terms: findOrderHistory(), OrderHistory, fulfillment status, orderCreationDate, provider, API composer, in-memory join, cutoffDate, Order Service, bulk fetch API

13.1.2 Challenging single-service queries

This section explains why queries confined to one service can still be difficult to support with that service’s primary database.

Key terms: Restaurant Service, findAvailableRestaurants(), hybrid query, CQRS pattern, indexes, read performance, write throughput, denormalized schema, pre-joins, normalized and denormalized representations

13.2 Using the CQRS pattern

The section introduces CQRS as an event-updated, query-optimized read model and explains when its separation of reads and writes is useful.

Key terms: CQRS, query-optimized database, events, API Composition, single service, multiple services, Command-Query Separation, CQS, Command-Query Responsibility Segregation, read and write models

13.2.1 Overview of the CQRS pattern

The section explains the participants in a CQRS view and how provider events keep its database ready to answer system queries.

Key terms: provider services, view subdomain, CQRS view, view service, view database, view event handlers, update path, provider event, view event handler, view repository

13.2.2 The benefits of the CQRS pattern

This section explains five benefits of CQRS: efficient distributed queries, reduced runtime coupling, query-specific optimization, simpler responsibilities, and querying over event-sourced data.

Key terms: CQRS view, API Composition pattern, in-memory joins, pre-joins, runtime coupling, view service, provider services, partial failure, service database, view database

13.2.3 The drawbacks of the CQRS pattern

This section explains the development, infrastructure, operational, consistency, and coupling costs of CQRS and how those costs affect when to use it.

Key terms: provider services, view services, event contracts, transactional outbox, idempotency, message broker, view databases, consumer lag, end-to-end latency, replication lag

13.3 Designing a CQRS view for a system query

This section explains how query requirements and scale shape CQRS view design, then weighs enhancing an existing view against creating a new one.

Key terms: identity query, relationship query, predicate query, analytical query, view schema, view repository, normalized schema, denormalizing, indexing, precomputing

13.3.1 Selecting the view database

Explains how to select a CQRS view database and schema by matching the view’s query and event-driven update requirements to database features.

Key terms: CQRS view, view database, schema, data model, record, secondary indexes, application-maintained index, query operations, filtering, sorting

13.3.2 Designing the view schema

This section explains how to design a CQRS view schema so queries are fast while event handlers can efficiently update the view.

Key terms: CQRS view, CUD operations, provider entities, grouping, denormalize, records, event handler, precompute, aggregate, sum and count

13.3.3 Designing the view repository

This section explains how a CQRS view repository serves queries and applies events while handling concurrent, out-of-order, and duplicate updates and the view’s eventual consistency.

Key terms: view repository, find methods, query handlers, CUD methods, primary key, foreign key, secondary index, concurrent updates, aggregate instance, pessimistic locking

13.3.4 Designing the provider events and their handlers

This section explains how to design the provider events that supply a CQRS view and how event handlers pass those events to the view repository.

Key terms: provider events, view database, Order History view, domain event, aggregate’s ID, related aggregates, foreign keys, OrderCreated, event contracts, provider services

13.3.5 Deploying and updating the view service

This section explains where to deploy a CQRS view and how to initialize or rebuild one after an application has been running.

Key terms: CQRS view, standalone service, provider services, event handlers, message broker, event history, event versions, snapshot event, aggregate, state change events

13.4 Summary

Concepts explained in this chapter

← Chapter 12: Using the API Composition pattern to implement system queries

These guides are generated from the text of Manning books by Manning's knowledge graph and link to the sections they draw on. The full books are available on liveBook and at manning.com.