Hexagonal Architecture
Learn about hexagonal architecture, a pattern that lets you switch technologies with ease.
Hexagonal architecture is an architectural pattern proposed by Alistair Cockburn. It’s also called ports and adapters, and this name is more appropriate but less popular because hexagonal architecture just sounds better.
The main idea is to divide application core logic from all the technical infrastructure using ports and adapters. For example, let’s imagine we’re building an incident tracker. It gets triggered by an incident and notifies the related people accordingly. There might be many different trigger sources for incidents—logs, monitoring systems, webhooks, or manual triggers by users. There might also be different notification mechanisms—emails, messengers, SMS, or phone calls.
We don’t want to write a separate tracker for each possible trigger source and notification mechanism combination. There are just too many of them. Instead, we can expel all the trigger sources and notification mechanisms from the application core and turn them into plug-ins.
In this case, an application core would provide a single interface (a port) for triggering incidents. And, for each source, we create an adapter that turns a source-specific trigger request into a port call. We also make a standard interface, a port, for notification mechanisms. And we write adapters—classes that implement the port and trigger specific notification mechanisms. After this, we can run any combination without changing the application core just by plugging in different adapters.
Basically, hexagonal architecture is essentially a dependency inversion principle that is just a bit more formalized and on a scale of the whole application.
And, there’s no hidden meaning behind the word hexagonal. Hexagons are just fancy-looking blocks on a schema that provide enough space to draw all the ports.
What does hexagonal architecture mean?
The application schema in the hexagonal architecture looks like this:
Get hands-on with 1200+ tech skills courses.