Low-Level Design Tutorial 0/42 lessons ~6 min read Lesson 18

    Sequence Diagrams

    Sequence diagrams show object interactions over time — the perfect follow-up after a class diagram when the interviewer says 'walk me through parking a car.' Vertical lifelines…

    Course progress0%
    Focus
    10 guided sections
    Practice signal
    Examples included
    Career prep
    Interview Q&A included

    Introduction

    Sequence diagrams show object interactions over time — the perfect follow-up after a class diagram when the interviewer says 'walk me through parking a car.'

    Vertical lifelines represent objects; horizontal arrows are messages. Order matters: validate, allocate, persist, respond. Sequence diagrams expose missing classes (who issues the ticket?) faster than staring at boxes.

    Practice one happy path and one failure path per LLD problem.

    Understanding the topic

    Key concepts

    • Lifeline: object instance over time.
    • Synchronous call: solid arrow; return: dashed.
    • Activation bar: object actively processing.
    • alt/opt frames for conditionals and loops.
    • Self-call for internal private method.
    • Actor stick figure for external user or system.
    text
    sequenceDiagram
    Client->>ParkingLot: park(vehicle)
    ParkingLot->>SpotFinder: findAvailable(type)
    SpotFinder-->>ParkingLot: spot
    ParkingLot->>Ticket: issue()

    Step-by-step explanation

    1. Place actors and objects left-to-right by initiation order.
    2. Draw messages top-to-bottom chronologically.
    3. Show return values when they drive next step.
    4. Use alt block for insufficient balance / full lot.
    5. Number messages if explaining complex flow.
    6. Stop when reaching stable end state.

    Syntax reference

    Sequence diagram text notation:

    • Solid arrow = call; dashed = return.
    • Add alt/else for failure branches.
    text
    actor User
    participant Controller
    participant Service
    participant Repository
    User -> Controller: withdraw(amount)
    Controller -> Service: withdraw(account, amount)
    Service -> Repository: save(txn)
    Repository --> Service: ok
    Service --> Controller: Result
    Controller --> User: cash + receipt

    Informative example

    ATM withdraw flow matching sequence messages:

    java
    public final class AtmController {
    private final CardReader cards;
    private final AtmService service;
    public AtmController(CardReader cards, AtmService service) {
    this.cards = cards;
    this.service = service;
    }
    public Result handleWithdraw(String cardToken, long cents) {
    Account account = cards.authenticate(cardToken);
    return service.withdraw(account, Money.ofCents(cents));
    }
    }

    Sequence: User -> AtmController -> CardReader -> AtmService -> CashDispenser — each arrow is one message.

    Real-world use

    Real-world applications

    • Explaining checkout, transfer, and booking flows.
    • Finding missing collaborator during design.
    • Documenting API orchestration in RFCs.

    Best practices

    • One diagram per primary use case.
    • Name messages after methods you will implement.
    • Show failure rollback path for transactional flows.
    • Do not include every private helper call.
    • Reference sequence numbers when coding aloud.

    Common mistakes

    • Random object order — hard to follow.
    • Skipping return messages that carry data.
    • Modeling every loop iteration instead of one representative.
    • Objects that never receive messages (remove from diagram).

    Advanced interview questions

    Q1BeginnerWhat is a sequence diagram?
    Shows objects and message order over time for a scenario.
    Q2BeginnerWhen do you draw one in LLD?
    After class diagram, to validate a core use case flow.
    Q3IntermediateHow show conditional logic?
    alt/else frames with guards [balance ok] / [insufficient].
    Q4IntermediateSequence vs activity diagram?
    Sequence emphasizes object collaboration; activity emphasizes workflow steps and branching.
    Q5AdvancedModel concurrent booking collision.
    Two users -> BookingService -> SeatLock; second gets alt failure branch when lock fails.

    Summary

    Sequence diagrams show temporal object interactions. Draw after class diagram for core flows. Use alt frames for success and failure paths. Message names should match Java methods. Keep diagrams readable — omit trivial internals.

    Ready to mark this lesson complete?Track your journey across the entire course.