System Design Academy

Saving…

Lesson 10 of 10 in Module 10 of 45 lessons complete

UML Sequence Diagrams

Module 1, lesson 10Concept12 min

Definition

Sequence diagram

In plain English
A sequence diagram shows the messages that parts of a program send each other, in time order, for one scenario.
Think of it like
It is like a comic strip of a conversation read from top to bottom.

Lifeline

In plain English
A lifeline is the vertical line under a participant that shows it exists during the scenario.
Think of it like
It is like an actor's time on stage.

Why it exists

Your class diagram shows order and payment parts. It does not show what happens if payment fails or the queue rejects an order.

Walk through one action in time order. You spot missing calls and work assigned to the wrong part before writing code.

How it works

Fragment

In plain English
A fragment is a box in a sequence diagram that marks a branch with alt or a repetition with loop.
Think of it like
It is like a choose-your-own-adventure page.
  • Participants are the parts involved in this scenario.
  • Each participant has a vertical line. Read messages from top to bottom.
  • A solid arrow means a call; a dashed arrow means a return in these diagrams.
  • Use alt and else for different outcomes, loop for repeated calls, and notes for important rules.
  1. Pick one scenario

    Choose a concrete action such as tapping a hotel keycard.

  2. List participants

    Name only the parts needed for that scenario.

  3. Draw the happy path

    Show the calls and responses when everything succeeds.

  4. Add failure branches

    Use alt for error paths and loop for repeated calls with a limit.

  5. Check operations

    Every call should name a method that its receiving type provides.

Mermaid source for the keycard scenario.
Text
sequenceDiagram
 participant Reader
 participant Lock
 participant KeyValidator
 Reader->>Lock: Tap(card)
 Lock->>KeyValidator: Validate(card, now)
 KeyValidator-->>Lock: valid or error
 alt expired card
 Lock-->>Reader: Red light
 else valid card
 Lock->>Lock: Unlock()
 Lock-->>Reader: Green light
 end

Reader sends a card tap to Lock, which asks KeyValidator to check it. An expired card gets red; a valid card unlocks the lock and gets green.

An expired card returns a red light; a valid one unlocks the door.

Reader sends a card tap to Lock, which asks KeyValidator to check it. An expired card gets red; a valid card unlocks the lock and gets green.

Examples

A declined payment must stop the order

The app places an order. The service charges payment before sending a successful order to the barista queue.

App places an order; OrderService asks Payment to charge it. A decline returns an error; approval sends the order to BaristaQueue before confirming to App.

Payment failure returns an error without enqueueing the order.

App places an order; OrderService asks Payment to charge it. A decline returns an error; approval sends the order to BaristaQueue before confirming to App.

The alt branch shows what happens when payment fails. Put Enqueue on BaristaQueue because that part receives the call.

The insight

Drawing different outcomes forces you to plan for failure.

Trade-offs

Pros

  • Reveal missing methods.
  • Clarify who owns each action.
  • Make error paths visible.

Cons

  • One diagram covers one scenario.
  • Many participants make the flow hard to read.

Use when

  • Complex or failure-heavy flows.
  • Explaining a flow in an interview.

Avoid when

  • Routine create, read and update operations.
  • One direct call needs no separate diagram.

Quick check

A dashed arrow from Payment back to OrderService means…
While drawing, you find OrderService calls BaristaQueue.Enqueue(), but your class diagram has no such method. What does it tell you?

Takeaways

  • Sequence diagrams show behaviour over time.
  • Use alt and loop for failures and retries.
  • Every call arrow names a method on its receiving part.

Try the quick check above whenever you feel ready.

Previous: UML Class Diagrams