Sequence Diagrams: Modelling Interactions Over Time

A sequence diagram shows how a group of objects cooperate to carry out one scenario, such as checking out a shopping cart, by drawing the messages they exchange in the order they happen. It is the most widely used of the UML interaction diagrams. You reach for it when you turn a use case into a design: a class diagram says which objects exist, while a sequence diagram says who calls whom, in what order, and who has to wait.

Lifelines and activations

Each participant appears as a box across the top, labelled with an optional object name and its class, such as cart : Cart, or : Browser for an anonymous instance. Beneath the box hangs a lifeline, a dashed vertical line that represents that participant for the duration of the interaction. Time runs downward, so a message drawn lower happens later; horizontal spacing carries no meaning.


A thin rectangle on a lifeline is an execution specification, often called an activation bar. It starts when the participant receives a message and ends when that work finishes. A participant created during the scenario has its head box drawn level with the create message, and a large X ends the lifeline of a participant that is destroyed.


A sequence diagram with lifelines for a browser, a cart and a payment gateway exchanging checkout, charge, approved, notifyAudit and receipt messages.


Messages

A message is a horizontal arrow from the sender's lifeline to the receiver's, labelled with the operation and its arguments, such as charge(total). A reply is labelled with the value it returns. The arrow style tells you whether the sender waits.


Message

Notation

Meaning

Synchronous call

Solid line, filled arrowhead

Sender waits until the receiver finishes and replies

Asynchronous signal

Solid line, open arrowhead

Sender continues at once; no reply follows

Reply

Dashed line, open arrowhead

Returns control, and often a value, to the caller

Create

Dashed line, open arrowhead ending at a new head box

Brings a new participant into existence


Synchronous calls nest like a call stack. In the checkout figure the cart calls the gateway while it is still handling checkout(), so the gateway's approved reply arrives before the cart sends receipt back to the browser: replies return in the reverse order of the calls that caused them.



Combined fragments

Real scenarios branch and repeat. A combined fragment is a rectangle drawn across the lifelines involved, with an interaction operator in a small pentagon at its top-left corner and one or more operands. Each operand may carry a guard, a condition in square brackets such as [stock > 0], that must be true for that operand to run.


Operator

Operands

What runs

alt

Two or more, separated by dashed lines

The one operand whose guard is true, or the [else] operand

opt

One

The operand once if its guard is true, otherwise nothing

loop

One

The operand repeatedly, while its guard holds or for loop(min, max) iterations

par

Two or more

All operands, with their messages interleaved in any order

break

One

The operand, after which the rest of the enclosing fragment is skipped


In an alt, write guards that do not overlap; if two guards can be true at once, the diagram no longer says which operand runs.


Counting the messages in one run turns a diagram into a number you can compare between designs, for example to estimate how many network round trips a design needs. Add the messages outside every fragment to the messages inside each operand, weighted by how often that operand runs:


N=N0+∑krk mkN = N_0 + \sum_k r_k\, m_k


Here N0N_0 is the number of messages outside every fragment, mkm_k is the number of messages directly inside operand kk, and rkr_k is how many times operand kk executes in the run: the iteration count for a loop, 0 or 1 for an opt, and the number of times its guard is selected for an alt operand. For a fragment nested inside a loop, rkr_k counts its executions across all iterations.


Example: A loop over 3 order lines contains one synchronous call and its reply, and outside the loop there is one call and its reply. The run sends 2+3×2=82 + 3 \times 2 = 8 messages.



What to remember

  • Time runs down each lifeline; horizontal position has no meaning.
  • A filled arrowhead is a synchronous call and the sender waits; an open arrowhead on a solid line is an asynchronous signal; a dashed line is a reply.
  • Replies to nested synchronous calls unwind in reverse order.
  • alt chooses one operand, opt runs its operand zero or one time, loop repeats, par interleaves and break exits.
  • The messages in one run total N=N0+∑krkmkN = N_0 + \sum_k r_k m_k.

Example: Counting messages in a library loan

A self-service kiosk lets a member borrow several books at once. The sequence diagram below models one request. The kiosk sends the synchronous call borrow(ids) to the library. Inside a loop over the requested ids, the library calls find(id) on the catalogue and receives the book as a reply. An alt fragment then branches: if the book is available, the library calls reserve(id) on the catalogue and receives ok; otherwise it sends the asynchronous signal notifyUnavailable(id) to the kiosk. After the loop the library replies to the kiosk with the loan.


