Clean architecture
Чистая архитектура
This approach organizes an application so its business rules don’t depend on a database, web framework, or user interface. You can test those rules without starting the surrounding infrastructure.
Also known as: гексагональная архитектура
What Clean Architecture separates
Imagine customers can cancel an order from a website or through a support tool. The rule that a shipped order can’t be canceled should be the same in both places. Clean Architecture keeps that rule in the core of the application rather than burying it in a web controller or a database integration.
The dependency rule
Source code dependencies point inward, toward business policy. The core does not import the web framework, database driver, or types defined by those tools. A typical implementation separates three responsibilities:
- Domain: Enforces business rules. An
Orderknows whether its current status allows cancellation. - Application: Coordinates use cases.
CancelOrderloads the order, asks it to cancel, and saves the result. - Adapters and infrastructure: Translate incoming requests and connect the application to external systems. An HTTP handler maps a request to use-case inputs, and a database adapter maps between stored records and domain objects.
The arrows in a dependency diagram are not necessarily the arrows in a runtime call trace. A use case can call a database adapter through an interface defined by the application. The adapter imports that interface to implement it; the use case never imports the adapter. That reversal is dependency inversion.
Example: cancel an order without starting a database
CancelOrder(orderId, orderStore):
order = orderStore.load(orderId)
order.cancel()
orderStore.save(order)
At startup, the application wires in a PostgreSQL adapter for orderStore. In a unit test, an in-memory implementation can supply a shipped order so the test can verify that cancellation is rejected. That test exercises the rule and the use case without a database. It doesn’t tell you whether the PostgreSQL adapter can correctly save a canceled order; test that separately with a real database.
When the boundaries pay off
This structure helps when business rules are complex or the same use case has multiple entry points, such as HTTP and a background worker. On a small CRUD service, extra interfaces and mappings may cost more to maintain than they save. Don’t judge an implementation by its folder names or by how many rings it has. If a use case takes a framework request object or returns an ORM record, infrastructure types have crossed the boundary.
Hexagonal architecture is closely related, not a synonym. It describes the boundary between an application and the outside world in terms of ports and adapters. Clean Architecture also distinguishes domain rules from application use cases and explicitly sets the direction of dependencies between layers.
What interviewers look for
Pick an operation and trace it from the request handler through the use case to storage. Then say which modules import which types. Explaining how a call can reach the database while the source dependencies still point inward is more useful than naming every layer in a diagram.