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