Hexagonal architecture: what is it and why should you use it?

Hexagonal architecture: what is it and why should you use it?

Franco Brutti

Aug 15, 2023

Aug 15, 2023

Aug 15, 2023

Hexagonal architecture: what is it and why should you use it?
Hexagonal architecture: what is it and why should you use it?
Hexagonal architecture: what is it and why should you use it?

There’s a type of software architecture that is quite popular and has gained its place among developers in recent years.

Today we want to tell you about hexagonal architecture, a different way of developing code, which, although complicated, is worth taking into account.

Not only in terms of work, but also in terms of solutions. Do you want to know why? We invite you to read on:

What is Software Architecture?

First, what is software architecture? We can't tell you about hexagonal architecture if we don't lay some groundwork first. 

The architecture of a system describes its main components, as well as their relationships and how they interact with each other. 

Software architecture and design include several contributing factors, such as business strategy, quality attributes, human dynamics, design and IT environment.

We can divide software architecture and design into two distinct phases: software architecture and software design. In architecture, non-functional decisions are shaped and separated by functional requirements. In design, functional requirements are met.

1. Software architecture

Architecture works as a model for a system as it provides an abstraction to manage the complexity of the system and establish a mechanism for communication and coordination between components.

It’s defined as a structured solution because it meets all technical and operational requirements while optimizing common quality attributes such as performance and security. 

In addition, it involves a set of important organizational decisions related to software development and each of these decisions can have a considerable impact on the quality, maintainability, performance and overall success of the final product. 

These decisions include:

  • Selection of the structural elements and their interfaces by which the system is composed.

  • Behavior as specified in the collaborations between these elements.

  • Composition of these structural and behavioral elements into a large subsystem.

  • Architectural decisions align with business objectives.

  • Architectural styles guide the organization.

 

2. Software Design

Software design provides a design plan that describes the elements of a system, how they fit together and work together to meet system requirements. The objectives of having a design plan are as follows:

  • Negotiating system requirements and setting expectations with customers, marketing and management personnel.

  • Having a model during the development process.

  • Guiding implementation tasks, including detailed design, coding, integration and testing.

Comes before detailed design, coding, integration and testing, and after domain analysis, requirements analysis and risk analysis.

What is hexagonal architecture?

Once we understand what software architecture is we can focus more on what hexagonal software architecture is, which we can say is a general purpose architectural style that aims to create decoupled software.

This architectural design allows applications to be driven by users, programs, automated tests or batch scripts, allowing it to be developed and tested in isolation from its possible runtime devices and databases.

This architectural style is also known as ports and adapters, and that’s because each of these aspects are the main properties of this pattern.

The key idea is to separate the core business logic from external concerns, such as databases, web frameworks, message queues... In this way, the application itself does not depend on any particular technology.

The general rule is that nothing goes in and nothing comes out without passing through the ports, which facilitates software testing.

Hexagonal architecture layers

The creation of dependency rules allows layers to be decoupled. The hexagonal architecture is also called "Ports and Adapters". 

External layers can only depend on internal layers, and internal layers must not depend on external layers. Each layer is defined as follows:

1. Infrastructure layer

The infrastructure layer contains code interfaces to the application infrastructure: drivers, user interface, persistence, and gateways to external systems. 

A web framework or persistence library will provide many of the objects in this layer. Domain repository concretions are placed in this layer, while virtual interfaces are defined in the domain layer.

2. Application layer

The application layer provides an API for all functions provided by the application. It accepts client commands (web, API or CLI) and translates them into values understood by the domain layer. 

For example, a RegisterUser service would accept a data transfer object containing the credentials of a new user and delegate the responsibility of creating a user to the domain layer.

3. Domain layer

The domain layer contains any core domain logic. It’s entirely about domain concepts and lacks knowledge of external layers.

Hexagonal architecture layers

What are the advantages and disadvantages of hexagonal architecture?

Although the area of software engineering has made great advances in recent years, no development has yet been experienced that is a solution to all software development problems, not even in the hexagonal architecture.

