Skip to content
MermaidViewer

Software diagram

Mermaid Sequence Diagram

Show how services, users and systems exchange messages over time.

Basic sequence diagram example

Basic sequence diagram example diagram
mermaid
sequenceDiagram
    autonumber
    actor U as User
    participant W as Web App
    participant A as Auth API
    participant D as Database
    U->>W: Submit login form
    W->>A: POST /login
    A->>D: Find user by email
    D-->>A: User record
    alt password valid
        A-->>W: 200 + session token
        W-->>U: Redirect to dashboard
    else invalid
        A-->>W: 401 Unauthorized
        W-->>U: Show error
    end
Open in the live editor

Sequence diagrams show the order in which participants exchange messages. They are the go-to diagram for API calls, authentication handshakes, microservice interactions and any flow where timing matters.

Mermaid sequence diagrams read almost like a script: each line is one message from one participant to another. Mermaid handles lifelines, arrow alignment and spacing, and you get blocks for loops, alternatives, optional steps and parallel work.

When to use a sequence diagram

  • Documenting REST and GraphQL API request/response flows
  • Explaining OAuth, SSO and other authentication handshakes
  • Designing event-driven and microservice interactions
  • Debugging race conditions by laying out message order

Sequence Diagram syntax reference

SyntaxWhat it does
sequenceDiagramStart a sequence diagram.
participant A as AliceDeclare a participant with an alias (controls order).
actor UDeclare a participant drawn as a stick figure.
A->>B: textSolid line with arrowhead (synchronous call).
A-->>B: textDotted line with arrowhead (response).
A-)B: textAsync message (open arrow).
A-xB: textMessage with a cross at the end (failure / lost).
activate A / deactivate A or A->>+B / B-->>-AShow an activation bar on the lifeline.
Note right of A: text / Note over A,B: textAdd notes beside or spanning participants.
loop / alt ... else / opt / par ... and / critical / breakControl-flow blocks, each closed with end.
rect rgb(240,248,255) ... endHighlight a region with a background color.
autonumberNumber every message automatically.

Sequence Diagram examples

Retry loop with activations

Activation bars (+ and -) show when a participant is busy; loop shows retries.

Retry loop with activations diagram
mermaid
sequenceDiagram
    participant C as Client
    participant S as Service
    participant Q as Queue
    C->>+S: Create job
    S->>Q: Enqueue
    loop Every 5s until done
        C->>S: GET /status
        S-->>C: pending
    end
    S-->>-C: completed
Open in the live editor

Parallel calls

par blocks show work happening at the same time.

Parallel calls diagram
mermaid
sequenceDiagram
    participant G as API Gateway
    participant U as Users
    participant O as Orders
    participant R as Recommendations
    G->>U: Get profile
    par fetch in parallel
        G->>O: Recent orders
    and
        G->>R: Suggestions
    end
    O-->>G: orders
    R-->>G: items
    Note over G: Merge and respond
Open in the live editor

Tips and best practices

  • Declare participants up front to control their left-to-right order instead of relying on first mention.
  • Use solid arrows (->>) for requests and dotted arrows (-->>) for responses to make the flow scannable.
  • Turn on autonumber for diagrams that will be discussed in reviews so people can reference steps.
  • Keep messages short; push details into notes so the diagram stays readable.

Generate a sequence diagram with AI

Not sure where to start? Describe what you need and the AI sequence diagram generator writes the Mermaid code for you. Try prompts like:

  • “OAuth 2.0 authorization code flow with PKCE”
  • “Checkout with payment provider webhook confirmation”
  • “Chat message delivery with read receipts”

Sequence Diagram templates

Sequence Diagram FAQ

What arrow types does a Mermaid sequence diagram support?

Common ones are ->> (solid with arrowhead), -->> (dotted with arrowhead), -> and --> (no arrowhead), -x and --x (cross), and -) / --) for asynchronous messages.

How do I show if/else logic in a sequence diagram?

Use an alt block: alt condition, the messages, else other condition, more messages, and close with end. For a single optional branch use opt ... end.

Can participants be grouped?

Yes. Wrap participant declarations in box Title ... end (optionally with a color, e.g. box Aqua Backend) to draw a labelled box around them.

How do I show an actor as a person?

Declare it with actor instead of participant, for example actor U as User.