The C4 container diagram
Level 2 of the C4 model, drawn end to end - and the one level whose name causes more confusion than the diagram itself ever does.
6 min readC4 model2 of 3
The short answer
- A container is anything separately deployable or runnable: an application, an API, a database, a broker. Not a Docker container.
- Every box gets its technology and every line gets its protocol. A container diagram without those is a box-and-arrow sketch.
- Draw the arrow in the direction of the call, not the direction of the data. The reply is implied and drawing it doubles the lines for nothing.
- External systems stay on the diagram but stay outside the boundary, greyed - they explain the edges without pretending you control them.
01One system, opened#
A container diagram takes the single box from the system context diagram and opens it. Everything inside is something that has to be running; everything outside stays as context.
Read it as a set of sentences: the customer browses the single-page application; the application calls the storefront API over HTTPS; the API writes orders to PostgreSQL and publishes to RabbitMQ; the worker consumes from RabbitMQ and writes to the same database. That last one is the sentence a container diagram exists to make visible - two containers writing to one database is a coupling that no service catalogue shows.
02Technology on the box, protocol on the line#
These two labels are what separate a container diagram from a sketch. Without the technology, a box called "Storefront API" could be a Lambda, a Rails monolith or a stored procedure. Without the protocol, a line between two boxes could be a synchronous call in the request path or a nightly batch job - which are opposite facts for anybody reasoning about failure.
Draw the arrow in the direction of the call rather than the direction of the data. A read is still a call from the reader to the store, and drawing the response as a second arrow doubles the line count while adding nothing - the reply is implied by the request.
03The four that make it useless#
Leaving out the datastores. A diagram of services with no databases hides exactly the coupling that causes incidents. If two containers write the same table, that has to be on the picture.
Mixing levels. A container diagram with one container opened into its internal classes is two diagrams badly overlaid. Open it on a component diagram instead, where the audience expects that depth.
Drawing infrastructure. Load balancers, sidecars and Kubernetes nodes belong on a deployment diagram. A container diagram is about what the software is, not where it runs.
Silently dropping external systems. The payment provider is not yours and cannot be omitted for that reason - it is where your latency and half your failure modes come from. Keep it, mark it external, and stop there.
In one line each
- 01A container is separately deployable or runnable - application, API, database, broker, job.
- 02Label every box with its technology and every line with its protocol, or the diagram says nothing.
- 03Arrows follow calls, not data. The response is implied.
- 04Datastores and external systems stay on the diagram; infrastructure moves to a deployment diagram.
04Common questions#
What counts as a container in C4?
Anything that has to be running for the system to work and that can be deployed or started separately: a web application, a single-page app, a mobile app, an API, a database, a message broker, a scheduled job. The test is deployability, not process count and not technology.
Is a Docker container a C4 container?
Not necessarily. The names collide and mean different things. Three processes in one image are three C4 containers if the team reasons about them separately; one service running on forty replicas is one C4 container. Ignore the packaging and ask what is separately deployable.
Should microservices each be a container?
Yes, and that is the honest reason container diagrams get large. If forty services make the diagram unreadable, that is information about the architecture rather than about the notation - group them by bounded context and draw the groups, or draw one diagram per context.
Where do queues and databases go?
On the diagram, as containers, with their technology labelled. Leaving out the database is the most common way a container diagram becomes misleading: it hides the coupling between the two services that both write to it.
In this series
- 01What is C4?
- 02Container diagram
- 03C4 vs UML
Related reading
Foundations
Modelling practice
Structure diagrams
Modelling practice