Hexagonal Architecture: A GoLang Perspective

Ebubekir Yiğit
4 min readMay 30, 2023

--

Adaptability is the key.

In the fast-paced world of software development, change is constant. The need for a flexible, yet robust architectural approach has never been greater. Hexagonal Architecture or Ports and Adapters, shines in this respect, managing change with ease and elegance. With the rise of modular monolithic applications, maintaining scalability and simplicity in our architecture becomes even more crucial. Hexagonal Architecture helps us to navigate these complexities, fostering a design that is loosely connected, yet cohesive. It’s not just an architectural approach, it’s a pragmatic solution for the modern software design challenges we face today.

Let’s break down the fundamentals of Hexagonal Architecture.

Understanding Ports: In Hexagonal Architecture, Ports act as interfaces for the core of your application. They define what kinds of interactions your application can have with the outside world. There are two types of ports:

  • Primary or Driving Ports: These are the entry points into your application. They define the use-cases that your application exposes to the outside world. A REST API, a GraphQL schema, or a command line interface could act as a Driving Port.
  • Secondary or Driven Ports: These ports are used by your application to interact with external systems such as databases, third-party services, or even the system clock. They abstract the technical infrastructure from your application core.

Understanding Adapters: Adapters provide the actual implementation for the interactions defined by the Ports. They translate the data between the format most convenient for the Ports and the format most convenient for the external systems.

  • Primary or Driving Adapters: Driving Adapters convert external input, like HTTP requests or command-line inputs, into a format that the application can understand.
  • Secondary or Driven Adapters: Driven Adapters convert the application’s output into a format that the external system can understand. This could involve converting domain objects into database rows, or formatting an HTTP response.

Understanding these core concepts is crucial to effectively implementing a Hexagonal Architecture. They enable us to create a decoupled, testable system that remains isolated from external changes in infrastructure or technology.

Case Study: Food Ordering Service

For a practical illustration of Hexagonal Architecture, we’re going to design a simplified ‘Order Placement’ feature of a Food Ordering System using GoLang and MongoDB.

Our application will have one primary port, the OrderService interface:

This is the API our application provides to the outside world. Any client that wants to place an order will use this interface.

The primary adapter for this port is an HTTP handler that handles HTTP POST requests to the /order endpoint:

Now, let’s define our secondary port, the OrderRepository interface. The secondary adapter for this port is a MongoDB repository that knows how to save an order into a MongoDB database:

Order Model:

Time to put the pieces together:

The OrderHandler (primary adapter) depends on OrderService (primary port), OrderServiceImpl (application core) depends on OrderRepository (secondary port), and MongoDBOrderRepository (secondary adapter) implements OrderRepository.

Hexagonal Architecture has guided us to structure our application in a way that isolates the core logic from outside concerns. Now, we could swap out MongoDB for another database, or replace GoFiber with another HTTP framework, with minimal impact on our application’s core logic.

Advantages of Hexagonal Architecture:

  1. Separation of Concerns: Hexagonal Architecture promotes the separation of concerns by isolating the core business logic from external interfaces such as the UI or database. This enhances code maintainability and reduces the risk of errors.
  2. Improved Testability: Because of the clear separation between the application and the outside world, testing becomes easier and more effective. You can use mock adapters to create various test scenarios.
  3. Increased Flexibility: The architecture makes your application more adaptable to changes in the external environment, like the introduction of a new database or a change in the UI.
  4. Technology Agnostic: Since the core logic of the application doesn’t rely on external frameworks or technologies, you can easily switch technologies without modifying the business logic.
  5. Enables Concurrency: By decoupling external agents, different parts of the system can run concurrently, which can be beneficial in terms of performance and resource management.

Disadvantages of Hexagonal Architecture:

  1. Complexity: One of the main criticisms is that it can add unnecessary complexity to simple applications. It can be overkill for small projects or teams.
  2. Learning Curve: It can be difficult to understand and implement correctly, especially for developers who are accustomed to traditional layered architecture.
  3. Increased Development Time: Because of the separation of concerns and the creation of interfaces and adapters, it may increase initial development time.

Conclusion

Choosing the right architecture for your software project can make a significant difference in the long run. Hexagonal Architecture provides a robust framework for building scalable and maintainable applications, offering several compelling advantages like enhanced testability, increased flexibility, and technology-agnostic design. However, as with any architectural style, it’s not without its potential drawbacks.

While Hexagonal Architecture’s decoupling and port-and-adapter approach can add a level of complexity to your project, this could lead to overengineering, especially for simpler applications. Furthermore, it can initially be more time-consuming to set up due to its focus on separation of concerns and additional interfaces and adapters.

The key to leveraging Hexagonal Architecture, or any architecture, lies in understanding its trade-offs and matching it to your project’s specific needs and context. For complex, enterprise-level applications where flexibility, testability, and maintainability are paramount, Hexagonal Architecture could be a great fit. However, for simpler applications, it might not provide sufficient benefits to outweigh the added complexity. As with many aspects of software development, it’s all about using the right tool for the job.

References

  1. Alistair Cockburn’s original paper on Hexagonal Architecture
  2. Implementing the Clean Architecture by Uncle Bob Martin

Thank you! :)

--

--

Ebubekir Yiğit

Senior Software Engineer | Enthusiast in Software Architecture & Big Data