Many engineering teams fall into one of two architectural traps: they prematurely over-engineer simple microservices with complex abstraction layers, or they neglect architecture entirely until their core backend becomes an untestable monolith where business rules are entangled with database queries.
When you mix your framework, database, and business logic into a single unlayered codebase, swapping infrastructure components becomes a nightmare. Testing unit logic requires spinning up real database instances, and simple feature additions start breaking unrelated parts of the system.
Here is how to evaluate whether your project actually needs Clean Architecture, and how to implement it cleanly without adding unnecessary friction.
The Checklist: Do You Need Clean Architecture?
Before introducing multiple layers of abstractions, evaluate your domain against three questions:
- Does your domain contain complex business rules that will frequently evolve?
- Will multiple teams touch and extend this service over several years?
- Is unit testability without external dependencies (like DBs or external APIs) non-negotiable?
If you answered yes to these questions, Clean Architecture earns its keep. Your core domain stays isolated at the center, completely untouched by database migrations, framework updates, or external vendor swaps.
However, if you are building a straightforward CRUD application, a temporary prototype, or a narrow-scoped microservice, skip the overhead. A clean, single-file or basic module layout will ship faster and remain easy to manage.
Step 1: Define Core Entities and Interfaces
The innermost layer consists of your domain entities and repository interfaces. This layer has zero dependencies on web frameworks, ORMs, or database drivers.
Here is an example in Python defining a domain entity and an abstract repository interface using Protocol:
# domain/entities.py from dataclasses import dataclass from typing import Protocol, Optional @dataclass class Order: id: str total_amount: float is_paid: bool = False def mark_as_paid(self) -> None: if self.total_amount <= 0: raise ValueError("Cannot process payment for non-positive amount") self.is_paid = True # domain/interfaces.py class OrderRepository(Protocol): def get_by_id(self, order_id: str) -> Optional[Order]: ... def save(self, order: Order) -> None: ... Notice how the Order entity enforces business rules (such as validating payment amounts) independently of how or where the order is stored.
Step 2: Implement Pure Application Use Cases
The next layer contains your application use cases. Each use case orchestrates the domain entities and abstract interfaces to execute a specific business operation.
# use_cases/pay_order.py from domain.entities import Order from domain.interfaces import OrderRepository class PayOrderUseCase: def __init__(self, repository: OrderRepository): self.repository = repository def execute(self, order_id: str) -> Order: order = self.repository.get_by_id(order_id) if not order: raise ValueError(f"Order {order_id} not found") order.mark_as_paid() self.repository.save(order) return order Because PayOrderUseCase relies on the abstract OrderRepository contract, you can test this entire flow using an in-memory mock repository without touching a live database.
Step 3: Implement Infrastructure Adapters
The outermost layer implements the concrete infrastructure components, such as database repositories, external API clients, and HTTP controllers.
# infrastructure/postgres_repository.py from domain.entities import Order class PostgresOrderRepository: def __init__(self, db_session): self.session = db_session def get_by_id(self, order_id: str) -> Order | None: record = self.session.query(OrderORM).filter_by(id=order_id).first() if not record: return None return Order(id=record.id, total_amount=record.total_amount, is_paid=record.is_paid) def save(self, order: Order) -> None: record = self.session.query(OrderORM).filter_by(id=order.id).first() if record: record.is_paid = order.is_paid self.session.commit() If you ever need to replace PostgreSQL with MongoDB, DynamoDB, or an external third-party API, you only write a new adapter. Your use cases and domain entities remain completely unchanged.
Balance Abstraction with Pragmatism
Clean Architecture is a tool to manage complexity, not a goal in itself. For large-scale systems or core business services, isolating your business logic ensures longevity and testability. For simpler, single-purpose utilities, favor flat structures to maintain development speed. The real engineering skill lies in recognizing which approach the current problem demands.
About me
Tanmay builds AI agents and automation for businesses and is open to freelance projects. Feel free to connect on LinkedIn.