When designing software systems, in most cases we focus more on the benefits of a particular pattern or technology. However, making a decision based solely on the benefits it provides creates many problems in the long run.

Therefore, like any other software design pattern, hexagonal architecture comes with its good and bad sides.

Key highlights of the hexagonal architecture

These are the top 5 benefits of using hexagonal architecture as a software design pattern:

1. It’s easier to test.

This is one of the main benefits of Hexagonal Architecture. Because you can decouple the business rules from external concerns, such as database, framework, UI and other dependencies, it allows you to test those business rules independently.

Those rules depend on some abstractions (ports), and the concrete implementation can be easily circumvented. In turn, it makes it easier to write automated tests and also makes the tests run faster.

2. It has a higher degree of flexibility

This is another good quality of the hex architecture. The ability to switch between different technologies is what makes this pattern a snap-in style architecture.

Whenever you have a given port, what you need to do is change the specific implementations without having to touch anything within the business rules to make the specific, not global, modifications.

3. It’s considered agnostic technology

The parts of the hex architecture that contain the technology-specific code are the adapters, and these are the concrete implementations of the ports defined within the core domain.

That said, the source code of business rules is not affected by changes to a particular technology-specific adapter. Those changes are isolated at the adapter level.

4. Postponing decisions

When we start development, we can focus only on business logic, which allows us to delay decisions about which framework and technology to use. You can choose a technology later and code an adapter for it.

Deferring decisions is very useful in the sense that requirements may change and a decision on a specific technology may no longer be a good option.

While deferring decisions has great benefits, it’s always good not to defer decisions forever, just defer decisions until the last responsible moment.

5. Focus on the business domain

Since we understand that the main goal is to decouple business rules from technology-specific concerns, we can immediately start focusing on the most crucial aspect of the system, which is the business domain, and defer technology-specific decisions until the last responsible moment.

Top 5 benefits of using hexagonal architecture.

Disadvantages of hexagonal architecture

As expected, not everything is good with this type of architecture. Here we’re going to tell you about the disadvantages it has.

1. Debugging

Applications built with the hexagonal architecture pattern can be more difficult to debug because they do not directly use the concrete implementations that other software development systems use.

2. Indirection

Both patterns use indirect addressing which is understood as decoupling, so performance may suffer due to intermediate classes in development. 

For sure, an additional call will be made between the start and end of a service. There are additional lines of code, additional classes, hence additional complexity and extra effort to understand the design.

3. Translation

When the business domain is modeled independently of a database or other technology, the translation between the models used for persistence or communication and the domain model can be complicated. 

This problem worsens when the models have as a main feature a different development from other architectures, both technically and conceptually from each other.

4. Steep learning curve

Unlike traditional architectural patterns, which are mostly imposed on developers by frameworks, hexagonal architecture has a learning curve that is not precisely difficult, but it is steeper.

Indirection, translation, principles, and design patterns applied to enable this architectural pattern can be a challenge for software developers who are just getting started in this area, i.e. newcomers.

When can we implement the hexagonal architecture?

Yes, it offers interesting benefits, so we can say that it is a type of development that programmers should consider, but when should we use it? The answer is simple: it depends.

Simple applications may not benefit from this type of architecture, but when we are already facing more complex developments the matter changes a little, let's see some cases where it can be used:

  • A banking application that allows us to send money from one account to another.

  • A system that will allow us to request a loan, go through verification and receive an update when our application is updated.

  • A loyalty application that allows us to register customers and upgrade or downgrade their memberships.

While implementing hex architecture is a long-term investment the advantages it offers outweigh the disadvantages. 

Fact: Netflix uses hexagonal architecture.

Are you willing to learn how to use this architecture?

Let's be honest, if you are an entrepreneur it would be of little or no use for you to learn how to use it, but it would be worthwhile to learn a little more about it because if tomorrow you want to develop an application, the fact that your developers know and use it could make a difference.

However, if you are a developer, you might be interested in mastering the basic concepts and, if possible, the practical level of this type of architecture as well, since it would open up several options for you at a professional level.