What is C4 Model
The C4 Model is a lean graphical notation technique for visualising software architecture. It helps software development teams to describe and communicate software architecture. The C4 refers to Context, Container, Component and Code diagram.
The C4 Model was created by Simon Brown. Official documentation for this can be found here.
It takes an outside in or top down approach. Starts with abstract and then zooms into the details. Something, like what you would do with Google maps. You start from the zoomed out view and then slowly zoom in to the place of interest.
It has a small set of abstractions and diagram types to use when creating architecture diagrams. Unlike UML, you do not have to deal with too many symbols. This makes is easy for developers to learn and use them.
The four C in the C4 model
- Context diagram - This is the top most view. You start at this level and then double click on System abstraction.
- Container diagram - This is the second level. This zooms into the software system showing high level applications blocks.
- Component diagram - This is the third level. This zooms into a specific Container and shows components inside them.
- Code diagram - This is the fourth level. This shows how the component has been implemented.
So from this perspective, a Software system will be made up of Containers, each of these will have Components, these in turn will be implemented by classes.
Elements of C4 Model
- Person
- Software System
- Container
- Component
Person
This would be any human interacting with the software system. This is an actor in traditional sense. I prefer to call this as a persona rather than person. A system can have different types of users and it is good to highlight the different types of users who would interact with our software system. Examples in Healthcare provider system, you can have Patients, Nurses, Practioners, Providers, Administration Staff, and Super Users.
Software System
This is an abstraction of the software system that you are modeling. This is the highest level of abstraction for something that is going to deliver value to its users. It can also be the software systems that your system depends on or is interacting with. Different organizations might have different terminologies for this like - application or product. A System will have one or more Container (This is NOT a Docker Container).
Container
A Container represents an application or a service or a data store. This is a deployable unit and runs in its own memory space. Any communication between two Containers will result in inter-process communication. A Container can have a separate repository if you are not working monorepo. A Container might be coded in a different coding language than another container in the samme system. A Container might have a dedicated team for building and maintaining it.
Some examples of Containers are:
- Web Application
- Mobile Application
- Web API
- Batch Job
- Message Queue
- Database
- File Store
Component
A Component in C4 Model is a grouping of related functionality. These are not independently deployable unit. Any communication between different components with in a single container happens in process.
Here are the basic symbols you will make use of when creating the C4 Model diagratms.
Diagraming with The C4 Model
Let us take an example of a simple patient registeration system and do the exercise of visualizing the architecture diagrams using the C4 Model. In this system patient can self register themselves using a mobile application. In the case where patients walk in to the hospital the frontdesk staff will register the patient. These registrations are persisted and tracked in the registration system's database. The registration system also needs to pass on this information to the main hospital managment system.
Context Diagram
The context diagram is the starting point or the top most level where you start diagraming in The C4 Model. This is ten thousand feet view of the system. Here the system under consideration is abstracted away into a System Symbol and the focus is on the different personas that are going to interact with the system and the different third party / external systems that are interacting with our sysetm.
In our patient registeration system, we have two types of users or personas - Patients and Frontdesk Staff. We will show our Registration system at the highes abstraction level. Our, system needs to interact with an external system, which is the hospital managment system.
Container Diagram
Container diagram is double click into the System under consideration in the above Context diagram. This is the next level in the C4 Model. You want to show various deployable units that make up the system. It shows the high level shape of the software architecture. It also helps highlight responsibilities and technical choices for each of the constituent of the system. This is what would show at a high level that a Web Application is interacting with Web API which persists data in a Database.
In our case double clicking into the registration system, we will show a container for mobile application that patients use to self register and a web application that frontdesk staff will use to register walkin patients. We will also show that our mobile application and web application both these containers rely on a REST Web API Container. This REST Web API container further persists data in a Database and is also responsible for passing the registration information to the hospital management system. It will also show that our technology choices for each of the Containers. In our case Mobile App is in ReactNative, Web Application is in ReactJS, Web API is in NodeJS, and Database is PostgreSQL.
Component Diagram
You get to a Component diagram when you double click on any of the Container in the Container diagram. This will show how a Container is made up of many Components. This is one more level down. At this level you want to show major components or logical units that make up a Container. I personally stop at this level.
Code Diagram
Code Diagram is an optional diagram. I personally do not go to this level. This is the last level in the C4 Model. You want to create this diagram for any complex implementation detail in your component.
There are a few other diagrams you can create like:
- System Landscape Diagram
- Dynamic Diagram
- Deployment Diagram
I have found that Context, Container, Component are the most useful diagram. I also find Deployment Diagrams to be useful. However, for Deployment Diagram I prefer making use of symbols and structures that identify more to the target environment like using AWS symbols, GCP symbols or Azure symbols.