Monday, January 8, 2024

API Gateway Pattern

 Problem

When an application is broken down to smaller microservices, there are a few concerns that need to be addressed:

  1. How to call multiple microservices abstracting producer information.
  2. On different channels (like desktop, mobile, and tablets), apps need different data to respond for the same backend service, as the UI might be different.
  3. Different consumers might need a different format of the responses from reusable microservices. Who will do the data transformation or field manipulation?
  4. How to handle different type of Protocols some of which might not be supported by producer microservice.

 Solution

An API Gateway helps to address many concerns raised by microservice implementation, not limited to the ones above.

  1. An API Gateway is the single point of entry for any microservice call.
  2. It can work as a proxy service to route a request to the concerned microservice, abstracting the producer details.
  3. It can fan out a request to multiple services and aggregate the results to send back to the consumer.
  4. One-size-fits-all APIs cannot solve all the consumer's requirements; this solution can create a fine-grained API for each specific type of client.
  5. It can also convert the protocol request (e.g. AMQP) to another protocol (e.g. HTTP) and vice versa so that the producer and consumer can handle it.
  6. It can also offload the authentication/authorization responsibility of the microservice.

 

 

Anti-corruption layer

 Problem

How do you prevent a legacy monolith’s domain model from polluting the domain model of a new service.

Solution

Define an anti-corruption layer, which translates between the two domain models.

Strangler Pattern

Problem

How do you migrate a legacy monolithic application to a microservice architecture?

 Solution

Modernize an application by incrementally developing a new (strangler) application around the legacy application. In this scenario, the strangler application has a microservice architecture.

The strangler application consists of two types of services. First, there are services that implement functionality that previously resided in the monolith. Second, there are services that implement new features. The latter are particularly useful since they demonstrate to the business the value of using microservices. Eventually, the newly refactored application “strangles” or replaces the original application until finally you can shut off the monolithic application.