As software grows, small shortcuts and reasonable compromises can accumulate into accidental complexity: tightly coupled components, blurred responsibilities, and changes that ripple unpredictably across a codebase. Hexagonal Architecture addresses this by making boundaries explicit and protecting business logic from frameworks, databases, user interfaces, and external services. Its goal is not to eliminate complexity, but to contain change and keep the system easier to understand and maintain.
The architecture separates the application core from the technologies around it. The core defines ports: input ports express the actions the application offers, while output ports describe the capabilities it needs from external systems. Adapters connect those contracts to concrete technologies, such as REST controllers, databases, message brokers, or external APIs. Through dependency inversion, adapters depend on core-defined interfaces, so dependencies point inward and technical details remain at the edges.
This structure supports focused tests of business rules without starting infrastructure, and makes external implementations easier to replace without rewriting the core. A typical request enters through a driving adapter, passes through an input port to a handler and domain logic, then uses output ports to reach driven adapters; results and errors return through the boundary. The approach brings some upfront structure and works best when its boundaries are applied consistently, so teams should adopt it pragmatically—especially where integrations, change, and long-term adaptability matter.
Metaphor of lasagna leaking technical constraints of the data model into all the layers.
A synthetic interface does not create a meaningful architectural boundary when the core still depends on an abstraction owned by the infrastructure layer. The dependency continues to point outward from the business core toward infrastructure, so the interface does not protect the core from changes in the implementation.
The core defines ports (interfaces). Adapters implement them. Dependencies always point inwards.
In traditional, layered architectures, dependencies flow downwards, whereas in the hexagonal architecture, dependencies flow inward.
Hexagonal Architecture for Article creation -- complete component diagram with flow.
Summary
Hexagonal Architecture isolates core business logic from external concerns using Ports (interfaces) and Adapters (implementations), with dependencies always flowing inward via the Dependency Inversion Principle.
The pattern solves accidental complexity, the maintainability decay that plagues growing codebases, by keeping domain logic framework-agnostic, testable in isolation, and swappable at the boundaries.
Real-world use cases include SaaS products needing multiple interfaces, technology migrations without touching business logic, and testing transformations from slow integration tests to fast unit tests.
Think of it as a pipeline that follows a clear flow: any request (in whatever flavor) arrives at a Primary Input Adapter, which calls an Input Port (an interface). The Handler implements it by orchestrating validation, domain creation, and persistence or any other external dependencies via Output Ports (again, interfaces). Results flow back as typed Either<Error, Void|DTO> values -- no hidden exceptions.
Key patterns include the Always-Valid Domain Model, functional error handling with Either, CQRS, and architectural testing with ArchUnit. The book uses Java + Spring Boot + Vavr + additional aid libraries, but the principles apply universally to them.
This is pragmatic architecture, not dogma. You will learn when to apply patterns rigorously, when to compromise, and how to introduce hexagonal architecture gradually to legacy codebases.
FAQ
What is Hexagonal Architecture?Hexagonal Architecture, also called Ports and Adapters, protects an application’s business logic by keeping external technologies outside the core. The core defines its boundaries, and adapters connect it to systems such as databases, APIs, and message brokers.What is accidental complexity, and how does Hexagonal Architecture help contain it?Accidental complexity is avoidable difficulty introduced by design choices, shortcuts, and tightly coupled components. Hexagonal Architecture makes boundaries explicit and keeps dependencies pointed inward, so changes to external technologies are less likely to spread into business logic.What are ports?Ports are interfaces defined by the application core. Input ports describe what the application can do, such as creating an article. Output ports describe what the core needs from outside, such as saving an article or looking up an author.What are adapters, and what is the difference between driving and driven adapters?Adapters translate between a port and a concrete technology. Driving adapters, such as REST controllers or message consumers, initiate interactions with the core through input ports. Driven adapters, such as database implementations or HTTP clients, fulfill needs expressed through output ports.What does dependency inversion mean in Hexagonal Architecture?Dependency inversion means the core defines the abstractions it needs, while external implementations depend on those abstractions. For example, a handler depends on a core-defined repository interface, and a JPA adapter implements that interface. Dependencies point toward the core rather than from the core toward infrastructure.Why is an interface not automatically a meaningful architectural boundary?An interface alone does not guarantee decoupling. Its ownership and dependency direction matter: if the infrastructure layer owns an interface that merely mirrors its implementation, the core may still depend on an abstraction shaped by infrastructure. A meaningful port is defined by the core to express the core’s needs.How does Hexagonal Architecture make the application easier to test?The business core can be tested without starting a framework or connecting to a real database. Tests can exercise handlers using mocks or other test adapters that implement the core’s ports, keeping tests focused and fast.How does an article-creation request flow through a hexagonal application?A REST controller maps the incoming request to a command and calls an input port. A handler orchestrates the use case, applies business rules, and calls output ports as needed—for example, to look up an author and save the article. Driven adapters implement those output ports, and the result returns to the controller for mapping to an HTTP response.Does Hexagonal Architecture mean a system has exactly six ports or adapters?No. The hexagon is a visual metaphor for the boundary around the application core, not a limit on the number of sides, ports, or adapters. The architecture emphasizes the distinction between the core and external interactions, rather than a top-down arrangement of layers.When might Hexagonal Architecture be unnecessary or a poor fit?It can add boilerplate, a learning curve, and extra tracing overhead. For simple CRUD applications, prototypes, or stable systems with little need to isolate or change external technologies, a conventional layered approach may be more practical. The chapter recommends treating architecture as a trade-off rather than assuming one style is best for every system.
pro $24.99 per month
access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!