Layered versus Hexagonal Architecture

How classic layered architecture and Hexagonal Architecture (Ports and Adapters) differ in dependency direction, testability and cost — what ports and adapters are, why each port exists, and when a simple layered design is enough.

On this page

Simple definition#

  • Layered architecture stacks the code in horizontal layers — typically presentation, business and data access. Each layer calls the layer below it, so dependencies point down, towards the database.
  • Hexagonal Architecture, also called Ports and Adapters (Alistair Cockburn, 2005), puts the business logic at the centre. The core defines ports — interfaces in its own terms — and technology-specific adapters plug into them from outside. All dependencies point inwards, towards the business logic.

The difference is one word: direction. That single change has large consequences for testing and for changing technology.

The problem Hexagonal Architecture addresses#

In a classic layered Spring application, the business layer depends on the data-access layer:

@Service
public class OrderService {
    private final OrderJpaRepository repository;   // a Spring Data interface
    public Order place(...) {
        OrderEntity entity = new OrderEntity();      // a JPA entity is the business object
        ...
    }
}

Over time this produces familiar symptoms:

  • Business rules depend on infrastructure. The Order is a JPA entity, so it must have a no-argument constructor and mutable fields, and lazy-loading rules leak into business code.
  • Tests need the infrastructure. Testing a pricing rule requires a database, or extensive mocking of repository internals.
  • Business logic leaks outwards. With no clear boundary, rules spread into controllers (“if the status is X, return 409”) and into SQL queries.
  • Technology changes ripple. Replacing the payment provider’s SDK or moving from REST to messaging touches the business layer.

Cockburn’s stated goal was to let an application be “equally driven by users, programs, automated tests or batch scripts, and to be developed and tested in isolation from its eventual run-time devices and databases”.

Core concept#

Layered architecture#

Classic layered architecture — dependencies point down
flowchart TB
    P["Presentation layer<br/>controllers, views"]
    B["Business layer<br/>services, entities"]
    D["Data access layer<br/>repositories, ORM"]
    DB[("Database")]
    P --> B --> D --> DB

The rule is simple: a layer may use only the layer below it (strict layering), or any layer below it (relaxed layering). The structure is easy to understand and suits many applications. Its weakness is that the most important code — the business rules — depends on the most volatile code: frameworks and storage.

Hexagonal architecture#

Hexagonal architecture — dependencies point inwards
flowchart LR
    subgraph Driving["Driving side (who calls us)"]
        REST["REST controller"]
        CLI["CLI / batch job"]
        TEST["Test"]
    end
    subgraph Core["Application core"]
        IN["Inbound ports<br/>PlaceOrderUseCase<br/>GetOrderQuery"]
        APP["Application service<br/>OrderApplicationService"]
        DOM["Domain model<br/>Order, Money, OrderLine"]
        OUT["Outbound ports<br/>OrderRepository<br/>PaymentPort"]
        IN --- APP
        APP --> DOM
        APP --> OUT
    end
    subgraph Driven["Driven side (whom we call)"]
        JPA["JPA adapter"]
        PAY["Payment adapter"]
        FAKE["In-memory fakes"]
    end
    REST --> IN
    CLI --> IN
    TEST --> IN
    JPA -. implements .-> OUT
    PAY -. implements .-> OUT
    FAKE -. implements .-> OUT

The hexagon shape has no special meaning. Cockburn chose it to leave room for several ports, and to break the habit of drawing layers stacked from top to bottom.

The vocabulary#

TermMeaningIn the example
Application coreDomain model plus application services. No framework dependencies.domain, application packages
Inbound (driving, primary) portInterface through which the outside world asks the application to do somethingPlaceOrderUseCase, GetOrderQuery
Inbound adapterTranslates an external trigger (HTTP, a message, a CLI) into calls on an inbound portOrderController
Outbound (driven, secondary) portInterface through which the application asks for something it needs from outsideOrderRepository, PaymentPort
Outbound adapterImplements an outbound port with a concrete technologyJpaOrderRepositoryAdapter, SimulatedPaymentAdapter

Dependency direction#

Compare where the arrows point:

DependencyLayeredHexagonal
Controller → business logicYesYes (through an inbound port)
Business logic → persistenceYes — the service calls the repository implementationNo — the service calls a port; the persistence adapter implements it
Business logic → external APIsYesNo — through an outbound port
Business objects → framework annotationsOften (@Entity)No — mapping happens in adapters

Why each port exists#