A sequence diagram in which a kiosk asks a library to borrow books, with a loop over book ids that contains an alt fragment for available and unavailable books.


a) A member requests 4 books and all of them are available. How many messages, counting replies, appear in this run?


Count the messages outside every fragment first. Only borrow(ids) and the final loan reply sit outside the loop, so the outside count is ________.


Every iteration with an available book runs the first alt operand: find(id), the book reply, reserve(id) and the ok reply, which is ________ per iteration. The [else] operand never runs, and the loop executes 4 times.


N=N0+r m=2+4×4=18N = N_0 + r\,m = 2 + 4 \times 4 = 18


The run contains 18 messages.


b) A member requests 5 books, and 2 of them are unavailable. How many messages does the run contain, and how many of those messages does the catalogue receive?


An unavailable iteration still asks the catalogue first, so it runs find(id), the book reply and notifyUnavailable(id), which is ________. The signal is asynchronous, so no reply follows it.


The 3 available iterations contribute ________ and the 2 unavailable iterations contribute ________. Adding the 2 messages outside the loop:


N=2+3×4+2×3=20N = 2 + 3 \times 4 + 2 \times 3 = 20


The catalogue receives find(id) in all 5 iterations and reserve(id) in the 3 available ones. The book and ok replies travel away from the catalogue, so they do not count as received by it.


Ncat=5+3=8N_{\text{cat}} = 5 + 3 = 8


The run contains 20 messages, and the catalogue receives 8 of them.


c) To cut network traffic, the team replaces the per-book find(id) with one synchronous call findAll(ids), made before the loop, that returns every requested book at once. The loop and its alt fragment keep reserve(id), ok and notifyUnavailable(id). For the request in part b), how many messages does the new design send, and how many synchronous round trips to the catalogue remain?


Outside the loop there are now four messages: borrow(ids), findAll(ids), its reply and the final loan reply. Inside the loop, an available book needs only reserve(id) and ok, which is ________, and an unavailable book needs only the single asynchronous notifyUnavailable(id).


N=4+3×2+2×1=12N = 4 + 3 \times 2 + 2 \times 1 = 12


The synchronous calls to the catalogue are findAll(ids) once plus reserve(id) for each of the 3 available books. In the original design they were find(id) five times plus reserve(id) three times, which is 8.


Rnew=1+3=4R_{\text{new}} = 1 + 3 = 4


The new design sends 12 messages instead of 20 and makes 4 synchronous round trips to the catalogue instead of 8. Batching trades many small messages for one larger reply, and the sequence diagram shows that trade before any code is written.

Match each element of a UML 2 sequence diagram with the notation or meaning that identifies it, so that every element is paired with exactly one description.

Match each term
A.

Large X that ends a participant's dashed line

B.

Solid arrow with an open arrowhead; the sender carries on without waiting

C.

Dashed vertical line below a participant's head box, representing that participant over the interaction

D.

Thin rectangle on a participant's dashed line marking the period when it is carrying out behaviour

E.

Dashed arrow with an open arrowhead that returns control or a value to the caller

F.

Solid arrow with a filled arrowhead; the sender waits until the receiver finishes

Execution specification

Reply

Synchronous call

Destruction occurrence

Asynchronous signal

Lifeline

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

A booking sequence diagram must reuse an existing interaction named ReserveSeats. Its formal parameters are flightId and seatCount, in that order. The current booking requests three seats on flight F482.


Complete the notation for an interaction use, which references an existing interaction. Enter the standard lower-case frame keyword and an integer argument.


The frame's identifying keyword is , and its content names the interaction as ReserveSeats("F482", ).

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

In a checkout sequence diagram, after a payment succeeds, OrderService sends sendSms(receipt) to SmsGateway only when the customer has opted in to text receipts; when the customer has not opted in, no extra message is sent. In either case OrderService then sends recordSale(order) to Ledger. Which notation best models the sendSms(receipt) message?

Choose an answer

Create a free account to get started

Unlock full course notes by signing up

  • Video lessons taught by top educators
  • Practice questions with guided solutions
  • Written review notes for reference
  • Expert answers anytime from course tutors

Extra Practice