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.

Hexagonal architecture with ports and adapters The application core, with the domain model inside and use cases around it, sits in a hexagon. Ports sit on its edges. On the driving side, a REST controller calls the PlaceOrder port and a message consumer calls the CancelOrder port. On the driven side, the OrderRepository port is implemented by a SQL repository talking to PostgreSQL, and the PaymentGateway port by a payment client talking to the payment provider. Driving side (primary) Driven side (secondary) Application core use cases Domain model use cases PlaceOrder CancelOrder OrderRepository PaymentGateway REST controller Message consumer called by browsers, apps fed by a message broker SQL repository Payment client talks to PostgreSQL talks to the payment provider Inbound (driving) adapters Ports sit on the edge Outbound (driven) adapters
Arrows show calls at runtime. Source-code dependencies all point inward: adapters depend on the ports, never the other way round.

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.

Adapters

When to Use

When to Avoid

Benefits

Trade-offs

Common Mistakes

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.

Clean Architecture rings Four concentric rings. From the outside in: frameworks and drivers, interface adapters, use cases, entities. An arrow shows that source-code dependencies point inward. Frameworks & Drivers web framework, database, broker, UI Interface Adapters controllers, presenters, repositories Use Cases application services, ports Entities domain model dependencies point inward
  • 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.
Clean Architecture's rings, mapped to the layer names most codebases use. In hexagonal terms, the two inner rings are the inside of the hexagon and the ports sit at the edge of the use cases.

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.

Related Styles