Overview
A sequence diagram shows how objects interact over time for one scenario: vertical lifelines for each participant and horizontal arrows for messages in order, including return values, conditions (alt), and loops. Where class diagrams show structure, sequence diagrams show behavior.
In interviews, sketching the sequence of the main use case (for example 'book a seat' or 'request an elevator') proves your classes actually collaborate correctly: every message must be a method on the receiver, and responsibilities that nobody handles become obvious.
Characters are listed across the top, and the script shows who says what to whom, in order. Reading it top to bottom tells the story of one scene.
When to use it
- Validating the design against the main use case.
- Explaining complex interactions (payment flows, callbacks).
- Finding missing methods or misplaced responsibilities.
Where it shows up in interviews
Recognize it when: 'walk me through what happens when...'
- Design a movie ticket system
- Design an elevator system
- Design an ATM
Where it is used in real software
Common in GitHub docs and design docs to describe API flows.
Providers like Stripe and Auth0 document flows with sequence diagrams.
Trace waterfalls are runtime sequence diagrams of real requests.
Key terms
- Lifeline
- A participant's timeline.
- Message
- A call from one participant to another.
- Activation
- Period a participant is executing.
- alt / opt / loop
- Conditional, optional, and repeated fragments.
- Async message
- Open arrowhead; sender does not wait.
How it works, step by step
- 1Pick one scenario
The main use case.
- 2List participants left to right
Actor, controller, service, entities, external systems.
- 3Draw messages in order
Each must be a method on the receiver.
- 4Add alternatives
alt for failure paths like payment declined.
- 5Check responsibilities
Is any object doing too much? Is data coming from the right owner?
STEP 1User calls book(showId, ['A5', 'A6']).
What the diagram reveals
Reviewing the booking sequence
| Observation | Design fix |
|---|---|
| Controller calls Seat.status directly | Move availability check into Show |
| No release on payment failure | Add alt branch with SeatLock.release |
| BookingService formats emails | Delegate to Notifier |
| Two users hold the same seat | SeatLock.hold must be atomic |
NOWObservation: Controller calls Seat.status directly | Design fix: Move availability check into Show
A sequence diagram is a cheap test of the design before writing code.
Implementation
sequenceDiagram actor U as User participant B as BookingService participant L as SeatLock participant P as PaymentGateway participant S as Show U->>B: book(showId, seats) B->>L: hold(showId, seats, 10m) alt seats already held L-->>B: false B-->>U: SeatUnavailable else held B->>P: charge(user, amount) alt declined B->>L: release(showId, seats) B-->>U: PaymentFailed else paid B->>S: confirm(seats, user) B-->>U: Booking end endComplexity and performance
Beyond that, split the scenario.
Main flow plus one failure.
Trade-offs
One diagram per scenario is clear; cramming every branch makes it unreadable.
Detailed diagrams go stale; keep them for key flows.
Variants and related techniques
Same messages arranged by object links rather than time.
Flowchart of steps and decisions without participants.
Common mistakes
- Messages to objects that do not have that method.
Fix: Update the class diagram or reassign responsibility.
- The controller doing all the work.
Fix: Delegate to domain objects; watch for a 'fan' of arrows from one lifeline.
Interview questions
Why draw a sequence diagram in an LLD interview?
It validates that classes collaborate correctly for the main use case, exposes missing methods and misplaced responsibilities, and communicates failure handling clearly.
How do you show error handling?
With alt fragments for alternative outcomes, showing compensations like releasing held seats when payment fails.
Practice problems
| Problem | Difficulty | What it trains |
|---|---|---|
| Sequence diagram for an elevator request | Medium | Controller and scheduling. |
| Sequence diagram for Splitwise settle up | Medium | Balances update. |