Skip to content
Tutorials Center
My learning

Topics

Lesson 17 of 39 ยท Sequence and Communication Diagrams

Lifelines, Gates and Interaction Use

Go beyond the basics of sequence diagrams: lost and found messages, gates, interaction use, state invariants, durations, system sequence diagrams and real design workflow.

You now know participants, messages and fragments. This lesson fills in the remaining notation (lost and found messages, gates, interaction references, invariants, time) and then shows how sequence diagrams fit into everyday design work, from a first "system sequence diagram" to a detailed internal design.

Found and lost messages

Normally a message has a visible sender and receiver. Sometimes one end is outside the diagram's scope.

  • A found message has a receiver but an unknown or irrelevant sender. It starts at a filled dot. Example: an event from "somewhere".
  • A lost message has a sender but no receiver in the diagram. It ends at a filled dot or an X. Example: a log call or an event nobody in scope consumes.
sequenceDiagram
    participant D as Device
    participant H as Hub
    participant L as Logger
    Note over D,H: found message: sender is outside the model
    D->>H: heartbeat()
    H-xL: log("heartbeat received")
    Note over H,L: lost message: the receiver is not modeled

Mermaid draws a lost message with the -x arrow. These notations let you stay focused without pretending to know more than you do.

Gates

A gate is a point on the frame boundary where a message enters or leaves a diagram. It connects a sequence diagram to its surroundings. When a fragment or a referenced interaction is drawn on its own, gates show the inputs and outputs of that sub-scenario.

TEXT
frame: sd Authenticate
  gate in:  login(user, password)
  gate out: token

If you split a long scenario into smaller diagrams, gates are the glue: the outer diagram's message into the referenced interaction matches the gate's name.

Interaction use (ref)

An interaction use is a ref box covering some lifelines, with the name of another sequence diagram inside. It means "the steps of that diagram happen here". It is the sequence-diagram equivalent of a function call, and it keeps big scenarios readable.

sequenceDiagram
    actor U as User
    participant W as WebApp
    participant S as Shop
    rect rgb(235, 240, 250)
        Note over U,W: ref: Authenticate
    end
    U->>W: openCart()
    W->>S: getCart()
    S-->>W: cart
    rect rgb(235, 240, 250)
        Note over W,S: ref: Calculate Totals
    end
    W-->>U: show cart

Define each referenced interaction once, and reference it from everywhere it is needed. When a login flow changes, one diagram changes.

State invariants and time

A state invariant is a condition written in curly braces on a lifeline that must hold at that point: {order.status = PAID}. It documents the state of an object between messages and ties sequence diagrams to state diagrams.

Duration and timing constraints annotate how long things may take: {0..2s} next to a message pair means the reply should arrive within two seconds. Use them for requirements such as response time; for detailed analysis UML offers timing diagrams.

sequenceDiagram
    autonumber
    participant C as Client
    participant S as Server
    C->>S: request()
    Note right of S: server must answer within 2 s
    S-->>C: response()
    Note over C: invariant: session.active = true

autonumber adds numbers to messages: handy in reviews ("please look at step 4"), and exactly what communication diagrams use (next lesson).

Three levels of sequence diagram

Teams use sequence diagrams at three levels. Mixing them in one diagram is a common error.

Three levels of detail

Context Actor and system as one box.
Architecture Services and layers.
Detailed Objects and methods.

One diagram, one level.

1 / 2
  1. System sequence diagram (SSD). The system is a single black box. Only the actor and the system appear; messages are the system operations triggered by the actor. SSDs come straight from use case scenarios and list the interface the system must offer.
  2. Architecture-level. Services, layers or components talk to each other.
  3. Detailed design. Objects and method calls inside one component.

A system sequence diagram

sequenceDiagram
    actor C as Cashier
    participant POS as Point-of-Sale System
    C->>POS: startSale()
    loop while items remain
        C->>POS: scanItem(barcode)
        POS-->>C: itemDescription, runningTotal
    end
    C->>POS: finishSale()
    POS-->>C: total
    C->>POS: pay(cash)
    POS-->>C: change, receipt

The point-of-sale system is a black box. The messages (startSale, scanItem...) become the system's public operations.

The same scenario, one level down

sequenceDiagram
    participant UI as CashierUI
    participant SC as SaleController
    participant SL as Sale
    participant CAT as Catalog
    UI->>SC: scanItem(barcode)
    SC->>CAT: find(barcode)
    CAT-->>SC: product
    SC->>SL: addLine(product)
    SL-->>SC: total
    SC-->>UI: description, total

Inside the black box: a controller coordinates catalog lookup and the Sale object.

Workflow: from scenario to design

  1. Pick a use case scenario (the main success path).
  2. Draw the system sequence diagram. List the operations.
  3. For each operation, draw a detailed sequence diagram. Decide which object receives the message first (a controller) and who does the work.
  4. Each arrow becomes an operation in a class. Update the class diagram.
  5. Review: is any lifeline doing too much? Is any message passing data that should be hidden?

Checking a trace in code

Python A sequence diagram as an executable expectation

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Python Group messages by level to spot mixed abstraction

Press Run (or Ctrl + Enter) to see the result here.

Program input ยท what you'd type while the program runs

Type the answers before you press Run, one per line. Each time the program reads a line (with input()), it gets the next line from this box.

Good habits

  • Name participants by role (OrderService), not by variable (o1).
  • Return only what matters. Omit the return arrow when it is obvious, but show values the next step needs.
  • Keep it under a page. If it does not fit, split with ref.
  • Show the trigger. Begin with the actor or event.
  • Keep lifelines in a stable order: user on the left, deepest dependency (database, external service) on the right.
  • Do not model a loop with a hundred arrows. Use loop once.

Sequence diagrams are less good at showing complex control flow and many objects at once. For those, use activity diagrams or communication diagrams (next lesson).

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What is a system sequence diagram?

    SSDs define the system's operations from the outside, derived from use case scenarios.
  2. 2. What does a ref box (interaction use) do?

    ref keeps diagrams small by referring to another interaction.
  3. 3. A message whose receiver is not shown is called:

    A lost message has a sender but no modeled receiver; a found message has a receiver but unknown sender.
  4. 4. What is a state invariant on a lifeline?

    Written in curly braces, such as {order.status = PAID}.

Summary

  • Found and lost messages show where the model's scope ends; gates connect sub-diagrams.
  • ref reuses another interaction; state invariants and time constraints add precision.
  • Use three levels (system, architecture, detailed) and never mix them in one diagram.
  • Workflow: scenario, system sequence diagram, detailed diagrams, operations in the class diagram.
  • Messages can be checked against real call traces in tests.

Next: Communication Diagrams