Materialized View Pattern

In this article, we are going to talk about Design Patterns of Microservices architecture which is The Materialized View Pattern. As you know that we learned practices and patterns and add them into our design toolbox. And we will use these pattern and practices when designing e-commerce microservice architecture.

By the end of the article, you will learn where and when to apply Materialized View Pattern into Microservices Architecture with designing e-commerce application system.

Step by Step Design Architectures w/ Course

I have just published a new course — Design Microservices Architecture with Patterns & Principles.

In this course, we’re going to learn how to Design Microservices Architecture with using Design Patterns, Principles and the Best Practices. We will start with designing Monolithic to Event-Driven Microservices step by step and together using the right architecture design patterns and techniques.

The Problem

Let me start with the problem. Think about that we have a use case that is “add item into shopping cart” use case of our e-commerce microservices application. You can see illustration in the below image.

According to image we have “Shopping Cart” , “Catalog” and “Pricing” microservices. And if the user add item into basket, how these microservices should interact each other ?

Should SC microservice query product data and price information to another microservices with sync calls ? Or are there any other way to handle this problem ? And also what if we have transactional use cases that need to interact several services with rollback features ?

In order to solve the questions, We will see patterns and principles for these problems. We will solve this problem with “Materialized View Pattern”.

Materialized View Pattern

So at that stage, Materialized View Pattern is very good option for this problem. Materialized View Pattern is recommend to store its own local copy of data in the microservice.

In our case, shopping car microservice should have table that contains a denormalized copy of the data which needed from the product and pricing microservices. We can also called this local copy of data as a Read Model. Thats why the name of pattern is Materialized View Pattern.

So Instead of the Shopping Basket microservice querying the Product Catalog and Pricing microservices, it maintains its own local copy of that data. By this way SC microservice eliminates the synchronous cross-service calls.

And also this makes SC microservice is more resilient, because if SC try to call catalog and pricing microservices and if one of the service is down, than the whole operation could be block or rollback. So this pattern broke the direct dependency of other microservices and make faster the response time.

Drawbacks of Materialized View Pattern

But this pattern has also drawback that we have to consider and make sure when deciding implement this pattern.

The main consideration is How and when the demormalized data will be updated ?

Because the source of data is other microservices and when the original data changes it should update into SC microservices. There are several way to handle this problem like using message broker systems, when data is changed in the original microservices publish an event and consumes from the subscriber microservice in order to update its denormalized table. Another way can be using a scheduled task, an external trigger, or a manual action to regenerate the table.

As you can see that we have learned Materialized View Pattern in our one of the use cases which is add item into shopping cart.

Design —Materialized View Pattern

You can find example implementation of Materialized View Pattern for our e-commerce architecture.

As you can see that we have applied Materialized View Pattern in Ordering microservices for our e-commerce architecture.

So we should evolve our architecture with applying new microservices patterns in order to accommodate business adaptations faster time-to-market and handle larger requests.

Step by Step Design Architectures w/ Course

I have just published a new course — Design Microservices Architecture with Patterns & Principles.

In this course, we’re going to learn how to Design Microservices Architecture with using Design Patterns, Principles and the Best Practices. We will start with designing Monolithic to Event-Driven Microservices step by step and together using the right architecture design patterns and techniques.

--

--

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