UML Sequence Diagrams
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.
- Pick one scenario
Choose a concrete action such as tapping a hotel keycard.
- List participants
Name only the parts needed for that scenario.
- Draw the happy path
Show the calls and responses when everything succeeds.
- Add failure branches
Use alt for error paths and loop for repeated calls with a limit.
- Check operations
Every call should name a method that its receiving type provides.
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
endReader 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.
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.
The diagram implies Validate
A reader asks the lock to process a tap. The lock asks KeyValidator whether the card has expired.
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.
package main
import (
"fmt"
"time"
)
type KeyCard struct{ ValidUntil time.Time }
type KeyValidator struct{}
func (KeyValidator) Validate(card KeyCard, now time.Time) error {
if !now.Before(card.ValidUntil) {
return fmt.Errorf("expired card")
}
return nil
}
func main() {
now := time.Now()
validator := KeyValidator{}
fmt.Println(validator.Validate(KeyCard{ValidUntil: now.Add(time.Hour)}, now))
fmt.Println(validator.Validate(KeyCard{ValidUntil: now}, now))
}
The insight
Each call arrow becomes a method signature.
Bound the insurance retries
A refill service retries a temporarily unavailable insurance check up to three times, then reports approval or failure.
Patient requests a refill; RefillService asks Insurance for coverage up to three times after temporary errors. Approval accepts the refill; denial or exhausted retries makes it unavailable.
Stop after approval or denial. Retry only temporary failures, up to three attempts. A denied claim is not a network failure.
The insight
A loop shows which calls repeat and when they stop.
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
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.