All articles
165 articles · updated weekly See our Tools
All articles
Tutorials

Clean Architecture: real benefits and common pitfalls

Four layers, twelve interfaces, and a Use Case that runs a single query. How Clean Architecture leaves the books and becomes overengineering — and when it actually pays off.

Clean Architecture: real benefits and common pitfalls
COVER · Tutorials

You decide to adopt Clean Architecture on the new project. Three weeks later, you have four layers, twelve interfaces, and a UseCase that runs a single database query. The code is "architecturally correct" and completely unreadable to anyone new on the team.

This isn't an isolated implementation failure. It's the most common path Clean Architecture takes from the book to real projects. This post sums up the pros and cons of Clean Architecture, the pitfalls that turn it into overengineering, and the criteria for deciding when it pays off. If you want a complete ports-and-adapters example with code, it lives in the sibling post on hexagonal architecture; I don't repeat that code here.

What Clean Architecture actually says

Clean Architecture is the model Robert C. Martin described in the post The Clean Architecture, from August 2012, and consolidated in the 2017 book Clean Architecture: A Craftsman's Guide to Software Structure and Design. The central idea is simple: business rules should not depend on details. Database, web framework, UI, delivery mechanism: all of that is detail. The core of the system is the logic that exists regardless of how it's exposed or persisted.

In the post itself, Martin says he is unifying earlier ideas: hexagonal architecture (ports and adapters), Onion Architecture, Screaming Architecture, DCI and BCE. The visual representation is concentric circles, from the inside out:

  • Entities: enterprise business rules. The most stable, with no external dependencies.
  • Use Cases: application rules. In his words, they orchestrate the flow of data to and from the entities.
  • Interface Adapters: controllers, presenters, gateways. They convert data between the format convenient for the use cases and the format convenient for the outside world.
  • Frameworks & Drivers: database, web framework, UI, tools. Everything that is concrete implementation detail.

The Dependency Rule is the only real commandment: source code dependencies only point inward. Nothing in an inner circle can know anything about an outer circle. Use Cases can depend on Entities, never the reverse. Interface Adapters depend on Use Cases; the database and the framework, in the Frameworks & Drivers layer, sit at the edge. What many people call "the infrastructure layer" is the sum of those two outer layers, and Use Cases import nothing from them.

Frameworks & Drivers  →  Interface Adapters  →  Use Cases  →  Entities
(arrows mean "depends on": always from the outside in)

A detail the diagrams tend to hide: Martin writes that the circles are schematic and there's no rule saying there must be exactly four. What always applies is the Dependency Rule. If an arrow points outward, the architecture is broken, no matter how many nicely named folders you created.

Pros and cons of Clean Architecture in one table

Pro Con
Tests Business rules tested without database, HTTP or filesystem Every boundary needs an interface and at least one fake
Framework Upgrading or swapping the framework doesn't touch the core You swim against "batteries included" MVC frameworks
Evolution Infrastructure changes don't cascade into the rules More files and indirection for every new feature
Multiple entry points API, worker and CLI reuse the same use case With a single entry point, the separation protects nothing
Team Explicit boundaries make it easier to split work A team new to the pattern gets boundaries wrong and adds ceremony

The table sums up the trade-off: the pros show up when there are real business rules to protect; the cons show up always, from day one.

The benefits, when they actually apply

Real testability

