liveBook · Manning

View database

As explained in Microservices Patterns, Second Edition

A view database is a data store dedicated to maintaining and serving the data required by a CQRS view’s queries.

A view database stores the data needed to answer a CQRS view’s queries. Its technology and schema are chosen for those queries rather than for the commands handled by the services that own the data. It can support a distributed query without querying provider services at request time, or serve a single service’s queries that its command-oriented database handles poorly. section 13.0 section 13.2.1

Provider services publish events when their data changes. View event handlers process those events and update the database through a view repository; query handlers use the repository to read it. The database can hold prejoined or otherwise reshaped copies of provider data, so a query need not make runtime calls to the providers. section 13.0 section 13.2.1

Order history

An Order History Service maintains a view database using events from four provider services. By the time findOrderHistory() runs, the required data has been joined and stored in a form the query can read efficiently, rather than assembled through provider calls. section 13.2.1

Choose and design the database for both query and maintenance operations:

  • Match query requirements to the database’s data model, query and create, update, and delete operations, transaction model, and operational characteristics. Query operations matter most. section 13.3.1 section 13.4
  • Store returned data, additional data needed to evaluate queries efficiently, and data needed to maintain the view as events are processed. Design records, precomputation, and indexes around those operations. section 13.3.1 section 13.4
  • For small, infrequently queried data, consider a normalized relational schema with joins. Denormalization, indexes, and precomputation become more valuable when volume or data size makes efficiency important. section 13.3
Figure 13.5 A view database must support the CUD operations invoked by the view’s event handlers and the queries invoked by its query handlers. These two kinds of operations drive both design decisions: (1) selecting the database, and (2) designing a schema whose records and indexes store the query results, the data needed to evaluate the queries efficiently, and the data needed to maintain the view.
DesignViewDatabase

The diagram connects query and event-handler operations to the database and schema choices. section 13.3.1

A view database can improve query availability and latency and can scale independently of provider services. The cost is another database, event infrastructure, and potentially another service to build and operate. The view is eventually consistent: clients may not immediately see updates, and related data may arrive at different times. Maintenance must also handle concurrent updates, out-of-order events, and duplicates. section 13.2.2 section 13.4

Before creating a view, check whether an existing one can support the query. Reuse saves infrastructure and operational effort, but adding an unrelated query can reduce cohesion and couple queries that should evolve independently. section 13.3

How Microservices Patterns, Second Edition covers it

How deep it goes
Details database selection, schema design, event maintenance, and reuse trade-offs.
Examples
Illustrates an order-history view maintained from four provider services and queried by findOrderHistory().
What you can do after
Evaluate database and schema choices against query, maintenance, transaction, and operational requirements.
How it connects
Connects CQRS with API composition, event sourcing, and eventual consistency.

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.