AGGREGATOR DESIGN PATTERN IN MICROSERVICES

Hansini Rupasinghe
6 min readJun 9, 2021

--

AGGREGATOR DESIGN PATTERN

=======================================

Ref: https://images.app.goo.gl/DbynxNW5X5U78vKV6

There are 3 different ways that you can implement Aggregator pattern.

Let us describe the Aggregator design pattern using a real world scenario.

Use case:

๐Ÿ“ Assume you are asked to implement a microservice platform for an IT company. They have different systems, but your responsibility is to implement microservices.

Remember: We need to go with a DDD (Domain Driven Design)

Just think you came up with a small design like this where there are 4 different services and consumers who use these services.

Furthermore, you have Attendance Management System and a Project Management System.

At present, the company has a monolithic system that has everything in built. They can login to the system, do project allocation, check leave and attendance system etc. Your responsibility is to convert this to Microservice platform.

How can you do this???

Someone can create one microservice for attendance system and another microservice for project management system. And another microservice can be created if you have something like Claim Management System. The fact that you need to focus on is to deal with a Domain Driven Design. There should be one service that does well.

Solution:

๐Ÿ“ Create first service โžœ To get personal information

๐Ÿ“ Create second service โžœ To get personal information

๐Ÿ“ Create third service โžœ To get appraisal information

๐Ÿ“ Create fourth service โžœ To get allocation information

โœน You do not have any service to get personal and leave information for the attendance system.

โœน There is no service where you get personal and allocation information for project management system. And that is fine. But why???

You have 4 different services. Those are micro-level services. These services will not do anything alone other than what it decides to do. This is fine because of the concept โ€œ Do one thing and do it wellโ€ that is used in Microservices.

When you go with this kind of a pattern, you can create and deploy 4 or multiple dockers based on the demand since you have 4 different services.

Solution: Now you can create 1 service to consume those two microservices (personal information service & leave information service) and give response back to consumer. It will take the request from the consumer and invoke personal information service and leave information service, then aggregate responses and send back to the consumer.

How can you achieve the solution using;

Scatter Gather (Parallel) Aggregation Pattern

โ‰ You can send parallel calls to personal information service and leave information service. Then, get those responses, aggregate them as a single response and send back to consumer. This is called โ€œParallel Aggregationโ€.

Chaining Aggregation Pattern

โ‰ Just assume if your leave information service has a dependency from your personal information service.

Ex: The consumer sends Employee ID to you. Your leave system still does not know how to grab information from ID. May be the current version of leave system/database does not have ID. (Instead of that, it might contain something called โ€œEmployee Codeโ€) But user will send you the user ID.

โ‰ Now, you can invoke personal information service and get Employee code along with other information from that. And then pass this code into leave information service and get leave information. Now you can send aggregated response back to consumer. This is called โ€œService Chainingโ€.

(You call one service, get response and call another service)

๐Ÿ”ธ There is no requirement to get something from the first service, but one after the other.

If you go with Parallel Aggregation:

You can both (Personal & Leave) information in 10 ms (theoretically) which means you may be able to deliver your response within 15 ms.

If you go with Service Chaining Approach (one after the other):

First call will take 10 ms and second call too will take 10 ms. Altogether 20 ms. When you put your transformation time, it will take approximately 25 ms for the response.

  • * This is not good! **
  • This may be a better approach when you are migrating from monolithic applications to microservices platform since your backend may still run with some legacy platform.

๐Ÿ”” Assume you have an encoding service where you take some response and push it into encoding service. It must be Service Chaining method because you have to get some response and you should send this to encode/decode/encryption service. In such kind of scenarios, you can apply aggregation pattern using service chaining.

โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€” โ€”

Let us consider a use case.

Your project management service decides to check employee leave information before allocating a new project. Now, business logic has changed. Earlier Requirement was;

You have deployed in the production. You have 2 aggregate services in the production. They are for;

  1. Attendance System
  2. Project Management System

Currently;

Project Management System currently consumes 2 services.

  1. Personal information Service
  2. Allocation information Service

Now it is in the production. But their business requirement has changed eventually. They decide to check an employeeโ€™s leave behavior before allocating to a critical project.

Solution

๐Ÿ’ก In such a scenario, you do not have to manipulate any existing services. You can create a new aggregation service that will invoke all 3 services.(Personal Information Service, Leave Information Service 7 Allocation Information Service) Aggregation services do not cost much. It does not need much performance as it just aggregates things without performing any transformation logic.

๐Ÿ’ก You have 2 different services in your Project Management System. Now you can deploy both services in production. And you can ask consumers to migrate into this new version. And according to the scrum timeline/plans, consumers can slowly migrate into the new service that consumes all 3 backends. When you notice that all consumers have migrated from existing service, you can shutdown and keep the newer one alive.

Advantages of the Aggregator Pattern

๐Ÿ’  Reduces communication overheads among clients and services

๐Ÿ’  Easy to understand

๐Ÿ’  Easy to implement

๐Ÿ’  Can be developed fast

๐Ÿ’  Does not cost much

Disadvantages of the Aggregator Pattern

โ™จ๏ธ Latency of responses

โ™จ๏ธ Increased complexity

โ™จ๏ธ Availability issues

Instances where the Aggregator Pattern can be used

๐Ÿ”ฐ When there are Low Scale Needs

๐Ÿ”ฐ When business value functions are not critical

๐Ÿ”ฐ When a โ€œcombinedโ€ or monolithic service requires multiple teams to implement it

Considerations while using Aggregator Pattern

๐Ÿ”Ž Single Logical Point of Failure

๐Ÿ”Ž Install Circuit Breakers

๐Ÿ”Ž Real Time Monitoring

๐Ÿ”Ž Install Bulkheads

๐Ÿ”Ž Latency

References

--

--

No responses yet