Diagrams that help
Last updated
A good diagram lets a new teammate understand a system in two minutes; a bad one is a wall of boxes nobody reads twice. In this lesson you'll draw Pila the way the C4 model suggests, starting from the whole and zooming in, build a sequence diagram for one flow, and learn which diagrams are worth keeping up to date.
By the end of this lesson you can
- Read and draw a C4 context diagram and container diagram
- Put the messages of one flow in order as a sequence diagram
- Choose the level of detail a diagram needs for its reader
Why draw?
A diagram is for a reader with a question. "What is Pila, and what does it talk to?" is Kap. Ernesto's question. "Which part handles texts?" is a new developer's. "What happens, step by step, when a patient cancels?" is Rina's, before she tests it. One diagram can't answer all three; trying to makes one nobody can read.
The architecture standard, ISO/IEC/IEEE 42010, says the same thing formally: describe a system from several viewpoints, each for the concerns of particular stakeholders. In practice: decide who the diagram is for, and leave out everything they don't need.
Zoom in: context, then containers
Simon Brown's C4 model gives a simple set of zoom levels, like a map: context, containers, components, code. Most teams find the first two do most of the work.
A context diagram shows the system as one box, the people who use it, and the other systems it talks to. Nothing inside.
- In the middle: Pila, the queue system, drawn as one box.
- Patients, often with basic phones, get a number from it.
- Ate Cora and the counter staff call and print numbers with it.
- Pila sends and receives texts through the SMS company, another system.
- Pila produces a monthly report for the health office.
A container diagram zooms into that box. A container here means a separately running piece: an application, a database, a page running in a browser. (It isn't the same as a Docker container, though one may run in one.) Each arrow says how the pieces talk.
- A patient texts through the SMS company, which calls Pila’s web app (written in Python) over HTTPS.
- The web app reads and writes the database.
- Counter staff use the counter page in a browser, which talks to the web app over HTTPS.
- The waiting-room screen is another browser page, asking the web app who’s next.
The next levels, components inside one container and then code, change so often that many teams draw them only when needed, or generate them from the code.
One flow, in order
Box diagrams show structure. A sequence diagram shows behaviour: the messages passed between parts for one flow, top to bottom in time order. Each part is a lifeline, a box with a dashed line down from it, and each message is an arrow from one lifeline to another.
Here are the messages when a patient texts CANCEL, out of order. Put them in the order they happen.
- 1 Storage → queue rules: “ticket 38”
- 2 SMS gateway → Pila web server: “a text from 0917…: CANCEL”
- 3 Queue rules → SMS module → gateway: “send: Number 38 cancelled”
- 4 Patient’s phone → SMS gateway: the text CANCEL
- 5 Queue rules → storage: “today’s ticket for 0917…?”
- 6 SMS gateway → patient’s phone: the reply text
- 7 Web server → queue rules:
cancel(phone) - 8 Queue rules → storage: “mark 38 cancelled”
Here is the same flow drawn as a UML sequence diagram, with one more part: the SMS module that sends the reply. A frame marked opt holds the messages that happen only when its [condition] holds; UML also has alt for either-or and loop for repeats.
- The patient texts CANCEL to the SMS gateway, which calls the web server; the web server calls cancel(phone) on the queue rules.
- The queue rules ask storage for today’s ticket for that phone, and get ticket 38 back.
- Only if a ticket was found (the opt frame): storage marks 38 cancelled, the queue rules ask the SMS module to send “38 cancelled”, and it reaches the patient through the gateway.
It shows at a glance that the queue rules talk to storage twice, and that the reply waits on both. That's where Rina will look first when replies are slow.
A little UML, and diagrams as text
UML, the Unified Modeling Language, is a standard set of diagram types from the 1990s. Two besides the sequence diagram are worth knowing:
- Class diagrams: classes, their attributes and methods, and how they relate. Simple Python drew these for its own programs.
- Activity diagrams: actions, decisions and loops. Stage 2 drew every life-cycle model as one.
The C4 diagrams in step 2 aren't UML: C4 deliberately uses plain labelled boxes and arrows instead of UML's notation.
Few teams use all of UML's detail today, but when a team draws one of these diagrams, UML's notation means everyone reads it the same way.
A diagram drawn in a drawing program goes out of date the day someone changes the code. Diagrams as text help: tools such as Mermaid and PlantUML draw a diagram from a few lines kept in the repository beside the code, so changing the design and changing the diagram happen in the same commit and the same review.
sequenceDiagram
Phone->>Gateway: CANCEL
Gateway->>Web: a text from 0917…
Web->>Queue: cancel(phone)Spot the trap: the diagram of everything
Marco spends two days on one huge diagram of Pila: every function, every database column, every page and every arrow between them. He prints it on A2 and pins it up.
What happens if…
Check what you learned
Sources
Every claim on this page comes from these sources: the standard, the founding text of a method, a study, or the official record of a project, from the body that publishes it. Industry reports are labelled as such.
- Simon Brown: The C4 model: context, container, component and code diagrams, from the model’s author
- The C4 model: System context diagram: the system as one box, with its users and the systems around it
- The C4 model: Container diagram: the separately running parts inside the system
- ISO/IEC/IEEE 42010:2022, Architecture description: describing an architecture from several viewpoints, for different readers
- Object Management Group: Unified Modeling Language (UML) 2.5.1: the UML standard: sequence, class and activity diagrams and their notation