What it is
Hexagonal architecture, introduced by Alistair Cockburn in 2005 and also called ports and adapters, places the application (its use cases and business rules) in the middle and lets it interact with the outside world only through ports: interfaces defined by the application, in its own vocabulary.
- Driving (primary) ports describe what the application offers:
IPlaceOrder. - Driven (secondary) ports describe what the application needs:
IOrderStore,IPayments.
Adapters connect a port to a technology. Driving adapters (an HTTP endpoint, a queue consumer, a CLI, a test) call driving ports. Driven adapters (an EF Core store, a payment SDK wrapper, an SMTP sender) implement driven ports. The hexagon shape has no meaning beyond leaving room to draw several ports.
Why it exists
Cockburn's goal was to let an application be driven equally by users, programs, automated tests or batch scripts, and to develop and test it in isolation from its eventual devices and databases. Business logic tangled with UI and database code cannot be tested without them, and every new channel tends to copy logic. Separating the core behind ports means one core can serve many channels and many technologies.
How it works
Ports in the core.
// Ordering.Core
public interface IPlaceOrder { Task<OrderId> Handle(PlaceOrder command, CancellationToken ct); }
public interface IOrderStore { Task Save(Order order, CancellationToken ct); }
public interface IPayments { Task<ChargeResult> Charge(OrderId id, Money amount, CancellationToken ct); }
Adapters outside. An ASP.NET Core endpoint maps JSON to a PlaceOrder command and calls IPlaceOrder. A hosted service reading a queue does the same for marketplace orders. SqlOrderStore : IOrderStore uses EF Core; CardPayments : IPayments wraps the provider's SDK.
Configuration. The host chooses the adapters, typically with dependency injection: production registers SqlOrderStore; a test registers InMemoryOrderStore. The core's code is identical in both.
Dependencies versus calls. At runtime the core calls the payment adapter. In source code the adapter references the core's port. Every arrow points at the hexagon, which is the same dependency inversion clean architecture builds on.
Test doubles. Because driven ports are small, application-shaped interfaces, fakes are easy to write: a list behind IOrderStore, a stub that approves charges behind IPayments. Tests then drive the core through its driving port with no network or database.
When to use it
- Applications with several entry points (API, messages, scheduled jobs) that must share one set of rules.
- Systems whose infrastructure is likely to change or has variants per environment or market.
- When fast, reliable tests of business rules matter.
- As the structure inside a service or module that has real domain logic.
When not to
- Thin CRUD services where the "core" would just forward to the database.
- Throwaway prototypes.
- When ports would mirror one technology's API exactly; then they add a layer without decoupling anything.
Trade-offs
| Choice | You gain | You pay |
|---|---|---|
| Ports in the application's language | Replaceable adapters, technology-free core | Designing the abstraction; mapping at each adapter |
| Test doubles behind driven ports | Fast, deterministic tests | Doubles can drift from real behaviour; adapters need their own tests |
| Many fine-grained ports | Precise seams | More interfaces and wiring to navigate |
| Few coarse ports | Less ceremony | Bigger adapters, broader fakes |
Alternatives
- Clean / onion architecture: the same inversion, with prescribed inner rings (entities, use cases).
- Layered architecture: simpler, but the domain depends on data access.
- Vertical slices: organise by feature; slices may still use ports where they need to isolate infrastructure.
- Functional core, imperative shell: pure business functions in the middle, all I/O in a thin outer shell; similar goals with less interface ceremony.
Failure modes
- Leaky ports:
IQueryable, EF entities or HTTP status codes appear in port signatures, so the core depends on a technology after all. - Fakes that lie: an in-memory store that ignores uniqueness constraints or transactions lets bugs pass that the real database would reject.
- Port per class: every helper gets an interface, so the seams no longer mark real boundaries.
- Logic in adapters: business decisions drift into controllers or repositories, and different channels behave differently.
In production
Keep driving adapters thin: parse, authorise, map, call the port, map the result. Put transactions and orchestration in the core's use cases. Test the core with doubles, and test each driven adapter with integration tests against the real technology (Testcontainers for databases and brokers, the provider's sandbox for payments). Contract tests between the double and the real adapter help keep fakes honest.
Common misconceptions
- "Hexagonal means six ports." The hexagon is just a drawing with room for several ports.
- "Ports are repositories." Driven ports include any outside need: payments, clocks, messaging, notifications. Driving ports describe the application's use cases.
- "Test doubles replace integration tests." They test the rules. Only the real adapter against real infrastructure proves the mapping and the SQL.
- "The core calls nothing outside." It calls through ports at runtime; it just does not reference the implementations.
Interview angle
Draw the core with a driving port on one side and driven ports on the other, put adapters around it and point every dependency at the core. Explain how this enables multiple channels and fast tests, and contrast it with layered architecture's downward dependency on data. Strong answers mention test doubles and their limits, port design in the domain's language, and the relationship to clean and onion architecture.
Model assumptions
The walk-throughs, the second driver and the dependency view are scripted illustrations with hand-placed packets, slowed to 30–40 ms per hop. The live steps are simulated: adapters, ports and the core are servers joined by 0 ms links (in-process calls); ports take essentially no time and are drawn as nodes only to make the path visible; the core takes about 1 ms. The shopper is 20 ms away, the payment provider 30 ms (20 ms of work), SQL Server 1 ms (5 ms per query, 8 connections). The core saves an order before charging it. Test doubles are in-memory components taking about 0.05 ms. Shopper arrivals are random (Poisson); tests send calls at a constant rate. Numbers show behaviour and orders of magnitude, not benchmarks.