Friday, November 29, 2024

Micro Services - Composition


Microservices architecture often requires composing multiple services to complete a single operation. Here are various composition patterns used in microservices: 

When an operation requires more than one service to complete, a composition pattern is used. This involves coordinating multiple microservices to achieve a single business goal. 

 

Broker Composition Pattern

In the Broker Composition pattern, a message is published to a message broker. All subscribers pick up this message and process their tasks. Once all the work is complete, the client gets a notification and can pull the relevant data back. This asynchronous communication provides flexibility, good performance, and reliability. 

 

Aggregate Composition Pattern

The Aggregate Composition pattern involves an aggregator component, often a Backend for Frontend (BFF), responsible for fetching data from multiple microservices. The aggregator connects with other microservices, aggregates the data, and returns it to the front end. However, synchronous calls between the aggregator and other microservices can lead to a poor client experience. 

 

Chained Composition Pattern

The Chained Composition pattern is considered an anti-pattern. In this pattern, the client calls Service 1, which calls Service 2, and so on, creating a chain of HTTP calls. This can lead to increased latency and complexity. 

 

Proxy Composition Pattern

In the Proxy Composition pattern, an API gateway connects with all downstream services and returns the information to the client. The gateway does not aggregate data; instead, the front end is responsible for making multiple gateway calls and aggregating the data. These endpoints are called passive endpoints because the proxy has minimal logic and acts as a pass-through. The API gateway can provide benefits such as a centralized layer for security checks and data caching. It can also handle API version mapping. However, the drawback is that the client has to make multiple calls, one for each service. 

 

Mixed Composition Pattern

The Mixed Composition pattern combines multiple composition patterns. It uses the Broker Composition pattern as the base and adds a BFF as an aggregator layer. Once a message is published to the broker and a notification indicates that the data is ready, the aggregator fetches the data points, aggregates them, and returns them to the client. The BFF can connect with individual services to get the data, avoiding a chain of calls. An API gateway (Proxy Composition pattern) can be introduced on top of the services to expose the data to more consumers.

 


No comments:

Post a Comment