← Back to RawStack

Hexagonal Architecture

Hexagonal Architecture, also known as the Ports and Adapters pattern, is a software design approach that promotes modularity, maintainability, and testability. It structures an application around a core domain while keeping external dependencies—such as databases, APIs, and user interfaces—decoupled.

At its core, Hexagonal Architecture consists of three main layers:

  • Domain Layer (Business Logic) – The heart of the application, where business rules and domain models reside. This layer remains isolated from external technologies, ensuring that logic is independent and reusable.
  • Application Layer – Serves as an intermediary between the domain and external components. It orchestrates use cases, handling requests from various sources (e.g., APIs, UI, CLI).
  • Infrastructure Layer – Contains external dependencies like databases, messaging systems, and web frameworks. It communicates with the core application through adapters, ensuring that business logic remains untouched when swapping out dependencies.

How it Works

Hexagonal Architecture introduces ports and adapters to facilitate communication between these layers.

  • Ports define the interfaces through which the core application interacts with the outside world (e.g., service interfaces for data retrieval or messaging).
  • Adapters implement these interfaces, acting as bridges between the business logic and infrastructure (e.g., a repository adapter for database access or an API adapter for HTTP requests).

By enforcing strict separation between layers, Hexagonal Architecture allows for seamless technology swaps without affecting business logic. This makes it particularly valuable for long-term scalability, testing, and future-proofing applications.

Why It Works Well with Domain-Driven Design

Hexagonal Architecture aligns perfectly with Domain-Driven Design (DDD), as both emphasize domain-centric development. DDD encourages rich domain models and explicitly defined boundaries, which fit naturally within the layered structure of Hexagonal Architecture.

  • The Domain Layer in Hexagonal Architecture corresponds directly to the Core Domain in DDD, where business rules are encapsulated.
  • The Application Layer acts as the Application Services in DDD, orchestrating use cases while maintaining separation from domain logic.
  • The Infrastructure Layer aligns with technical concerns in DDD, handling persistence, messaging, and external API interactions.

By combining Hexagonal Architecture with DDD, applications gain clean separation of concerns, better testability, and the flexibility to evolve over time without being tightly coupled to external technologies. This combination is particularly beneficial for complex, large-scale systems where business logic must remain adaptable as requirements change.

Where is it used in the stack?

The RawStack API isn't strictly bound to Hexagonal architectural principles, but it draws heavily from the core concepts of both Hexagonal Architecture and Domain-Driven Design, ensuring a clean, scalable, and maintainable structure.

Start building now!

Give you or your team a production-ready foundation for web and mobile apps, with performance, scalability, and maintainability built in.

npx create-rawstack-app@latest

copyright © rawstack.io