How it works · 7 of 49 Core

What is hexagonal architecture (ports and adapters)?

Keep the core ignorant of the database

Hexagonal (ports & adapters): Hexagonal architecture puts the business rules in a core that talks to the outside only through ports, each a fixed interface for one conversation ("fetch a rate"); an adapter per technology (SQL, a file, an in-memory fake, a web form, a test script) fits that port, so you can swap the database or drive the app from a test without touching the core. The price is an interface per port and an adapter per technology: code you write even if you never swap anything.

Loading the animation…

Say it like this Hexagonal keeps the rules in a core that only talks through ports, so the database, the UI or a test is an adapter you can swap without touching the core.

Under the hood — for builders
The source, in its words
Alistair Cockburn, "The Hexagonal (Ports & Adapters) Architecture", HaT Technical Report 2005.02, 4 Sep 2005. Intent: "Create your application to work without either a UI or a database so you can run automated regression-tests against the application". The application is "blissfully ignorant of the nature of the input device". The six sides are room to draw, not a rule: ports number "two, three, or four (four is most I have encountered to date)".
His own example: the Discounter
Port: a purposeful conversation, named for what it is for ("fetch a rate"), not the technology. Adapter: the code that turns one technology into that conversation. Cockburn's sample is discount(amount) = amount × rate(amount); the rate comes through a RateRepository port, and his in-memory adapter answers from three rules:
Run here: what one test costs, per adapter
Cockburn's Discounter in Python, the same three checks (100, 500, 2,000) through each adapter; each test builds a fresh store, as integration tests do. All three give the same answers; the core's source is byte-for-byte unchanged.
What the port saves when the store changes
The same toy, switching rates from SQLite to a CSV file, with the SQL written inside the rule and with the rule behind a port:
Two kinds of side
Driving (primary) adapters call in: a web form, a test, a batch job. Driven (secondary) adapters are called out to: a database, email, a model API. Cockburn: tests stand in on the left, in-memory fakes on the right.
Measure it
Imports of database or web packages inside the core (aim: zero, checked in the build) · seconds to run the core's tests with no database · adapters per port.
Watch out
A port per use case is too many (Cockburn favours two to four). An in-memory fake can drift from the real store: keep a few tests on the real adapter. Don't pass SQL rows through a port.
Sources
Cockburn, "The Hexagonal (Ports & Adapters) Architecture", HaT Technical Report 2005.02, alistair.cockburn.us, read in session · toy Discounter, Python, run for this card 18 Nov 2026.

What happens in the hexagonal (ports & adapters) animation, step by step?

  1. Start where B1 ended: the discount rule calls the database itself. Swap the database for a file and half the rule is rewritten.
  2. Cut the database talk out. The core now only asks "fetch a rate" through a port, and the SQL becomes an adapter that fits it.
  3. Your turn: drag an adapter into the "fetch a rate" port.
  4. With the in-memory adapter the rule is tested with no database at all. Same answer, thousands of times faster.
  5. The other side works the same way: anything that fits the "ask for a discount" port can drive the core.
  6. The price: an interface per port and an adapter per technology, written even if you never swap. Next: boundaries between whole parts of one app.

What everyday example explains hexagonal (ports & adapters)?

A travel adapter. The laptop never changes per country; a small piece between laptop and wall does. In a shape-sorter, any block with the right shape fits the hole, whatever is painted on its face.

Why should a team care about hexagonal (ports & adapters)?

Put a port where a technology might change or where a test needs to stand in (the database, a payment provider, a model API); skip it for code that will only ever talk to one thing. The test that needs no database is the payoff most teams feel first.