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)?
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.
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With