Hexagonal Architecture

Hexagonal architecture protects the core domain from external systems by using ports and adapters.

Core Idea

Also known as Ports & Adapters, hexagonal architecture places the domain logic at the center of the application. External concerns — databases, APIs, user interfaces, message queues — connect to the domain through well-defined ports (interfaces) and adapters (implementations). The domain never depends on infrastructure; infrastructure depends on the domain.

HTTP Controller
CLI
Message Listener

Inbound Adapters

Domain Core
Database
External API
Email Service

Outbound Adapters

Ports

Ports are interfaces defined by the domain 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

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.

Hexagonal architecture tends to be more pragmatic in practice, with fewer prescribed layers and more emphasis on the port/adapter mechanism itself.

Related Styles