Hexagonal Architecture does not mean “an interface for every class”. Each port should have a reason:

  • PlaceOrderUseCase — an inbound port that lets REST, messaging and tests drive the same use case. It also documents the application’s capabilities in one place.
  • GetOrderQuery — kept separate from the command port (Interface Segregation), so read-only clients do not depend on write operations.
  • OrderRepository — the core needs storage but must not know about JPA. In tests, a map implements it.
  • PaymentPort — wraps an external provider whose API, failures and credentials should never reach business code. Its contract states that the order ID is an idempotency key.

There is deliberately no port for the domain model itself and no interface for OrderApplicationService. Neither has a second implementation or a boundary to protect.

Practical example#

The same use case — place an order and charge for it — in each style.

Layered:

@Service
public class OrderService {
    private final OrderJpaRepository repository;      // infrastructure type
    private final StripeClient stripe;                 // vendor SDK

    @Transactional
    public OrderEntity place(OrderRequest request) {   // web DTO in the business layer
        OrderEntity order = new OrderEntity(request);
        repository.save(order);
        Charge charge = stripe.charges().create(Map.of("amount", order.totalCents()));
        order.setStatus(charge.getPaid() ? "PAID" : "DECLINED");
        return order;
    }
}

Hexagonal (from the runnable example):

OrderApplicationService.java View source
package com.example.orders.application.service;

import com.example.orders.application.port.in.GetOrderQuery;
import com.example.orders.application.port.in.PlaceOrderUseCase;
import com.example.orders.application.port.out.OrderRepository;
import com.example.orders.application.port.out.PaymentPort;
import com.example.orders.application.port.out.PaymentPort.PaymentOutcome;
import com.example.orders.domain.CustomerId;
import com.example.orders.domain.Money;
import com.example.orders.domain.Order;
import com.example.orders.domain.OrderId;
import com.example.orders.domain.OrderLine;

import java.time.Clock;
import java.util.Currency;
import java.util.List;
import java.util.Objects;
import java.util.function.Supplier;

/**
 * Application service: orchestrates a use case. It translates the command
 * into domain objects, lets the aggregate enforce business rules, and talks
 * to the outside world only through outbound ports. Plain Java — Spring
 * wiring happens in the config package.
 */
public final class OrderApplicationService implements PlaceOrderUseCase, GetOrderQuery {

    private final OrderRepository orders;
    private final PaymentPort payments;
    private final Clock clock;
    private final Supplier<OrderId> ids;

    public OrderApplicationService(OrderRepository orders, PaymentPort payments, Clock clock, Supplier<OrderId> ids) {
        this.orders = Objects.requireNonNull(orders);
        this.payments = Objects.requireNonNull(payments);
        this.clock = Objects.requireNonNull(clock);
        this.ids = Objects.requireNonNull(ids);
    }

    @Override
    public Order placeOrder(PlaceOrderCommand command) {
        Currency currency = Currency.getInstance(command.currency());
        List<OrderLine> lines = command.lines().stream()
                .map(l -> new OrderLine(l.productId(), l.quantity(), new Money(l.unitPrice(), currency)))
                .toList();

        Order order = Order.place(ids.get(), new CustomerId(command.customerId()), lines, clock.instant());
        orders.save(order);                         // 1. record the intent first

        switch (payments.charge(order.id(), order.total())) {   // 2. call the outside world
            case PaymentOutcome.Approved approved -> order.markPaid(approved.reference());
            case PaymentOutcome.Declined declined -> order.markPaymentDeclined();
        }
        orders.save(order);                         // 3. record the outcome
        return order;
    }

    @Override
    public Order getOrder(OrderId id) {
        return orders.findById(id).orElseThrow(() -> new OrderNotFoundException(id));
    }
}

The hexagonal version mentions no web, JPA or vendor types. Every collaborator is a port or a JDK abstraction, so the use case can be tested in milliseconds with hand-written fakes.

Implementation walkthrough#

  1. Start from the use cases. List what the application must do (“place order”, “get order”). These become inbound ports.
  2. Model the domain without frameworks. Order protects its invariants in plain Java.
  3. Name what the core needs from outside. Storage and payments become outbound ports, phrased in domain terms.
  4. Write the application service. It orchestrates: translate input, call the domain, call outbound ports.
  5. Write adapters last. Each adapter translates between a technology and a port. The REST controller maps JSON to commands; the JPA adapter maps aggregates to tables.
  6. Wire everything in one place. A configuration class is the composition root (see Dependency Injection).
  7. Enforce the direction. Tools such as ArchUnit can fail the build if domain or application imports org.springframework or an adapter package.

Real-world applications#

  • Systems with significant business rules that will outlive their frameworks: banking, insurance, logistics, ordering.
  • Applications driven by several channels — REST, messaging and batch jobs — that must share use cases.
  • Systems whose external providers are likely to change: payment, notification and shipping integrations.
  • Codebases where fast, reliable tests of business rules are a priority.

