Overview

1 Taming complexity with hexagons

Hexagonal architecture is presented as a practical response to the slow decay of software systems caused by accidental complexity. The chapter argues that codebases become hard to change when business logic is tightly wired to frameworks, databases, user interfaces, and external services, because even small changes then ripple across the system. Rather than treating architecture as decoration, the pattern is framed as a way to keep the most important parts of the application protected, stable, and easier to reason about over time.

At its core, the approach separates the application into an inner business core and an outer ring of technology-specific adapters. The core defines ports, which are interfaces for what the application can accept and what it needs, while adapters implement those ports on the outside. This means controllers, repositories, message consumers, and external clients can all be swapped without forcing changes in the domain logic, because dependencies are inverted and always point inward toward the core.

The chapter also shows how this structure works in practice through an article-creation flow: a REST controller translates requests into a command, an input port hands that command to a handler, the handler enforces rules and coordinates the domain, and output ports reach out to persistence or other services through adapters. The main benefits are stronger testability, clearer contracts, easier substitution of infrastructure, and a framework-agnostic core that can survive migrations and integration changes. At the same time, the text is pragmatic about trade-offs: hexagonal architecture adds boilerplate and discipline, so it is most valuable when systems are expected to grow, integrate heavily, or evolve for a long time.

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.
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.
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.

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 and why is it important?Hexagonal architecture is a way to structure software so the application core stays protected from frameworks, databases, and external services. It matters because it reduces accidental complexity, makes change easier to contain, and keeps the business logic testable and maintainable.
What is accidental complexity in a growing codebase?Accidental complexity is the extra mess created by design choices such as tight coupling, leaky abstractions, and framework-driven domain logic. It causes small changes to ripple through many parts of the system and makes the code harder to understand and maintain.
How does hexagonal architecture help tame accidental complexity?It tames accidental complexity by creating explicit boundaries around the core and forcing dependencies to point inward. This keeps infrastructure concerns at the edges, so changes in databases, APIs, or messaging systems do not spread into the domain logic.
What are ports in hexagonal architecture?Ports are interfaces defined by the core to describe what the application can do and what it needs from the outside world. Input ports represent use cases, while output ports represent dependencies such as repositories, external services, or notifications.
What are adapters in hexagonal architecture?Adapters are concrete implementations that connect external technologies to the ports defined by the core. Driving adapters, like REST controllers or CLI tools, call input ports, while driven adapters, like JPA repositories or API clients, implement output ports.
What is dependency inversion in hexagonal architecture?Dependency inversion means the core defines abstractions and the external world depends on them, not the other way around. This keeps the domain independent of specific frameworks or infrastructure choices and makes the architecture more flexible.
Why are boundaries so important in software architecture?Boundaries prevent responsibilities from leaking across layers and stop external concerns from shaping the core domain. When boundaries are weak, code becomes harder to reason about, harder to test, and more expensive to change.
How does hexagonal architecture improve testing?Because the core depends only on interfaces, it can be tested as plain code with mocked ports and without starting Spring, a database, or other infrastructure. This makes tests faster, more focused, and easier to maintain.
How does hexagonal architecture compare to layered architecture?Layered architecture typically has dependencies flowing downward, which can make infrastructure decisions ripple upward into business logic. Hexagonal architecture instead makes dependencies flow inward, keeping the application core independent and the external systems swappable.
When should you not use hexagonal architecture?It may be overkill for simple CRUD apps, prototypes, or stable systems that are unlikely to change much. In those cases, the added boilerplate and learning curve may not be worth the benefits of stronger boundaries and adapter flexibility.

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


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 ebook for free
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 ebook for free