Querying Microservices with the CQRS and Materialized View Pattern

How to speed up and scale-out inter-service queries using a dedicated materialized view database that caches queries.

Photo by Cleyder Duque from Pexels

Background

The Membership portal UI

The UI shows an aggregated view of a loyalty membership

Implementation choices

Distributed queries

API composition pattern

API Composition requires synchronous API orchestration and in-memory joins
  • Latency — Synchronous invocations of multiple services introduce latency. It could be a sub-seconds range to minutes, based on each service’s response time.
  • Availability — Synchronous service invocations force you to deal with the availability of downstream services. Also, you’ll have to introduce additional measures like circuit breakers and exponential backoff to the code.
  • Scalability and performance — When the number of records grows in each database table, querying takes more time. Apart from that, the UI access pattern forces you to run the query whenever the UI is refreshed. You’ll have to scale the MemberShipView and the downstream services proportionally to meet the increasing demand.
  • Integration — Some packaged systems like CRMs may not have APIs exposed with the format that MemberShipView is interested in. So the service has to rely on integration middleware to get the data out and perform inefficient in-memory joins.

What if we maintain the data set required to populate the UI local to the MembershipView service?

Querying with CQRS and materialized views

  1. Building the materialized view
  2. Event-driven update of the materialized view
  3. Serving queries

1. Building the materialized view

Denormalized view of a membership profile.

2. Event-driven update of materialized view

Serving the queries

Benefits

Challenges

Eventually-consistent materialized views

Handling concurrent view updates

Handling the duplicate delivery of change events

Takeaways

--

--

EdU is a place where you can find quality content on event streaming, real-time analytics, and modern data architectures

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Dunith Dhanushka

Editor of Event-driven Utopia(eventdrivenutopia.com). Technologist, Writer, Senior Developer Advocate at Redpanda. Event-driven Architecture, DataInMotion