Advantages and disadvantages#

LayeredHexagonal
Learning curveLow — familiar to most developersModerate — new vocabulary, more types
Amount of codeLessMore: ports, separate persistence models, mapping
Testing business rulesNeeds a database or deep mocksPlain unit tests with fakes
Replacing infrastructureTouches business codeWrite a new adapter
Risk of overengineeringLowReal: ports and mappers with no purpose
Framework features (lazy loading, @Transactional on entities)Directly availableKept at the edges; some convenience is lost

When a simpler layered architecture is sufficient#

Hexagonal Architecture is not automatically better. Prefer a simple layered design when:

  • The application is mostly CRUD: data in, data out, with little business logic to protect.
  • It is short-lived — a prototype, internal tool or migration utility.
  • There is one delivery mechanism and one database, and no realistic prospect of change.
  • The team is small and new to the vocabulary, and the cost of ceremony exceeds its benefit.

Comparison with Clean and Onion Architecture#

Hexagonal (Cockburn, 2005)Onion (Jeffrey Palermo, 2008)Clean (Robert C. Martin, 2012)
Core ideaPorts and adapters around an applicationConcentric rings: domain model, domain services, application services, infrastructureConcentric circles: entities, use cases, interface adapters, frameworks
Dependency ruleInwardsInwardsInwards (“the Dependency Rule”)
EmphasisSymmetry between driving and driven sides; testabilityLayers within the coreUse-case classes, input and output boundaries, presenters
PrescriptivenessLow — only ports and adaptersMediumHigher — named layers and boundary objects

All three share the same fundamental rule: source-code dependencies point towards the business logic. The differences lie mostly in how they divide the core and in naming. In practice, a well-built hexagonal application and a well-built clean-architecture application look very similar.

Common mistakes#

  • Interfaces for everything. A port without a reason — no boundary, no second implementation, no test seam — is ceremony.
  • Framework annotations in the core. @Entity, @JsonProperty or @Transactional on domain classes reintroduce the coupling you removed.
  • Business logic in adapters. A controller that checks order status before calling the use case splits the rules across layers.
  • Anaemic core. If the “domain” consists of getters and setters and all logic sits in services, the hexagon protects very little. See DDD Fundamentals.
  • Sharing one model across all layers. Serialising domain objects directly to JSON couples your API contract to your domain model. Use request and response DTOs in the web adapter.

Interview questions and answers#

What is Hexagonal Architecture? An architecture style in which the business core defines ports — interfaces in its own terms — and technology-specific adapters implement or call them. All dependencies point inwards, so the core can be developed and tested without frameworks, databases or networks.

What is the difference between an inbound and an outbound port? An inbound port is how the outside world drives the application, for example a PlaceOrderUseCase called by a REST controller. An outbound port is what the application needs from the outside world, for example an OrderRepository implemented by a JPA adapter.

How is Hexagonal different from a layered architecture? In a layered architecture, business code depends on the data-access layer. In Hexagonal, the data-access code depends on interfaces owned by the business core. The dependency direction is inverted.

How does Hexagonal Architecture relate to the Adapter pattern? Each outbound adapter is a GoF Adapter: it implements the port the core expects and translates calls to a technology’s API.

When would you not use Hexagonal Architecture? For CRUD applications with little business logic, short-lived tools, or teams for whom the extra types and mapping would cost more than the isolation is worth.

Is Clean Architecture the same as Hexagonal? They share the dependency rule. Clean Architecture is more prescriptive about the rings inside the core and about use-case boundary objects. Hexagonal only distinguishes the inside from the outside.

Summary#

  • Layered architecture points dependencies at the database; Hexagonal points them at the business logic.
  • The core owns its ports; adapters translate between ports and technologies.
  • The payoff is fast, isolated tests and replaceable infrastructure. The cost is more types and mapping code.
  • Hexagonal, Onion and Clean Architecture share the inward dependency rule.
  • Choose a simpler layered design when there is little business logic to protect.

References#

  • Alistair Cockburn. “Hexagonal Architecture”, 2005.
  • Jeffrey Palermo. “The Onion Architecture: part 1”, 2008.
  • Robert C. Martin. “The Clean Architecture”, 2012; and Clean Architecture, Prentice Hall, 2017.
  • Tom Hombergs. Get Your Hands Dirty on Clean Architecture, 2nd edition. Packt, 2023.
  • Mark Richards and Neal Ford. Fundamentals of Software Architecture. O’Reilly. Chapter on the layered architecture style.