Layered vs Hexagonal vs Clean Architecture: What's the Difference?

Compare layered (n-tier), hexagonal (ports and adapters) and clean architecture: dependency direction, testability, folder structures and when each fits.

Published November 27, 2026

Layered, hexagonal and clean architecture all answer the same question: how should code inside one application be organised so that business logic stays understandable and testable as the system grows? They differ mainly in which way dependencies point. Understanding that one idea makes all three, and their many folder-structure variations, easy to compare.

The concept

Classic layered (n-tier) architecture stacks presentation, business logic and data access, with each layer calling the one below. It is familiar and works well for straightforward CRUD applications. Its weakness is that business logic ends up depending on the data layer: ORM entities, SQL and framework details leak upward, so testing logic often requires a database.

Hexagonal architecture (ports and adapters, Alistair Cockburn) flips that dependency. The application core defines ports, interfaces describing what it needs (save an order, send an email) and what it offers. Adapters implement those ports for specific technologies: a PostgreSQL repository, an SMTP mailer, a REST controller. Clean architecture (Robert C. Martin) and onion architecture express the same idea as concentric circles with a strict rule: source code dependencies point only inward, toward entities and use cases.

Layered architecture

Presentationcontrollers, viewsBusiness logicservicesData accessrepositories, ORMDatabase
Layered: dependencies point down, so logic depends on data access.

Hexagonal (ports and adapters)

Core: use cases + domaindefines portsPortsinterfacesHTTP adapterdriving: REST controllerCLI / queue adapterdrivingPostgres adapterdriven: implements OrderRepositorySMTP adapterdriven: implements Mailer
Hexagonal: adapters depend on the core, never the other way round.

Comparison

LayeredHexagonalClean / onion
Dependency directionTop → bottom (toward DB)Adapters → coreOuter circles → inner
Business logic depends on DB?Usually yesNo (via ports)No
Unit testing logicNeeds mocks or DBEasy with fake adaptersEasy
CeremonyLowMediumMedium-high
Best forCRUD, simple domainsApps with many integrationsComplex domains, long-lived systems

Choosing between them

If the application mostly moves data between forms and tables, a layered structure inside a good framework is perfectly adequate and the extra indirection of ports adds little. When business rules are complex, change often, or must be tested thoroughly, and when the app talks to many external systems that might be swapped, hexagonal or clean architecture pays for its extra interfaces. Many teams apply it selectively: a clean core for the complicated domain modules, simple layering for admin CRUD.

Practical examples

A port and two adapters (TypeScript)

The use case depends on the interface; production and tests plug in different implementations.

// core/ports.ts
export interface OrderRepository { save(o: Order): Promise<void>; byId(id: string): Promise<Order | null> }
export interface PaymentGateway { charge(customerId: string, amount: number): Promise<string> }

// core/placeOrder.ts
export async function placeOrder(repo: OrderRepository, pay: PaymentGateway, cmd: PlaceOrderCmd) {
  const order = Order.create(cmd)               // pure domain logic
  order.markPaid(await pay.charge(cmd.customerId, order.total()))
  await repo.save(order)
  return order.id
}

// adapters/postgresOrderRepository.ts implements OrderRepository using SQL
// tests/fakes.ts: InMemoryOrderRepository, FakePaymentGateway

A typical clean architecture folder layout

Names vary; the dependency rule is what matters.

src/
  domain/          entities, value objects, domain services (no framework imports)
  application/     use cases, ports (interfaces), DTOs
  infrastructure/  db repositories, http clients, mailers (implement ports)
  interfaces/      controllers, CLI commands, queue consumers

Enforce the dependency rule automatically

Fail the build if the domain imports infrastructure.

// eslint-plugin-boundaries or dependency-cruiser
{ from: { path: '^src/domain' }, to: { path: '^src/(infrastructure|interfaces)' }, severity: 'error' }

Common mistakes

  • Creating interfaces for everything in a simple CRUD app, adding indirection without benefit.
  • Letting ORM entities become domain entities, coupling business rules to persistence.
  • Treating folder names as architecture while dependencies still point the wrong way.
  • Putting business rules in controllers or database triggers.
  • Mixing styles randomly across a codebase without a stated rule.

Key takeaways

  • All three organise code inside one application; they differ in dependency direction.
  • Layered points toward the database; hexagonal and clean point toward the domain.
  • Ports and adapters make business logic testable without infrastructure.
  • Use clean/hexagonal where the domain is complex; simple layering for CRUD.

FAQ

Are hexagonal and clean architecture the same thing?

Very close. Both enforce inward dependencies with interfaces at the boundary. Clean architecture adds named rings (entities, use cases, interface adapters, frameworks).

Do these replace microservices or monoliths?

No. They describe the inside of an application. A modular monolith or each microservice can use any of them internally.

Does Laravel or Rails work with hexagonal architecture?

Yes, though their conventions encourage layered/active-record styles. You can keep a framework-free core and let controllers and Eloquent models act as adapters.

More Architecture articles