Hexagonal Architecture
Hexagonal architecture protects the application core from the outside world by connecting everything through ports and adapters.
Core Idea
Described by Alistair Cockburn in 2005 and also known as Ports & Adapters, hexagonal architecture puts the application at the center: its domain logic and the use cases built on it. Everything else (user interfaces, APIs, databases, message queues, other systems) connects through well-defined ports (interfaces owned by the core) and adapters (technology-specific implementations). The core never depends on infrastructure; adapters depend on the core's ports.
Cockburn's original goal was to let an application be driven equally well by users, other programs, automated tests or batch scripts, and to develop and test it in isolation from its eventual databases and devices. The hexagon itself has no special meaning: the shape simply leaves room to draw several ports, instead of the top-to-bottom picture of layers.
Ports
Ports are interfaces owned by the application core (the domain model plus its use cases) that describe what capabilities it needs (outbound ports) or what operations it exposes (inbound ports). They are technology-agnostic and expressed in domain language.
- Inbound ports (Cockburn: driving or primary): define the use cases the application offers (e.g.,
PlaceOrder,RegisterUser) - Outbound ports (driven or secondary): define what the application needs from the outside world (e.g.,
OrderRepository,PaymentGateway)
Adapters
- Inbound (driving) adapters: Translate external requests into calls on inbound ports. Examples: HTTP controllers, GraphQL resolvers, message consumers, CLI commands.
- Outbound (driven) adapters: Implement outbound ports using specific technologies. Examples: SQL repositories, REST clients, SMTP email senders, file system storage.
When to Use
- The domain logic is complex enough to deserve protection from technical details
- The application has several ways in (UI, API, messages, batch jobs) or many integrations out
- The system will live long enough for its databases, frameworks or providers to change
- You want fast tests for business logic without starting databases or servers
When to Avoid
- Simple CRUD applications where most code just moves data in and out
- Short-lived prototypes and throwaway tools
- Teams that don't share the intent: without agreement, the boundaries erode quickly and leave only the indirection
Benefits
- Isolation: The domain logic and use cases are free from infrastructure concerns and can evolve independently
- Testability: Use cases can be tested without any infrastructure: tests drive the inbound ports, and in-memory adapters stand in for databases and external services. Pure domain logic needs no adapters at all
- Replaceable infrastructure: A database or provider can be swapped by writing a new adapter. In practice this is rarely the main reason to choose the pattern; isolation and testability pay off far more often
- Explicit boundaries: The ports make dependencies visible and intentional
- Multiple entry points: The same domain logic can be accessed via HTTP, CLI, events, or tests
Trade-offs
- Indirection: More interfaces and wiring than a direct approach
- Over-engineering risk: For simple CRUD applications, the extra structure may not pay for itself
- Learning curve: Teams unfamiliar with the pattern may struggle with where to place code
- Mapping overhead: Where adapter models and domain models differ, data has to be translated at the boundary. That's deliberate, but it is code to write and maintain
Common Mistakes
- Domain depending on infrastructure: The most critical violation - if the domain imports from an adapter package, the architecture is broken
- Table-shaped ports: Ports that mirror database tables (
saveRow,findByColumn) instead of expressing what the application needs - Adapter leakage: Letting adapter-specific types (ORM entities, HTTP request objects) flow into the domain
- Port granularity by accident: One port per method everywhere, or one giant port for everything. One inbound port per use case is a sensible default; group outbound operations by what the application needs
Relation to Clean Architecture
Clean Architecture (Robert C. Martin) shares the same core principle: dependencies point inward toward the domain. Clean Architecture adds more explicit layers (entities, use cases, interface adapters, frameworks) but the fundamental insight is identical - protect the domain from external change.
- Entities = DomainBusiness rules and the domain model. Depends on nothing.
- Use Cases = ApplicationOrchestrates the domain for one use case and defines the ports it needs.
- Interface Adapters = Presentation & Infrastructure adaptersControllers, presenters and repository implementations: the adapters of hexagonal architecture.
- Frameworks & Drivers = InfrastructureDatabase, web server, message broker, UI framework: the outside world the adapters talk to.
The difference is one of emphasis, not of pragmatism. Hexagonal architecture says nothing about how to structure the inside of the hexagon; it is about the boundary, the ports and adapters. Clean Architecture describes the boundary too, and also names the rings inside it. Either can be applied lightly or rigidly.
Historically, Clean Architecture (2012) was Martin's attempt to unify several very similar ideas, among them Hexagonal Architecture (Cockburn, 2005) and Onion Architecture (Jeffrey Palermo, 2008). That's why codebases following any of them tend to end up with the same split into Domain, Application and Infrastructure.
In Practice: Roarline's Payments
In Roarline, the fictional ticketing platform used across this site, the Payments module is a textbook case. "Charge for an order" is an inbound port. The payment provider sits behind an outbound port, PaymentGateway, and an adapter translates between Roarline's language and the provider's API. Adding a second provider, one of Roarline's quality scenarios, means writing one new adapter and touching nothing else. In domain-driven design terms, that adapter also acts as an anti-corruption layer: the provider's vocabulary never leaks into Roarline's model.