What Is Hexagonal Architecture?
Hexagonal Architecture (Ports & Adapters)
Hexagonal Architecture (also called Ports & Adapters) isolates business logic from external concerns. The core application defines 'ports' (interfaces) for incoming and outgoing interactions. 'Adapters' implement these ports for specific technologies (REST, databases, message queues).
How Hexagonal Architecture Works
Your OrderService defines a port (interface): OrderRepository with save() and findById(). One adapter implements it with PostgreSQL, another with MongoDB, another with in-memory storage for tests. The core business logic never imports pg or mongoose — it only knows the port interface.
Key Concepts
- Ports — Interfaces defined by the application core — incoming (driving) ports for use cases, outgoing (driven) ports for infrastructure
- Adapters — Implementations that connect ports to specific technologies — REST adapter, PostgreSQL adapter, test adapter
Frequently Asked Questions
Hexagonal vs Clean Architecture?
Same principle (dependency inversion, isolated core), different terminology. Hexagonal uses ports/adapters; Clean Architecture uses layers/boundaries. Choose the one your team understands better.