If you've ever needed to show how different systems, services, or objects communicate with each other in order, step by step a sequence diagram is one of the clearest ways to do it. And PlantUML lets you create these diagrams using plain text, no drag-and-drop tools required. Looking up plantuml sequence diagram example code is usually the fastest way to learn the syntax, see real patterns, and start diagramming your own workflows right away.
What Exactly Is a PlantUML Sequence Diagram?
A sequence diagram in PlantUML's sequence diagram syntax shows the interaction between participants (like users, servers, databases, or microservices) over time. Messages flow between participants in a top-to-bottom order. You write a few lines of text, and PlantUML renders a visual diagram from it.
Unlike activity diagrams that focus on workflow steps, sequence diagrams focus on who talks to whom and in what order. That makes them ideal for documenting APIs, request-response cycles, authentication flows, and system integrations.
Why Do Developers Use Sequence Diagrams?
When you're planning a new feature or debugging a multi-service interaction, it's easy to lose track of what calls what. A sequence diagram does three things well:
- Clarifies communication order You see exactly which message comes first, second, and so on.
- Documents system behavior New team members can understand an API flow without reading source code.
- Surfaces design issues Circular calls, missing error handling, or unnecessary steps become obvious on paper.
Using PlantUML text-based syntax means these diagrams live in your repository, get version-controlled alongside your code, and update in pull requests just like any other file.
Basic PlantUML Sequence Diagram Example Code
Here's the simplest working example. It shows a user logging into an application:
@startuml
actor User
participant "Web App" as App
participant "Auth Service" as Auth
database "Database" as DB
User -> App: Enter credentials
App -> Auth: Validate credentials
Auth -> DB: Query user
DB --> Auth: User record
Auth --> App: Token
App --> User: Login success
@enduml
Let's break down what each line does:
@startumland@endumlThese mark the beginning and end of every PlantUML diagram.actorDeclares a human participant (drawn as a stick figure).participantDeclares a system or component (drawn as a box). Theaskeyword gives it a short alias.->A solid arrow, representing a synchronous request.-->A dashed arrow, representing a response or return message.
How Do You Add Notes, Conditions, and Loops?
Real-world flows aren't always linear. You'll often need conditional logic, loops, and annotations. Here's an expanded example that includes those patterns:
@startuml
actor User
participant "API Gateway" as GW
participant "Order Service" as Order
participant "Payment Service" as Pay
database "Orders DB" as DB
User -> GW: POST /orders
GW -> Order: CreateOrder(payload)
alt Payment required
Order -> Pay: ChargeCard(orderId, amount)
Pay --> Order: PaymentConfirmed
else Free order
Order -> Order: Skip payment
end
loop Retry on failure (max 3)
Order -> DB: Save(order)
end
note right of Order: Orders are idempotent
Order --> GW: 201 Created
GW --> User: Order confirmation
@enduml
Key additions to know:
alt/else/endConditional blocks, similar to if-else logic.loop/endRepeated actions, like retry mechanisms.note right ofAdds a text annotation next to a participant.
These constructs are what make PlantUML sequence diagrams practical for real architecture discussions, not just simple request-response sketches.
How Do You Handle Async Messages and Self-Calls?
Some systems send messages without waiting for a response. PlantUML handles this with an open arrowhead:
@startuml
participant "Event Bus" as Bus
participant "Notification Service" as Notif
Bus ->> Notif: order.created (async)
Notif -> Notif: Format email
Notif ->> Bus: Send confirmation
@enduml
->>An open arrowhead indicates an asynchronous message (fire and forget).Notif -> NotifA self-call, where a participant invokes its own internal method.
What Are Common Mistakes When Writing Sequence Diagram Code?
If you're new to PlantUML, these errors come up frequently:
- Forgetting
@endumlThe diagram won't render without it. Every diagram needs both tags. - Mismatched
alt/endblocks Each conditional block must close withend. Missing one causes parse errors. - Using aliases before declaring them You must declare a participant with
asbefore referencing the alias in messages. - Overloading a single diagram If your diagram has 15+ participants or 50+ messages, break it into multiple focused diagrams. Readability drops fast with too many interactions on one canvas.
- Mixing arrow types inconsistently Use
->for synchronous calls and-->for responses. Mixing these up confuses anyone reading the diagram.
How Can You Customize the Look of Your Diagram?
PlantUML lets you control colors, grouping, and layout. A few useful directives:
@startuml
skinparam backgroundColor #FEFEFE
skinparam sequenceArrowThickness 2
skinparam roundcorner 10
box "Backend Services" #LightBlue
participant "Order Service" as Order
participant "Payment Service" as Pay
end box
group Authentication
User -> Order: Submit (with JWT)
end group
@enduml
skinparamControls visual styling like background color, arrow thickness, and rounded corners.boxGroups related participants visually with a labeled border.groupWraps a section of messages with a labeled box on the diagram timeline.
If you're also working on infrastructure diagrams, you might find it useful to combine sequence diagrams with PlantUML component diagrams for DevOps pipelines to get a fuller picture of your system.
Where Can You Render PlantUML Sequence Diagrams?
You have several options depending on your workflow:
- PlantUML Online Server Paste your code and get a rendered image instantly. Good for quick tests.
- VS Code extensions The PlantUML extension by jebbs gives you live preview as you type.
- CI/CD integration Generate diagrams automatically during builds. This pairs well with documentation-as-code workflows.
- Confluence, Notion, or Markdown Many tools support PlantUML rendering through plugins or embed codes.
Quick Reference: Essential Sequence Diagram Syntax
Keep this list handy as you write your own diagrams:
- Participants:
actor,participant,database,queue,control,entity,collections - Arrows:
->(sync request),-->(sync response),->>(async request),-->>(async response) - Blocks:
alt/else/end,loop/end,opt/end,group/end,par/end(parallel) - Annotations:
note left of,note right of,note over - Ref:
ref over A,B: Sub-process namereferences another diagram - Destruction:
destroy ParticipantNamemarks a participant as terminated
Your Next Step: Build a Diagram for Something Real
Reading example code helps, but the fastest way to learn PlantUML sequence diagrams is to model something you're actually working on. Pick one user flow from your current project a login process, a webhook handler, an API integration and write it out as a sequence diagram.
Here's a practical checklist to get your first diagram done today:
- List your participants Who or what is involved? (user, service, database, third-party API)
- Map the first happy-path message What's the very first request, and who sends it?
- Add responses with dashed arrows Every request should show what comes back.
- Layer in conditions Use
alt/elsefor error handling or branching logic. - Add a loop if retries exist Retry mechanisms and polling loops are common in real systems.
- Drop in notes Annotate anything that isn't obvious from the message flow alone.
- Render and review Paste into the PlantUML online server and share with your team.
Tip: Start small. A clean, focused diagram with 4-6 participants and 10-15 messages communicates more than a sprawling one that tries to cover every edge case. You can always create additional diagrams for exception flows or refer to them with the ref over syntax.
How to Draw an Activity Diagram Using Plantuml Syntax,
Plantuml Class Diagram Code for Microservices Architecture
Plantuml State Machine Diagram Cheatsheet Reference
Plantuml Devops Pipeline Component Diagram Code Tutorial
Best Diagram Code Editors for Software Architects in 2025
How to Create a Mermaid Diagram in Markdown Using Mermaid Syntax