Mermaid sequence diagrams

Show who sends what to whom, and in what order. This guide covers the sequence diagram syntax, with examples you can load into the converter and download as a PDF.

Markdown
Preview

Your first sequence diagram

A sequence diagram puts each person or system in its own column and draws messages as arrows between the columns, from top to bottom in the order they happen. It's the clearest way to explain a conversation between parts of a system: what a browser asks a server, how a payment is approved, or who hands work to whom in a process.

Start the block with sequenceDiagram. Each line after it is a message: the sender, an arrow, the receiver, a colon and the text of the message.

```mermaid
sequenceDiagram
  Alice->>Bob: Are we still on for lunch?
  Bob-->>Alice: Yes, 12:30 at the usual place
```

Mermaid creates a column for each name the first time it appears, and draws the name in a box at the top and bottom of the column's line, which is called a lifeline.

Participants and actors

Columns appear in the order their names are first used. To choose the order yourself, or to give a column a longer label, list the participants first. participant S as Server creates a column labeled Server that the rest of the diagram refers to as S, which keeps the message lines short.

Use actor instead of participant for a person. It draws a stick figure in place of the box, so readers can tell people from systems at a glance.

```mermaid
sequenceDiagram
  actor U as User
  participant B as Browser
  participant S as Server
  participant D as Database
  U->>B: Type a search
  B->>S: GET /search?q=lamps
  S->>D: Find matching products
  D-->>S: 24 products
  S-->>B: Results page
  B-->>U: Show the results
```

Arrows

The arrow's style tells the reader what kind of message it is. A common convention is a solid line for a request and a dashed line for its reply.

You typeYou getOften used for
A->>BSolid line, arrowheadA request or call
A-->>BDashed line, arrowheadA reply
A->BSolid line, no arrowheadA plain connection
A-->BDashed line, no arrowheadA loose connection
A-)BSolid line, open arrowheadA message sent without waiting
A-xBSolid line ending in a crossA failed or lost message

A participant can also send a message to itself, as in S->>S: Check the cache, which draws a short loop back to its own lifeline. That's a good way to show work that happens inside one system without involving the others.

Activations

An activation is a narrow bar on a lifeline showing that a participant is busy handling something. Add a + after the arrow to start one on the receiver, and a - on the reply to end it. Activations can nest, which shows one call waiting on another.

```mermaid
sequenceDiagram
  participant App
  participant API
  participant Auth as Auth service
  App->>+API: Load my profile
  API->>+Auth: Is this token valid?
  Auth-->>-API: Yes, user 1042
  API-->>-App: Profile details
```

The longer form, activate API and deactivate API on lines of their own, does the same thing. It's useful when the bar should start or stop somewhere other than on a message. Ending an activation that was never started is an error, so the + and - signs need to pair up.

Notes

A note adds a comment beside or across the lifelines without being a message. Place it with left of, right of or over; a note over two participants, separated by a comma, stretches across both.

```mermaid
sequenceDiagram
  participant P as Phone
  participant W as Watch
  Note over P,W: Paired over Bluetooth
  P->>W: New message
  Note right of W: Vibrates once
  W-->>P: Read
```

To break a note or a message onto a second line, put <br> where the line break should go.

Loops, options and alternatives

Real conversations have repeats and branches. Mermaid draws these as labeled boxes around the messages they apply to. Each box starts with a keyword and a label and finishes with end.

KeywordMeans
loopThese messages repeat
alt … elseOne of these branches happens, depending on a condition
optThese messages happen only sometimes
par … andThese branches happen at the same time
critical … optionThis must happen, with ways of handling failure
breakThe sequence stops here if this happens
```mermaid
sequenceDiagram
  actor C as Customer
  participant S as Shop
  participant W as Warehouse
  C->>S: Place order
  alt In stock
    S->>W: Ship the order
    W-->>C: Parcel delivered
  else Out of stock
    S-->>C: Offer a refund or a wait
  end
  opt Customer left a phone number
    S-)C: Text a delivery update
  end
  loop Every morning until delivered
    S->>W: Ask for tracking status
  end
```

Boxes can sit inside one another, such as a loop inside one branch of an alt. Indenting the lines inside each box isn't required, but it makes it much easier to see which end closes which box.

Numbering and grouping

A line reading autonumber numbers every message in order, which helps when the text around the diagram refers to individual steps (“in step 3, the server…”). To group related participants, wrap their declarations in box with a label; the box can also take a color.

```mermaid
sequenceDiagram
  autonumber
  actor U as User
  box Our system
    participant W as Web app
    participant Q as Job queue
  end
  participant E as Email provider
  U->>W: Export my data
  W->>Q: Queue the export
  W-->>U: We'll email you a link
  Q->>E: Send the download link
  E-->>U: Email with the link
```

Text that breaks a diagram

When a sequence diagram has a mistake, the preview shows its code with the error above it, including the line Mermaid stopped at. These are the usual causes:

Sequence diagrams in a PDF

md2pdf.me draws each diagram as a vector graphic, so the arrows and text stay sharp however far you zoom into the PDF, and a diagram is never split across two pages. Mermaid gives each participant a column about two inches wide, and a diagram wider than the page's text is shrunk to fit it. Three participants fit on a Letter or A4 page at about full size; with five, the text is drawn at a little over half size, which is still readable but small. Keep messages short too, since a long one widens the gap between its two columns.

Height works the same way: a diagram taller than most of a page is shrunk to fit on one. If a conversation has dozens of messages, split it into stages, each with its own heading and diagram. Readers can follow three short diagrams more easily than one that fills a page.

A diagram's colors come from the style you choose. Classic, Academic and Résumé draw it in shades of grey for printing, Modern and Report in their blue accent, Worksheet in its own blue and purple, and GitHub in the same colors as github.com. The themed styles use their own colors: oxblood for Gothic, rust and navy for Sandwake, forest green for Elvish, teal and magenta for Cyberpunk, black and oxblood on newsprint grey for Newspaper, and ink and vermilion for Fiendish.

Other diagram types

Back to the Mermaid cheat sheet