Backend / Hexagonal architecture

Hexagonal architecture

A shop’s order code is rebuilt so its rules sit in the middle and every database, screen and outside service plugs in from the edge. Then we swap one, and test without any.

Intermediate7 steps~7 min
Core — the business rules, and nothing else
Port — an interface the core defines
Adapter — fits one port to one technology
Outside — browsers, databases, other services
// THE SHORT VERSION

In 30 seconds

Hexagonal architecture keeps a program’s business rules in a core that knows nothing about databases, web pages or other companies’ services.

The core talks to the outside only through ports: interfaces it defines itself. An adapter fits one port to one real technology, such as an SQL database or a web framework.

Because adapters depend on the core and never the other way round, a technology can be swapped by writing a new adapter, and the rules can be tested with simple stand-ins plugged into the ports.

Hexagonal architecture, step by step

STEPS01 / 07

Also called ports and adapters; both names come from Alistair Cockburn. The six sides mean nothing: a hexagon just leaves room to draw several ports. PayCo is an invented company.

STEP 01 / 07 Rules tangled with tools
THE MATH
one function: web + rules + SQL + API
  1. Step 1: Rules tangled with tools. A shop’s “place order” code often grows like this: one function reads the web request, works out the total, checks stock, calls the payment company and writes SQL. Only two of its six lines are the shop’s actual rules. The rest is plumbing, and the rules cannot be run, tested or reused without all of it.
  2. Step 2: Put the rules in the centre. Hexagonal architecture starts by pulling the rules out into a core that stands on its own. The core knows how to total an order and when to refuse one. It does not mention a database, a web framework or a payment company, and it would still be correct if all three were replaced. Everything else is “outside”.
  3. Step 3: Ports: the core’s sockets. The core still has to be called, and it still needs things saved and charged. It states this as ports: interfaces written in its own vocabulary. An inbound port, PlaceOrder, is how the outside asks it to act. Outbound ports, OrderStore and Payments, are what it needs from outside. A port says what, never how.
  4. Step 4: Adapters plug in. An adapter is the piece that fits one port to one technology. The web adapter turns an HTTP request into a call on PlaceOrder. The Postgres adapter implements OrderStore by running SQL. The PayCo adapter implements Payments by calling that company’s API. Each adapter is small, and it is the only place that knows its technology.
  5. Step 5: Dependencies point inward. This is the rule that makes the shape work. Every adapter knows the port it plugs into, so adapter code refers to the core. The core refers to nothing outside itself: it calls its own port and has no idea what is behind it. The real adapters are chosen and plugged in once, when the program starts.
  6. Step 6: Swap an adapter. Suppose the shop moves its orders to a different database. Someone writes a new adapter for the same OrderStore port and plugs it in where the old one was. The core is not touched, because it only ever spoke to the port. The same goes for adding a second way in, such as a command-line tool beside the website.
  7. Step 7: Test the core alone. The same trick gives fast tests. A test plugs simple stand-ins into the ports: a store that keeps orders in a list, and a payment fake that just records what it was asked to charge. It then calls PlaceOrder directly and checks the answer. The real rules run with no browser, no database and no network.
VALUES SO FAR
mixed4 concernsproblem
core····centre
port····inbound
port····outbound
port····outbound
adapters····edge
depends····rule
swap····result
test····no I/O

Spotted a mistake on this page?