Skip to content
Tutorials Center
My learning

Topics

Lesson 15 of 39 ยท Sequence and Communication Diagrams

Sequence Diagram Basics

Learn to read and draw UML sequence diagrams: participants, lifelines, synchronous and asynchronous messages, return messages, activation bars, self-calls and creation.

A sequence diagram shows how objects collaborate in one scenario, in time order. Participants sit side by side along the top; time flows downward; arrows between them are messages. It is the most popular UML interaction diagram because it reads like a script: who says what to whom, and in what order.

The building blocks

Element Drawn as Meaning
Participant (lifeline head) rectangle name : Class (or actor figure) An object or role taking part
Lifeline dashed vertical line The participant's existence over time
Message horizontal arrow A call, signal or reply
Activation bar thin rectangle on the lifeline The participant is executing
Return message dashed arrow back The result, if you want it shown

Time runs from top to bottom; the horizontal position carries no meaning. Only vertical order matters.

sequenceDiagram
    actor User
    participant UI as ShopUI
    participant S as OrderService
    participant DB as Database
    User->>UI: click "Buy"
    activate UI
    UI->>S: placeOrder(cart)
    activate S
    S->>DB: save(order)
    activate DB
    DB-->>S: orderId
    deactivate DB
    S-->>UI: confirmation
    deactivate S
    UI-->>User: show receipt
    deactivate UI

A user buys something: each arrow is a message in time order, activation bars show who is busy.

Message types

The arrowhead tells the kind of message.

Arrow Kind Meaning
solid line, filled head (->> in Mermaid) Synchronous call The sender waits for the receiver to finish
solid line, open head (-) in Mermaid) Asynchronous message The sender continues immediately
dashed line, open head (-->>) Return / reply The result goes back to the caller

Name messages after the operation invoked, with arguments if they matter: save(order). Reply labels show the return value: orderId.

An asynchronous message is the right choice for events, queues and fire-and-forget calls such as sending an email.

sequenceDiagram
    participant S as OrderService
    participant Q as MessageQueue
    participant M as MailWorker
    S->>Q: publish(OrderPlaced)
    Note over S: continues without waiting
    Q-)M: deliver(OrderPlaced)
    M->>M: sendConfirmationEmail()

The service hands the event to a queue and moves on; the worker later handles it.

Self-calls, creation and destruction

  • A self-call is an arrow that loops back to the same lifeline: the object calls its own operation (M->>M: sendConfirmationEmail() above).
  • Creation: a message that creates a participant ends at the participant's head, which is drawn lower, at the moment of creation. Label it <<create>> or new.
  • Destruction: an X at the end of a lifeline shows that the object is destroyed.
sequenceDiagram
    participant C as Controller
    participant F as ReportFactory
    C->>F: build(data)
    create participant R as Report
    F->>R: new Report(data)
    R-->>F: report
    F-->>C: report
    destroy R
    C-xR: dispose()

Report exists only between its creation and its destruction.

Reading a sequence diagram

  1. Start at the first message, top left.
  2. Follow messages downward; for each one say it aloud: "UI asks OrderService to place the order".
  3. Note activations: where is control at each moment?
  4. Look for repeated or missing replies: every synchronous call should end with a return.
  5. Check that every message is an operation the receiver's class really has. This is how sequence diagrams drive class design.

Sequence diagrams and class diagrams

Each message becomes an operation on the receiver's class. If OrderService receives placeOrder(cart), the class diagram needs placeOrder in OrderService. This is the main way sequence diagrams feed design: you discover operations by drawing scenarios.

Python Derive operations from a list of messages

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.

Turning a sequence into a trace

The vertical order of arrows is exactly an execution trace. You can log it from running code and compare it to the diagram, which is a cheap way to test that code matches the design.

Python Record a call trace as sequence-diagram text

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.

The output is valid Mermaid text that you can paste into a renderer. Tools that "reverse-engineer" sequence diagrams from running programs work in a similar way.

Sequence diagram in short

Participants Across the top.
Time Flows downward.
Messages Calls, signals, replies.
Activation Who is busy now.

Horizontal position means nothing; order means everything.

1 / 2

Common mistakes

  • Trying to show every scenario. One diagram is one scenario. Use alternatives and loops (next lesson) sparingly and draw separate diagrams for big variations.
  • Too many participants. More than six or seven becomes a wide, unreadable picture.
  • Showing getters and trivial calls. Show the messages that carry the design.
  • No clear trigger. Start with an actor or an event, so readers know what begins the scenario.
  • Mixing abstraction levels. "User -> Database" skips the layers in between; keep to one level.

Start with a use case scenario from your specification, and draw one sequence diagram for the main success path. Add the alternative flows as separate diagrams.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. What does the vertical axis of a sequence diagram represent?

    Messages are ordered top to bottom in time.
  2. 2. Which arrow denotes a synchronous call?

    Solid line, filled head: the sender waits. A dashed line is a return.
  3. 3. What does an asynchronous message imply?

    Asynchronous messages are fire-and-forget; common with queues and events.
  4. 4. How do sequence diagrams help class design?

    By drawing scenarios you discover which operations each class must provide.

Summary

  • Sequence diagrams show one scenario as ordered messages between participants.
  • Time flows down; horizontal position is irrelevant.
  • Solid filled arrows are synchronous calls, open ones asynchronous, dashed ones replies.
  • Activation bars show execution; self-calls, creation and destruction add object life detail.
  • Messages become operations of the receiver's class; keep diagrams focused on one scenario.

Next: Alternatives, Loops and Combined Fragments