Overview

1 Taming complexity with hexagons

This chapter introduces hexagonal architecture as a practical response to accidental complexity, the kind of mess that grows when shortcuts, framework coupling, and leaky abstractions accumulate over time. Rather than treating complexity as inevitable, it argues that the real problem is weak boundaries: when business logic is tied directly to databases, APIs, user interfaces, and other infrastructure, every change ripples outward and maintenance becomes increasingly expensive. The chapter frames architecture as a way to protect the core of the system so that changes remain local, tests stay fast, and the application can evolve without constant fear of breaking unrelated parts.

At the center of the approach is a clear separation between the application core and everything outside it. The core contains the domain rules, entities, and use cases, while ports define the contracts it exposes or requires. Adapters sit at the edges and translate between external technologies and those core contracts. Input ports represent what the application can do, output ports represent what it needs, and adapters implement the technical details such as REST controllers, repositories, message consumers, or external service clients. This structure relies on dependency inversion so that dependencies always point inward: the core defines the abstractions, and the external world depends on them, not the other way around.

The chapter also shows why this matters in modern systems and how the model works in practice through an article-creation example. A request enters through a driving adapter, is translated into a command, handled by a use case in the core, may invoke output ports for persistence or external data, and then returns through the boundary with explicit error handling. The result is a system that is easier to test, easier to swap infrastructure in and out of, and clearer for teams to evolve over time. The chapter closes by acknowledging trade-offs: hexagonal architecture adds structure and upfront boilerplate, so it is most valuable when systems are expected to grow, integrate with many technologies, or outlive their first implementation.

Without an architectural structure, technical debt compounds exponentially. Hexagonal architecture keeps costs manageable by isolating change impact.
Without an architectural structure, technical debt compounds exponentially. Hexagonal architecture keeps costs manageable by isolating change impact.
The core defines ports (interfaces). Adapters implement them. Dependencies always point inwards.
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.
In traditional, layered architectures, dependencies flow downwards, whereas in the hexagonal architecture, dependencies flow inward.
Metaphor of lasagna leaking technical constraints of the data model into all the layers.
Metaphor of lasagna leaking technical constraints of the data model into all the layers.
Abstraction of a fake boundary pretending to protect the core and failing to do so.
Abstraction of a fake boundary pretending to protect the core and failing to do so.
Hexagonal Architecture for Article creation -- complete component diagram with flow.
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, in simple terms?Hexagonal architecture is a way to structure software so the business core stays protected from frameworks, databases, user interfaces, and external services. The core defines what it needs through ports, and the outside world connects through adapters.
Why does hexagonal architecture matter for maintainable software?It keeps change impact local. When persistence, APIs, or UI details are kept outside the core, you can change infrastructure without rewriting business logic, which makes systems easier to test, refactor, and evolve.
What problem does hexagonal architecture solve?It helps tame accidental complexity caused by hard-wiring code to external technologies. Instead of letting dependencies leak everywhere, it creates explicit boundaries that contain ripple effects.
What are ports in hexagonal architecture?Ports are interfaces defined by the core. Input ports describe what the application can do, while output ports describe what the application needs from the outside world, such as saving data or calling another service.
What are adapters in hexagonal architecture?Adapters are concrete implementations outside the core. Driving adapters, like REST controllers or CLI commands, call input ports. Driven adapters, like JPA repositories or external API clients, implement output ports.
How does dependency inversion work in hexagonal architecture?The core depends only on abstract ports, not on concrete technologies. External adapters depend inward on those interfaces, which reverses the usual dependency direction and keeps the domain sovereign.
How does hexagonal architecture improve testing?Because the core is framework-agnostic, you can test use cases and domain logic with mocks or stubs instead of starting Spring, a database, or other infrastructure. That makes tests faster and more focused.
How does hexagonal architecture help when swapping databases or frameworks?Since the core talks to ports rather than concrete implementations, you can replace one adapter with another—such as JPA with PostgreSQL changes, or one web framework with another—without changing the business logic.
How is hexagonal architecture different from layered architecture?Layered architecture usually flows top-down and can let infrastructure decisions leak upward. Hexagonal architecture instead centers on an application core with inward-pointing dependencies and explicit ports/adapters around it.
When should you avoid using hexagonal architecture?It may be overkill for simple CRUD apps, prototypes, or stable systems where infrastructure is unlikely to change. It also adds upfront structure and a learning curve, so it is best when long-term adaptability and testability matter.

pro $24.99 per month

  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose one free eBook per month to keep
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime

lite $19.99 per month

  • access to all Manning books, including MEAPs!

team

5, 10 or 20 seats+ for your team - learn more


Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Clean Applications with Hexagonal Architecture Clean Applications with Hexagonal Architecture ebook for free
Close
choose your plan

team

monthly
annual
$49.99
$499.99
only $41.67 per month
  • five seats for your team
  • access to all Manning books, MEAPs, liveVideos, liveProjects, and audiobooks!
  • choose another free product every time you renew
  • choose twelve free products per year
  • exclusive 50% discount on all purchases
  • renews monthly, pause or cancel renewal anytime
  • renews annually, pause or cancel renewal anytime
  • Clean Applications with Hexagonal Architecture Clean Applications with Hexagonal Architecture ebook for free