Sequence Diagram

The UML picture of one scenario over time: which objects send which messages, in what order, and what comes back. It is how you show that a class design actually works.

Overview

A sequence diagram shows one scenario as it plays out. Each participant gets a vertical lifeline; time runs down the page; each message is a horizontal arrow from the sender to the receiver. Where a class diagram shows what exists, a sequence diagram shows what happens when a user places an order or an elevator is called.

The notation is in clause 17 of UML 2.5.1. A lifeline is a head box with a vertical line under it. A synchronous call has a filled arrowhead; an asynchronous message has an open arrowhead; a reply is a dashed line. The time a participant spends busy is a thin rectangle on its lifeline, called an execution specification. Boxes called combined fragments wrap parts of the diagram to show alternatives (alt), options (opt), loops (loop) and parallel work (par).

In an LLD interview, the sequence diagram is the proof that your class diagram is more than boxes. Walking one main flow through your classes shows which object owns each step, exposes a class that is doing too much, and surfaces the failure paths you will be asked about. Draw the happy path first; add the alternatives when asked.

The parts

Eight elements make up nearly every diagram you will draw.

  • Lifeline: a head box and a vertical line. It is one participant for the whole scenario.
  • Synchronous message: a solid line with a filled arrowhead. The caller waits for the answer.
  • Asynchronous message: a solid line with an open arrowhead. The caller continues without waiting.
  • Reply: a dashed line back to the caller. It carries the return value.
  • Execution specification: a thin rectangle on the lifeline. The participant is busy.
  • Create message: a dashed line with an open arrowhead to a new head box. An object is created during the scenario.
  • Combined fragment: a labeled box around some messages: alt, opt, loop, par, break, critical and others.
  • Note: a folded-corner box, for a comment or a state.

A cash withdrawal needs only four of them: lifelines, synchronous messages, replies and execution specifications.

Interview questions

  • What are the key elements of a sequence diagram, and how do you show synchronous versus asynchronous calls?
  • Draw a sequence diagram for placing an order, including the failure path.
  • How do you show error handling and retries?
  • How do you show an object being created or destroyed during the scenario?
  • When should you draw a sequence diagram rather than an activity diagram?

Sign up free to read the full topic

More Low-Level Design topics

Start free on Cariara · Coding problems · System design studio