C4 model
Модель C4
The C4 model describes software architecture with diagrams that zoom in from users and external systems to applications, their internal parts, and code.
What is the C4 model?
C4 gives readers a way to move from the big picture to implementation detail without cramming both onto one diagram. A product manager can see who uses a system and what it depends on; an engineer can zoom in to see which application owns a responsibility. The four Cs stand for Context, Container, Component, and Code.
The four levels
- System context: Treat the system as one box. Show the people who use it and the external systems it interacts with. This view answers what is inside the scope of the discussion, not how the system is built.
- Container: Open the system box to show its applications and data stores, their responsibilities, and how they communicate. A browser application, a backend API, and a database can each be a C4 container.
- Component: Open one container. Show the major groups of code inside it and their dependencies. A component has a distinct responsibility but is not a separately deployed application.
- Code: Open one component to show implementation details such as classes and interfaces. A UML class diagram is one way to do this; C4 does not require a particular drawing notation.
Example: an online bookstore
At the context level, shoppers use the bookstore and the bookstore relies on an external payment provider. The container view shows a web app sending checkout requests to a backend API over HTTPS. The API saves orders in a database and requests payments from the provider. A component diagram for the API can show a checkout handler, an order service, and a payment adapter. A code diagram might then show the adapter's interface and the class that makes requests to the provider. The provider remains outside the bookstore's system boundary even though the adapter is inside it.
When to use it and what to watch for
Context and container diagrams are often enough for onboarding or an architecture review. Add a component view if the team needs to discuss how responsibilities are split inside an application. Label arrows with what the source does, such as “submits an order,” rather than a generic “uses.”
A C4 container is not a Docker container. A backend API can appear once on the container diagram even if several Docker containers run copies of it. Use a deployment diagram to show where those instances run. Likewise, the arrows on a structural view do not tell you which call happens first; use a dynamic diagram if the order of checkout calls matters.
In a system design interview
Start by agreeing on the system boundary and the external dependencies. Then sketch the applications and data stores and explain their responsibilities. If the interviewer asks which part of the API owns the checkout rules, zoom into that API. You rarely need all four levels: the useful level is the one that answers the question being discussed.