Microservices (Part 3) — Design Patterns for Microservices(Aggregator Pattern)

Damsak Bandara
Nerd For Tech
Published in
4 min readJun 7, 2021
Fig 1: Design Patterns ( Source: dribbble )

Microservices is a completely new way of developing applications. Design patterns are a crucial part of this new implementation process. Let’s discuss some commonly used designed patterns.

Aggregator Pattern

A microservices application consists of several independent services. These services are less coupled and talk to each other through well-defined interfaces. Service has its own database as well. However, there may be instances where we need to write queries to get results from multiple databases which are in different services. We can use the Aggregator pattern to implement this.

In the aggregator design pattern, a particular service(service A) will receive the request. Then this service will subsequently make requests to multiple services(Service B, C). Then it(Service A) will combine the results and responds to the initial request. There are 3 main ways to implement the Aggregator pattern.

  1. Scatter-gather Pattern
  2. Chained Pattern
  3. Branch Pattern

Let’s try to further understand these 3 main implementations with the help of a real-world example.

Use case

Assume that we need to implement a microservices-based system for a Hotel. This system contains the following services.

  1. Service 1: Get Guest Information.
  2. Service 2: Get Room Information.
  3. Service 3: Get Food Information.
  4. Service 4: Get Restaurant Information.

Assume that there are 2 main consumers of these services.

  1. Guest management System.
  2. Restaurant management system.

Normally what we can do —

use docker to create multiple instances of these 4 services and deploy them.

Problem: Guest Management system needs Guest information and room information.

Solution

In the microservices architecture, we always implement dump pipes and smart endpoints. Therefore we can create another service to consume these 2 services(Guest Information service and Room Information service) and provide the response back to the consumer. There are 2 main ways to implement this.

Option 01: Send parallel responses to Guest information and Room information services. Then aggregate the results and send them back.

This implementation is called the Scatter gather pattern(Also called parallel aggregation). It is basically a composite pattern that describes how to broadcast a message to multiple recipients and re-aggregate the responses back to a single message.

But there is a problem. We can’t always use this pattern.

What if the Guest information service has a dependency on the Room information service?

Now we cannot send parallel calls.

Option 02:

  1. Invoke the Guest Information service.
  2. Get a unique code(Guest Code) along with the response.
  3. Pass the Guest code to the Room information service.
  4. Then get the Room information.
  5. Finally, aggregate the result and send it back.

This implementation is called Service Chaining. Service Chaining is slower than parallel aggregation.

Branching

This is basically a mix of Aggregator and Chain design patterns. In this pattern, a particular service can communicate with multiple services at a given time. We can use a field called “Branch code”. Then the destination place of the request will be determined by this branch code(It could also be the next service invoking place).

Fig 2: Branching

Change in business requirements

New Requirement: Restaurant management needs some information from the Guest management before performing its operations.

Now we can implement this functionality without modifying any of the existing services. This is done by creating another aggregation service. This aggregation service will talk to necessary services(In Guest management) and get the information.

The deployment should be done with the help of versioning. We can deploy both versions of the system and ask the clients to upgrade to the newer version. Both the versions should be running for a particular time period(Till all the clients upgrade to the new version). Finally, we can remove the older version of the system.

Advantages of the Aggregator Pattern

  • Reduce the communication overhead between clients and services.
  • Easy to understand and Implement.
  • Fast development.

The next part of this article plains another widely used design pattern called Circuit Breaker Pattern. I have used the following playlist by Mr.Krishntha Dinesh to gather the required information.

--

--