Overview

5 Hexagonal Architecture: Protecting the Core: The Power of Ports and Adapters

Hexagonal Architecture is presented as a way to free business logic from the pull of databases, web frameworks, and other infrastructure that often dominate traditional layered designs. Instead of building software on top of a database, the chapter argues for building around a protected core so the most important rules can be tested and changed without dragging the rest of the system along. This shift is framed as a move from thinking in terms of top and bottom layers to thinking in terms of an inside and an outside, with the architect acting as a boundary keeper.

The pattern is built from three parts: the inside, which contains the domain rules and use-case orchestration; ports, which are interfaces that define what the core offers and what it needs; and adapters, which translate between the core and external systems. Primary adapters bring in requests from users, tests, or APIs, while secondary adapters handle persistence, messaging, or other outgoing needs. The key idea is inversion: data may still flow outward to a database or service, but the dependency points inward so the core never depends directly on external technologies.

The chapter illustrates this with examples like a server monitor that can send alerts through different adapters, such as SMS, console output, or Kafka, without changing the core logic. This makes the application easier to test, easier to extend, and less vulnerable to vendor or infrastructure changes, though it comes with added complexity in the form of more interfaces, mapping code, and indirection. It also explains the pattern’s history, notes its relationship to other architecture styles, and emphasizes that while Hexagonal Architecture is powerful for complex, long-lived systems, it is not always worth the cost for simple CRUD applications.

Layered Architecture can act like an anchor pulling everything down to the Database, while Hexagonal Architecture frees you of this concern.
Layered Architecture can act like an anchor pulling everything down to the Database, while Hexagonal Architecture frees you of this concern.
In the Leaky System, database changes "bleed" through the layers, breaking the UI and business logic. In the Sovereign System, the Application Core is protected by Ports and Adapters, ensuring that external changes to the database or UI never touch your core business rules.
In the Leaky System, database changes "bleed" through the layers, breaking the UI and business logic. In the Sovereign System, the Application Core is protected by Ports and Adapters, ensuring that external changes to the database or UI never touch your core business rules.
Visualizing the “Clean Room” being built with Hexagonal Architecture
Visualizing the “Clean Room” being built with Hexagonal Architecture
Display showing how Ports interact with the Domain. Ports can share interfaces or be distinct; it all depends on cohesion (e.g., one IOrderPort vs. separate Ports).
Display showing how Ports interact with the Domain. Ports can share interfaces or be distinct; it all depends on cohesion (e.g., one IOrderPort vs. separate Ports).
Adapters translate the messy language of the outside world (JSON, SQL) into the clean language of the Core, and back again.
Adapters translate the messy language of the outside world (JSON, SQL) into the clean language of the Core, and back again.
The Data goes out, but the Dependency points in. This is the Inversion.
The Data goes out, but the Dependency points in. This is the Inversion.
The Laptop doesn’t care what device is plugged in as long as it fits in the Port!
The Laptop doesn’t care what device is plugged in as long as it fits in the Port!
A Medieval Castle is a perfect visual to picture how Hexagonal Architecture works in principle!
A Medieval Castle is a perfect visual to picture how Hexagonal Architecture works in principle!
The Extensible Hexagon. Because the Application Core only communicates through defined Ports, you can easily plug in new "Future Adapters" (like a Mobile App or Admin CLI) without modifying any of your existing business logic.
The Extensible Hexagon. Because the Application Core only communicates through defined Ports, you can easily plug in new "Future Adapters" (like a Mobile App or Admin CLI) without modifying any of your existing business logic.
On the Driven Side, the Domain Logic never talks to the database directly; it simply calls a Secondary Port (Interface). The actual database or email server is hidden inside a Secondary Adapter that implements the interface, allowing you to swap out your infrastructure with no business logic changes.
On the Driven Side, the Domain Logic never talks to the database directly; it simply calls a Secondary Port (Interface). The actual database or email server is hidden inside a Secondary Adapter that implements the interface, allowing you to swap out your infrastructure with no business logic changes.
Plugging in a different DB type is a breeze!
Plugging in a different DB type is a breeze!
Archie (you!) remains safe in the Core from the blast if something happens to the API!
Archie (you!) remains safe in the Core from the blast if something happens to the API!
Cockburn wanted to make sure there was enough room implied thru the diagrams.
Cockburn wanted to make sure there was enough room implied thru the diagrams.
Look for the leaks to replace with Ports and Adapters!
Look for the leaks to replace with Ports and Adapters!

Recap

  • Hexagonal Architecture (also called Ports and Adapters) is a pattern designed to protect your most valuable asset, your Core Business Logic, from the messy, changing outside world.
  • It flips the mental model from a vertical "stack" to a concentric "Inside vs. Outside" model.
  • The Core (Inside) contains purely business logic and has zero dependencies on the outside world (no DB, no UI, no frameworks).
  • Ports are the "airlocks" or interfaces defined by the Core. They specify what the Core needs or can do, without explaining how.
  • Adapters are the translators that live on the Outside. They connect specific technologies (like a SQL database or a React app) to the Ports.
  • The Golden Rule: All dependencies point INWARD. The Outside knows about the Inside, but the Inside knows nothing about the Outside.
  • This architecture makes Testability easy. You can test your logic in isolation by plugging in "Fake" adapters, allowing for fast, reliable tests that run anywhere.
  • Alistair Cockburn created the pattern to solve the problem of entanglement, allowing an application to be driven equally by a user, a program, or an automated test script.

FAQ

What is Hexagonal Architecture in simple terms?It is an architectural pattern that protects the core business logic of an application from external details like databases, web frameworks, and user interfaces. The core communicates only through ports (interfaces), while adapters handle the translation to and from outside technologies.
Why is Hexagonal Architecture also called Ports and Adapters?Because the pattern is built around two main ideas: ports, which are interfaces that define how the core talks to the outside world, and adapters, which are the implementations that connect specific technologies to those ports.
What problem does Hexagonal Architecture solve?It solves the problem of tight coupling between business logic and infrastructure. By isolating the core, it prevents database schema changes, UI changes, or framework changes from forcing changes in the business rules.
What is the difference between the Inside and the Outside?The Inside is the protected core of the application, containing business rules and use-case logic. The Outside includes everything external, such as databases, APIs, web controllers, file systems, and user interfaces.
What are primary ports and secondary ports?Primary ports are the interfaces the outside world uses to drive the application, such as a service for placing an order. Secondary ports are the interfaces the application uses to request services from the outside world, such as saving data or sending alerts.
What is the role of an adapter in Hexagonal Architecture?An adapter translates between the outside world and the core. A primary adapter converts external requests into calls to the core, while a secondary adapter converts core requests into infrastructure-specific actions.
What is the Composition Root?The Composition Root is the place where the application is wired together, usually at the entry point such as Program.cs or Main. It creates the concrete adapters and injects them into the core, without the core knowing how those dependencies are created.
Why does Hexagonal Architecture improve testability?Because the core can be tested with fake or mock adapters instead of real infrastructure. This makes tests fast, deterministic, and independent of databases, networks, or external APIs.
Why is the hexagon shape used in the diagram?The hexagon is not meant to imply exactly six sides. Alistair Cockburn chose it because it suggests multiple connection points and avoids the top-bottom hierarchy of layered architecture.
What are the main tradeoffs of using Hexagonal Architecture?It adds more interfaces, mapping, and files, which creates a complexity tax and can feel like indirection or duplication. It is most worthwhile for systems with complex business logic, long lifespans, or multiple external entry points.

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
  • Grokking Software Architecture Grokking Software 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
  • Grokking Software Architecture Grokking Software Architecture ebook for free