What Is Hexagonal Architecture?

Ports and Adapters

Published on 27 Nov 2025
system design design patterns interview

Hexagonal Architecture — also known as the Ports and Adapters pattern — is a software design approach created by Alistair Cockburn. Its goal is to decouple the core business logic from external systems such as databases, APIs, frameworks, and user interfaces.

By isolating the application core behind “ports” and interacting with the outside world through “adapters,” systems become more maintainable, testable, and flexible to change.


Key Concepts

Hexagonal Architecture is built around a few foundational ideas.

Domain (Application Core)

  • Contains business logic and rules

  • Independent of frameworks, databases, and infrastructure

  • Represents the heart of the system

Ports

  • Abstract boundaries defining what the application needs or exposes

  • Describe interactions such as persistence, messaging, external services, and user interfaces

  • Typically implemented as interfaces or contracts

Adapters

  • Concrete implementations that connect ports to real systems, such as:

    • Database repositories

    • REST controllers

    • Message consumers

    • CLI or UI layers

  • Can be swapped or replaced without modifying business logic

Inside vs Outside the Hexagon

  • Inside → Core domain logic

  • Outside → Infrastructure and external concerns

This separation ensures technology becomes optional rather than structural.


How Hexagonal Architecture Works

Here’s how the pattern operates in practice:

  1. The application core exposes ports.
    These ports define how the outside world can interact with the system.

  2. External systems plug in via adapters.
    Each adapter implements a port contract. Examples include:

    • A REST API adapter that handles HTTP requests

    • A database adapter that implements persistence logic

    • A queue adapter that listens to messages

  3. All communication flows through ports.
    No external code directly touches business logic.

  4. Adapters can be swapped with minimal effort.
    For example:

    • Replacing SQL with NoSQL

    • Switching message broker tools

    • Adding a new UI without rewriting core logic

The architecture promotes inversion of dependencies: the domain does not depend on infrastructure — instead, infrastructure depends on the domain.


Advantages of Hexagonal Architecture

  • High testability — business logic can be tested without databases or frameworks

  • Strong separation of concerns — core logic stays clean and independent

  • Technology flexibility — tools and integrations can be replaced more easily

  • Supports long-term maintainability and refactoring

  • Well-suited to microservices, event-driven systems, and modern architectural styles


Disadvantages of Hexagonal Architecture

  • Adds complexity for small or simple projects

  • Requires design discipline and understanding of boundaries

  • Can introduce extra boilerplate such as interfaces and adapters

  • Not a solution to poor design by itself — it is only a structural pattern

Hexagonal Architecture is most valuable in systems that are expected to evolve or grow over time.


Summary

Hexagonal Architecture (Ports and Adapters) is a design pattern that separates business logic from external systems using well-defined ports and pluggable adapters. It leads to cleaner, modular, and more testable applications while providing flexibility and technology independence.

However, it also introduces extra structure and complexity, which means it delivers the most benefit in medium-to-large, long-lived systems rather than small or short-lived applications.