Summary of Hexagonal Architecture: What You Need To Know - Simple Explanation
Summary of "Hexagonal Architecture: What You Need To Know - Simple Explanation"
This video provides a clear and practical introduction to Hexagonal Architecture, explaining what it is, how it works, its benefits, drawbacks, and when to use it in software development.
Main Ideas and Concepts
- Introduction to Hexagonal Architecture:
- A software architecture pattern focused on decoupling the core application logic from external systems.
- Also known as the Ports and Adapters pattern, coined by Alistair Cockburn.
- It improves upon traditional Three-Tier Architecture (presentation, logic, data layers) by reducing tight coupling between layers.
- Three-Tier Architecture Recap:
- Presentation Layer: User interaction (frontend, API).
- Logic Layer: Business logic.
- Data Layer: Data persistence.
- Problem: Layers can become tightly coupled, making maintenance and testing harder.
- Hexagonal Architecture Structure:
- Visualize the application as a hexagon with the core logic in the center.
- Each side of the hexagon represents an input or output port.
- Ports: Abstractions or interfaces defining how the application interacts with the outside world (e.g., reading/writing data).
- Adapters: Implementations that convert between the core application’s ports and external systems (databases, file systems, APIs, message queues).
- The core logic only interacts with ports, never directly with external systems.
- Changes in adapters do not affect the core application.
- Input and Output Sides:
- Inputs (e.g., API calls, message queues) drive the application.
- Outputs (e.g., database writes, external API calls) are driven by the application.
- This separation enables flexibility and decoupling.
- Why a Hexagon?
- No strict reason; the hexagon shape is convenient to represent multiple ports.
- Analogous to a honeycomb where multiple hexagons (modules) connect.
- Supports modularization and domain-driven design by splitting large applications into smaller, domain-specific hexagons (e.g., user management, search, file persistence, email sending).
Methodology / Key Instructions
- Implementing Hexagonal Architecture:
- Define ports as interfaces that represent all inputs and outputs your application needs.
- Example: A port with generic
read
andwrite
methods instead of database-specific interfaces.
- Example: A port with generic
- Create adapters that implement these ports to interact with specific external systems.
- Example: An adapter to write data to a relational database, another to write to a file system, or an API.
- Ensure the core application logic depends only on ports, not on adapters or external systems.
- Use this pattern for both input and output sides of your application.
- Define ports as interfaces that represent all inputs and outputs your application needs.
- Applying Hexagonal Architecture in Large Applications:
- Break down large applications into multiple hexagons, each responsible for a single domain.
- Connect these hexagons via Ports and Adapters, forming a modular, honeycomb-like structure.
Pros and Cons of Hexagonal Architecture
- Pros:
- Testability: Decoupling via Ports and Adapters makes unit testing easier since dependencies can be mocked or stubbed.
- Maintainability: Changing underlying technologies (e.g., switching databases) only requires swapping adapters, not changing core logic.
- Flexibility: Easily add or change input/output mechanisms without affecting core logic; can redirect outputs to different systems or hexagons.
- Cons:
- Increased Complexity: More code and abstractions to maintain; introduces additional layers.
- Development Overhead: Running multiple isolated components locally can be cumbersome, similar to microservices challenges.
- Potential Performance Impact: Communication between hexagons via APIs can introduce latency.
When to Use Hexagonal Architecture
- Suitable for large, complex applications with multiple inputs and outputs.
- Beneficial for mature applications expected to evolve and change technologies over time.
- Probably not worth the effort for small or simple applications.
- Consider adopting when you need to add new input methods or swap out underlying technologies to improve maintainability and flexibility.
Speakers / Sources Featured
- Narrator / Host: The sole speaker explaining Hexagonal Architecture.
- Alistair Cockburn: Credited as the originator of the Hexagonal Architecture (Ports and Adapters) concept.
This video offers a practical and accessible overview of Hexagonal Architecture, emphasizing its design principles, implementation strategy, and real-world trade-offs to help software developers decide when and how to apply this pattern.
Notable Quotes
— 01:43 — « A port is really just an abstraction, a way for your application to interact with the outside world without knowing anything about what it's interacting with. »
— 02:55 — « No matter what you change in your adapter, the application itself with its port never changes. »
— 05:07 — « One of the main pros of hexagonal architecture is testability, because everything uses abstractions by design, making it a lot easier to test. »
— 06:21 — « One of the main cons is added complexity: instead of writing directly to the database, you introduce ports and adapters, adding more code to maintain. »
— 07:54 — « If your application is going to stay around for a while, you're probably going to need to change the underlying technologies from time to time. »
Category
Educational