TECH_COMPARISON
Hexagonal vs Layered Architecture: A Detailed Comparison for System Design
Compare hexagonal and layered architecture patterns — understand trade-offs in testability, coupling, flexibility, and domain isolation.
Hexagonal vs Layered Architecture
Hexagonal architecture (also called Ports and Adapters) and layered architecture represent different philosophies for structuring application code. The choice affects testability, flexibility, and how tightly your domain logic is coupled to infrastructure.
Layered Architecture
Layered architecture organizes code into horizontal tiers: presentation, business logic, and data access. Each layer depends only on the layer below it. This is intuitive and is the default pattern in most web frameworks.
The problem: over time, the data layer becomes the foundation everything depends on. Changing your database, ORM, or persistence strategy requires modifications that ripple upward through business logic and presentation.
Hexagonal Architecture
Hexagonal architecture inverts this relationship. The domain core sits at the center with no dependencies on anything external. It exposes ports (interfaces) that define how the outside world interacts with it. Adapters implement these ports to connect to databases, APIs, message queues, or UIs.
This means you can swap PostgreSQL for MongoDB, or REST for gRPC, by writing a new adapter — the domain logic never changes.
Testability
Hexagonal architecture's biggest practical benefit is testability. Domain logic can be unit-tested with simple in-memory adapters. No database, no HTTP server, no framework — just pure business logic. See our testing strategies concept for patterns.
When Layered Is Enough
For a standard CRUD application with simple business rules, hexagonal architecture is over-engineering. The ports and adapters add structural overhead without proportional benefit. Layered architecture is perfectly fine for most web apps.
For deeper study, explore our system design interview guide and architecture interview questions. See pricing for full access.
The Bottom Line
Use layered architecture as your default for straightforward applications. Adopt hexagonal architecture when domain complexity justifies the investment in clean boundaries and infrastructure independence.
GO DEEPER
Master this topic in our 12-week cohort
Our Advanced System Design cohort covers this and 11 other deep-dive topics with live sessions, assignments, and expert feedback.