Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hexagonal architecture and microservices: how do they fit together?

I was wondering how the hexagonal architecture relates to microservices. Do the microservices all go into the core of the hexagon? Or does each microservice get a hexagonal architecture? Or is it both (fractal)?

like image 918
Jonathan Aquino Avatar asked Feb 14 '19 18:02

Jonathan Aquino


People also ask

What is a hexagonal microservice?

Hexagonal architecture is a pattern that uses the mechanism of ports and adapters to achieve separation of concerns and isolate external systems and other external code such as user interfaces and databases from the core application.

What are microservices and how do they interact with each other?

Because microservices are distributed and microservices communicate with each other by inter-service communication on network level. Each microservice has its own instance and process. Therefore, services must interact using an inter-service communication protocols like HTTP, gRPC or message brokers AMQP protocol.

Which architecture is used in microservices?

Containers are a well-suited microservices architecture example, since they let you focus on developing the services without worrying about the dependencies. Modern cloud-native applications are usually built as microservices using containers.

Why is hexagon used in architecture?

Hexagonal Architecture promotes the separation of concerns by encapsulating logic in different layers of the application. This enables a higher level of isolation, testability and control over your business specific code. Each layer of the application has a strict set of responsibilities and requirements.


2 Answers

Hexagonal architecture is applied to one microservice.

And if you are using DDD:

  • At strategic level, each microservice would be a bounded context.

  • At tactic level, inside each microservice, the hexagon would enclose the application layer and the domain model. The adapters would be the infrastructure layer.

like image 92
choquero70 Avatar answered Oct 04 '22 23:10

choquero70


Do the microservices all go into the core of the hexagon?

A microservice is an entire service, if it was the inside the hexagon it would mean you cannot have any adapters inside so no way to expose your business logic on the network. So the business logic of a microservice is constrained to the inside of the hexagon, and all the technical layers, like your Rest Controllers, your persistence layer, your clients stay in adapters outside of your hexagon. You can check out this article on the hexagonal architecture.

does each microservice get a hexagonal architecture?

Yes and No. As Choquero70 said, in DDD/Hexagonal Architecture a microservice is sized after a bounded context. Let's say a single subdomain responsibility of the business of your platform e.g. billing, shipping, catalog for an e-commerce website. In that case a microservice can be built with the hexagonal architecture.

But sometimes you need to develop technical microservices for mappings, integration whatever. If a microservice is not dealing with business logic, it might be painful to use the hexagonal architecture, you'll end up with a lot of intermediary mappings to isolate a business responsibility which doesn't exist anyway.

like image 24
Julien Topçu Avatar answered Oct 04 '22 21:10

Julien Topçu