Clean Architecture
The four rings of Clean Architecture, the Dependency Rule, how a call crosses a boundary when control flows outward, and when the rings are too much.
Overview
Clean Architecture is the name Robert Martin (Uncle Bob) gave to a family of designs that keep business rules in the middle of a system and push the web framework, the database and the user interface to the edge. He set it out in a blog post in August 2012 and later in a book, Clean Architecture: A Craftsman's Guide to Software Structure and Design (Pearson, 2017).
The post is a synthesis, not a new invention. It lists Hexagonal Architecture (Alistair Cockburn), Onion Architecture (Jeffrey Palermo), his own Screaming Architecture, DCI and BCE, and says they share one goal: a system that is independent of frameworks, of the UI, of the database and of outside agencies, and that can be tested without any of them. Clean Architecture adds two things on top of that shared goal: it names rings inside the core, and it states one rule for source code that holds across every ring.
Interviewers use it to check three things: can you say the Dependency Rule in one sentence, can you show how a use case talks to a presenter or a database without depending on it, and do you know when the rings cost more than they save.
The four rings
From the inside out, the post names four rings. Martin says four is a sketch, not a law; a system may need more, as long as every dependency still points inward. The inner rings are the most abstract and change least often. Seen from the edge, the way a request arrives, the frameworks and drivers depend on the interface adapters, which depend on the use cases, which depend on the entities. Every one of those dependencies points inward, and nothing inside may name anything outside.
- Entities: the business rules, the ones that would hold even without this application. They change only when the business itself changes. Course.enroll() refusing a full course is an entity rule.
- Use cases: the application rules, specific to this application, with one class per thing a user can do. They change when the application's behavior changes. EnrollStudent loads a course, enrolls the student and saves.
- Interface adapters: the controllers, presenters and gateways that convert between the use cases' data and the outside world's formats. They change when a screen, an API shape or a schema changes. A controller, a presenter and a SQL gateway all live here.
- Frameworks and drivers: the tools themselves (the web framework, the database, the UI) plus thin glue code. They change when you swap or upgrade a tool, such as Flask, PostgreSQL or a message broker client.
The split between the two inner rings is what Clean adds to Hexagonal. An entity holds a rule the business would follow on paper; a use case holds the steps this one application takes to apply it.
Interview questions
- What is the Dependency Rule, and how does a use case send its result to a presenter without breaking it?
- Why should DTOs cross the boundary instead of ORM entities?
- What belongs in an entity and what belongs in a use case?
- How do you test each ring?
- When would you not use Clean Architecture, and how would you scale it down?
Sign up free to read the full topic
More Low-Level Design topics
Start free on Cariara · Coding problems · System design studio