Design · lesson 4 of 10
Ideas in this lesson
AC4 context and containerthe system in its world, then its main parts
BSequence diagramwho says what to whom, in order
CDiagrams as textkept beside the code, changed with it
DClient and serverfrom lesson 4.2

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.

gets a numbercalls, printssends, receives textsmonthly reportPatient(often a basic phone)Ate Cora andcounter staffPilathe queue systemSMS companyHealth office
  1. In the middle: Pila, the queue system, drawn as one box.
  2. Patients, often with basic phones, get a number from it.
  3. Ate Cora and the counter staff call and print numbers with it.
  4. Pila sends and receives texts through the SMS company, another system.
  5. Pila produces a monthly report for the health office.
Pila as a C4 context diagram. The system is the solid box in the middle; dashed boxes are other systems; Pink pills are people. Each arrow says what passes along it. C4 diagrams aren’t UML: they use plain boxes and arrows, with every arrow labelled.

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.

textsHTTPSSQLHTTPSHTTPSPatientSMS companyWeb app(Python)DatabaseCounter staffCounter page(in a browser)Waiting-roomscreen (browser)
  1. A patient texts through the SMS company, which calls Pila’s web app (written in Python) over HTTPS.
  2. The web app reads and writes the database.
  3. Counter staff use the counter page in a browser, which talks to the web app over HTTPS.
  4. The waiting-room screen is another browser page, asking the web app who’s next.
Pila as a C4 container diagram. Solid boxes are Pila’s containers: separately running pieces. Dashed boxes are outside Pila; Pink pills are people. C4 diagrams aren’t UML: they use plain boxes and arrows, with every arrow labelled.

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. 1 Storage → queue rules: “ticket 38”
  2. 2 SMS gateway → Pila web server: “a text from 0917…: CANCEL”
  3. 3 Queue rules → SMS module → gateway: “send: Number 38 cancelled”
  4. 4 Patient’s phone → SMS gateway: the text CANCEL
  5. 5 Queue rules → storage: “today’s ticket for 0917…?”
  6. 6 SMS gateway → patient’s phone: the reply text
  7. 7 Web server → queue rules: cancel(phone)
  8. 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.

Patient:SMS gateway:Web server:Queue rules:Storage:SMS moduleopt[a ticket was found]text “CANCEL”POST: from 0917…cancel(phone)today_ticket(phone)ticket 38mark_cancelled(38)send(“38 cancelled”)POST reply textthe reply text
  1. The patient texts CANCEL to the SMS gateway, which calls the web server; the web server calls cancel(phone) on the queue rules.
  2. The queue rules ask storage for today’s ticket for that phone, and get ticket 38 back.
  3. 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.
A patient cancels by text. A UML sequence diagram: each part is a lifeline, a box (or a stick figure for a person) with a dashed line below it, and time runs down the page. A solid arrow with a filled head is a call whose sender waits for the answer; an open head means the sender doesn’t wait; a dashed arrow is a reply; a labelled frame holds messages that happen only under its [condition].

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

Spot the trapMarco

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

Which diagram answers Kap. Ernesto’s question: “What is Pila, and what does it connect to?”
In C4, what is a container?
Why keep diagrams as text beside the code?

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.

All sources for Simple Software Development