Skip to content
IT glossary
ArchitectureUpdated December 3, 2026

Hexagonal architecture

Гексагональная архитектура

A way to organize an application so its business logic does not depend on a particular database, web framework, or vendor. Ports define how the application interacts with the outside world, and adapters connect those ports to specific technologies.

Also known as: порты и адаптеры, ports and adapters

What the hexagon represents

Hexagonal architecture, also called ports and adapters, draws a boundary between what an application does and how it communicates with the outside world. A booking can be confirmed from a web page or a command-line tool, but the rule that a canceled booking cannot be confirmed belongs in one place. The hexagon is a drawing convention, not a requirement to have six interfaces, modules, or layers.

Ports define the boundary; adapters cross it

An inbound port describes an operation the application offers, such as ConfirmBooking. An HTTP handler is an inbound adapter: it reads a request, calls the operation with application-level input, and turns the result into an HTTP response. A CLI command could call the same port without knowing anything about HTTP.

An outbound port describes a capability the application needs, such as loading and saving a booking. The core defines that contract in terms it understands. A PostgreSQL adapter implements the contract and translates between application objects and database rows. A port does not have to be a language-level interface: a public method can serve as an inbound contract.

The key distinction is between runtime calls and source-code dependencies. The use case calls the repository at runtime, but its code refers only to the repository contract, not to the PostgreSQL implementation. The implementation depends on the core's contract. Startup code connects the two, for example by passing a repository into the use case's constructor. No dependency injection framework is required.

Example: testing a booking rule

Suppose ConfirmBooking loads a booking, checks its status, and saves it as confirmed only if the transition is allowed. A unit test can supply an in-memory repository containing a canceled booking, invoke the use case directly, and check that the operation rejects the request and leaves the booking unchanged. The test needs neither a web server nor a database. An integration test against PostgreSQL answers a different question: does the real adapter persist and retrieve the status correctly?

When the separation pays off

The pattern can be used inside either a monolith or a microservice. It is useful when workflows have meaningful rules to test on their own or when several entry points share the same behavior. For a small service that mostly passes data through to a database, extra contracts and data mapping may cost more than they save. Package names are not enough: if a use case accepts a framework request object or returns an ORM entity, infrastructure details still cross the boundary.

How it comes up in interviews

Be ready to sketch one request from the HTTP handler to the use case and then to storage. Draw the runtime call arrows separately from the code dependency arrows. If asked what changes when the database changes, explain that the adapter and its wiring normally change first. The core stays unchanged only if the replacement still honors the port's contract, including any guarantees the use case relies on.