What is Hexagonal Architecture?

07 June 2021 at 10:00 by ParTech Media - Post a comment

Most software applications are designed using the hexagonal architecture model. In such a model, you put outputs and inputs at the edge of the design. The central logic or the application’s core logic is separated from any outside interventions.

The outputs and inputs at the edge mean the swapping of their handlers is done without altering the core code. The major appeal of using hexagonal architecture is that it makes the testing of code easier.

Tests are made more stable with the ability to swap in the fakes. The hexagonal architecture was a clear departure from layered architecture. In a layered architecture, to enable testing, you can use dependency injection and other techniques. The UI could also be swapped in hexagonal models.

These were the core reasons for creating hexagonal architecture. In this post, we will understand in detail what is hexagonal architecture and what are its benefits.

Table of contents

  1. What is Hexagonal Architecture?
  2. Benefits of Hexagonal Architecture
  3. The disadvantage of Hexagonal Architecture
  4. Layers of the Hexagonal Architecture
  5. Conclusion

What is Hexagonal Architecture?

Alistair Cockburn proposed the Hexagonal architecture in 2005. Hexagonal Architecture is an architecture pattern that helps in solving problems related to the maintenance of applications in traditional architecture (Database-centric architecture). The core concept behind Hexagonal Architecture (also known as Ports and adapters pattern) is that the application is central to the system. A port isolates the inputs and outputs that are reaching or leaving the application from the external technologies, delivery mechanics, and tools.

To understand this concept better, let's quickly look into the below diagram.

As you can see in this diagram, there are two hexagons, one inside the other. The inner hexagon depicts the core of the application, the application logic, and the business. The adapter layer lies between the core and the outer hexagon. Each side of the hexagon is representing a port.

Just because a hexagon has six sides, it doesn’t mean only six ports can be present in hexagonal architecture. The hexagon is just used to represent the whole concept in simple language. The flat sides of a hexagon were chosen by Cockburn instead of a circle to specifically show the locations of ports.

Benefits of Hexagonal Architecture

  • Plug and play: This architecture provides us the ability to add and remove adapters according to our requirements during development. For example, we can interchange the GraphQL adapter with the REST adapter without changing the logic
  • Testability: We can write test cases for each component effortlessly as it decouples all the layers.
  • Adaptability/Enhance: We can always add a new way to interact with applications easily.
  • Sustainability: Maintenance becomes easier as all the third-party libraries can be kept in the infrastructure layer.
  • Database Independent: We can easily switch the database providers as the database is separated from the data access.
  • Clean Code: UI can be easily implemented as the business logic is kept away from the presentation layer like React, Angular, or Blazor.
  • Well Organized: New developers will easily get on board and will have a better understanding of the project as it is well organized.

The disadvantage of Hexagonal Architecture

  • Domain Layer becomes heavy as plenty of logic is implemented in this layer.
  • The level of complexity will increase dramatically as we are building the application with multiple layers of abstraction.
  • Having many layers of indirection and isolation usually increases the cost of building and maintaining the application. This cost is more than the benefits of the abstraction.
  • Most web applications will never require the switching of databases and frameworks. They will only be used through a browser. It is a noble goal to create applications with the potential to swap any implementation. However, most applications won’t need that level of adaptability. Hence, it will be a waste of time to build an application that is so ideal.

Layers of the Hexagonal Architecture

  • Domain API Layer: This layer is the central layer and does not have any dependency on any other layer. Since primary and secondary adapters should be able to implement the contract, it is a contract for domain layer interaction (ports). This is also called as Dependency Inversion Principle Domain layer (DIP).
  • Domain Layer (Business Layer): These layers are kept clean without any other dependencies. They have the business logic.
  • Rest Adapter Layer: The rest adapter layer is also called the Left port adapter. The primary adapter is the layer where the restful services are implemented like GET, POST, PUT, DELETE, etc.
  • Persistence Adapter Layer: This is the Rest Adapter which is also known as the right port adapter or the secondary adapter. This is the layer where the implementation of Entity framework core is done, which previously implemented a repository design pattern. A DbSet is the repository and DbContext is the Unit of Work (UOW). Bootstrap/Presentation Layer:- This is the final layer of the project. Everything begins here.


Hexagonal architecture is one of the popular methodologies available for building a web application. It completely depends on the type of project as to when and where to use this architecture. Segmenting the application into different layers of responsibility is good but prematurely using abstraction will result in a fool’s errand.