This is the most concrete benefit. When the use case depends on abstractions instead of implementations, you test business logic without a running database. In Python, the abstraction can be a Protocol declared next to the use case, because the use case is the one that needs it (that's dependency inversion, the "D" in the SOLID principles):

from typing import Protocol

class OrderRepository(Protocol):
    def find(self, order_id: str) -> Order: ...
    def save(self, order: Order) -> None: ...

class Notifier(Protocol):
    def notify(self, order: Order) -> None: ...

class ProcessOrder:
    def __init__(self, repo: OrderRepository, notifier: Notifier):
        self.repo = repo
        self.notifier = notifier

    def execute(self, order_id: str) -> None:
        order = self.repo.find(order_id)
        order.confirm()  # entity rule: only a pending order can be confirmed
        self.repo.save(order)
        self.notifier.notify(order)

In the test, fakes stand in for the database and the email sender:

def test_confirms_saves_and_notifies(self):
    repo = InMemoryOrderRepository(Order("o-1"))
    notifier = FakeNotifier()

    ProcessOrder(repo, notifier).execute("o-1")

    self.assertEqual(repo.find("o-1").status, "confirmed")
    self.assertEqual(notifier.sent, ["o-1"])

I ran the full example (an Order dataclass entity, both fakes and two tests, including the case where an already confirmed order must not be notified again) with unittest on Python 3.12: both pass with no container, no seed data and no network. InMemoryOrderRepository and FakeNotifier are fakes, simple implementations that actually work, not mocks with call expectations; the difference is explained in mock, stub, fake and spy.

In systems with complex business logic (financing calculations, tax rules, dynamic pricing), this is what separates a suite everyone runs locally from a suite nobody runs because it needs half the environment up.

Framework independence

The framework is a delivery detail. With the Dependency Rule in place, switching from Flask to FastAPI or from Express to Fastify doesn't touch business rules. Replacing a framework outright is rare. What happens often is a major version with breaking changes, and an isolated core turns that migration into something surgical.

Code longevity

Business systems evolve: calculation rules change, delivery channels appear, external integrations get replaced. With the rules isolated, infrastructure changes don't cascade into the core. The post on Clean Code without dogma shows how implicit coupling becomes a maintenance bug at the function level; Clean Architecture tackles the same problem at the module level.

The cons and the real pitfalls

Overengineering on small projects

This is the most common mistake. Clean Architecture has a setup cost: more files, more indirection, more abstractions to keep in your head. On a simple CRUD or an API with ten endpoints, that cost never amortizes.

An internal automation script, a webhook that processes events, an admin tool: none of those justify four layers. They justify direct, well-named code with tests where there's non-trivial logic. For those cases, the controller, service and repository model is usually enough.

The right question before adopting it: "will the business logic in this system grow and diversify over time?" If the answer is no, Clean Architecture is overengineering with a nice name.

The folder illusion

The most frequent mistake in codebases that "use Clean Architecture" is confusing folder structure with architecture.

src/
  domain/
    user.py
  use_cases/
    create_user.py
  infrastructure/
    user_repository.py
  controllers/
    user_controller.py

Having these folders guarantees nothing. If use_cases/create_user.py imports infrastructure/user_repository.py directly instead of depending on an abstraction, the Dependency Rule is broken, whatever the directory layout. Clean architecture isn't a filesystem layout; it's the direction of source code dependencies. A cheap way to police it is to look at the imports of every PR that touches use_cases/: any import from infrastructure/ there is an arrow pointing outward.

Anemic use cases

A common symptom of poorly applied Clean Architecture is use cases that do nothing but delegate:

class GetUserById:
    def __init__(self, repo: UserRepository):
        self.repo = repo

    def execute(self, user_id: str) -> User:
        return self.repo.find_by_id(user_id)  # pure delegation, no logic

This is opinion, not a rule from the book: in my experience, a use case like this rarely pays for itself. It's indirection with no benefit, and the controller could call the repository (or a query service) directly. There's a legitimate argument on the other side: on large teams, routing every entry point through a use case gives consistency, an obvious place for authorization and logging, and ends the "does this one deserve a use case" debate. If the team chose that consistency on purpose, fine. The problem is when dozens of these appear out of inertia and the project pays the pattern's full complexity without reaping anything.

Excessive data mapping

Clean Architecture often leads to DTOs at every boundary. That makes sense when formats genuinely diverge: what comes from the database isn't what goes to the client, and what comes from the client isn't what the domain processes. But when every object has the same fields, you end up with five nearly identical classes and mapping code that is a guaranteed bug vector (a new field forgotten in one of the five).

Practical rule: map at the boundaries where the contract actually changes. Don't create mapping objects on principle.

When to use Clean Architecture

Clean Architecture pays off when two or more of these factors are present:

  1. Complex business logic: rules that change independently of infrastructure, that need fast tests, that go beyond CRUD.
  2. Long-lived system: it will be maintained and evolved for years, likely by different people.
  3. Multiple delivery interfaces: REST API, async workers, CLI, webhooks, all hitting the same core.
  4. A team experienced with the pattern: well-executed clean architecture requires the team to understand the why behind the rules. First-timers get the boundaries wrong and create coupling where they shouldn't.

If the project has only one of these factors, suspect overengineering. If it has none, it is overengineering. The heuristic from the hexagonal post applies too: you don't need the whole pattern everywhere. Isolating only the rules module that hurts (pricing, tax calculation) and leaving the rest as direct CRUD is as valid an architectural decision as the four layers.

Frequently asked questions

Is Clean Architecture the same as hexagonal architecture?

No, but they defend the same rule: dependencies pointing toward the core. Martin cites Alistair Cockburn's hexagonal architecture as one of the sources he unified; the difference is that Clean Architecture names concentric layers, while hexagonal organizes the design around inbound and outbound ports. The full comparison, with a PHP example, is in the post on hexagonal architecture.

Can you migrate a legacy project to Clean Architecture?

Yes, but incrementally. Identify the modules with the most business logic and the most change, and extract abstractions at those boundaries first. The rest of the system can stay as it is until refactoring makes economic sense. Trying to apply clean architecture to an entire legacy codebase at once is a recipe for a stalled project.

Does Clean Architecture work with Django or Rails?

It works, but it takes discipline. Conventional MVC frameworks encourage putting logic in the model or the controller. You can create a separate use case layer and use the ORM as the repository implementation, but you'll be swimming against the framework. On a small Django project it's probably not worth the friction; on a large system with real business rules, the separation pays.

What's the difference between Clean Architecture and DDD?

They're complementary. DDD is about how to model the domain: aggregates, entities, value objects, bounded contexts. Clean Architecture is about how to organize dependencies between layers. You can use both, and the post on DDD for small projects shows how much DDD fits in a project that isn't huge.

Clean architecture is about dependencies, not folders

The only criterion that matters is the Dependency Rule. Every other decision (how many layers, how many interfaces, how to name folders) follows from where the dependencies point. A three-file project that respects the rule has clean architecture; a forty-folder project with use cases importing concrete repositories only has the look.

Before adopting it, ask whether the system has enough business complexity to pay for the indirection. If it does, the investment pays back with fast tests and infrastructure changes that don't leak into the rules. If it doesn't, simple, direct, well-tested code is the right architecture. To police the rule day to day, I review the imports of every change to use cases; pasting the before and after into the Code Diff Checker makes it obvious when a new dependency crossed the boundary outward.

RD
Author
Rafael Duarte
Backend developer with a background in fintech and B2B SaaS — worked on teams that scaled APIs from zero to millions of requests. Carries enough production scars to hold strong opinions on tools, patterns and architecture decisions. Not an academic: he read the UUID RFC when he had to choose between v4 and v7 for a write-heavy table.
View profile