Microservice architectures trade the complexity of a monolith for a different kind of complexity: the interactions between services. When a single user action triggers a chain of HTTP calls, message queue events, and database writes across five or more services, understanding the flow becomes a serious challenge. Sequence diagrams solve this problem by showing exactly which service talks to which, in what order, and what happens when things go wrong.
In this tutorial you will learn how to build sequence diagrams for microservice systems using Mermaid.js, starting from the basics and working up to a complete real-world API gateway example.
Why Sequence Diagrams for Microservices
Sequence diagrams show interactions over time. The vertical axis represents time progressing downward, and each participant (service, database, queue, or external system) gets its own vertical lifeline. Arrows between lifelines represent messages, requests, or events.
This format is uniquely suited to microservices because it answers the questions that matter most during design reviews and incident investigations:
- Which service initiates the interaction?
- What is the exact order of calls?
- Which calls are synchronous (blocking) and which are asynchronous (fire-and-forget)?
- Where are the potential failure points?
- What happens when a downstream service is unavailable?
Defining Participants
In Mermaid, participants are the entities in your sequence diagram. Declaring them explicitly at the top of your diagram lets you control their order from left to right, which affects readability. Place the initiator on the far left and arrange downstream services in the order they are typically called.
MermaidNotice the use of the as keyword to give participants human-friendly display names while keeping the IDs short and clean in the rest of the diagram.
Synchronous vs Asynchronous Messages
The distinction between synchronous and asynchronous communication is critical in microservice diagrams. Mermaid uses different arrow types to express this clearly.
->>— Solid arrow: synchronous request (caller waits for a response)-->>— Dashed arrow: response to a synchronous request-)— Open arrow: asynchronous message (fire-and-forget)
Getting these arrows right tells the reader whether a caller is blocked waiting or whether it can continue processing.
MermaidIn this example, the order service waits for payment confirmation (synchronous), but then publishes an event to the message queue without waiting for inventory or email to finish processing (asynchronous).
Activation Boxes
Activation boxes show when a participant is actively processing a request. They appear as thin rectangles on the participant's lifeline. In Mermaid, you can toggle activation with activate and deactivate, or use the shorthand + and - on arrows.
The + after the arrow activates the target participant, and the - deactivates it when the response is sent back. This makes it visually clear how long each service is "busy" handling the request.
Loops, Alternatives, and Optional Blocks
Real-world microservice interactions are rarely straight lines. Mermaid supports control flow blocks that model common patterns.
Loop Block
Use loop to show repeated interactions, such as polling or retry logic.
Alt/Else Block
Use alt and else to show conditional branching, such as different behavior based on a cache hit versus a cache miss.
Opt Block
Use opt for optional interactions that may or may not occur.
Tip: Keep control flow blocks shallow. If you find yourself nesting more than two levels deep, consider splitting the diagram into separate sequences for each major path.
Real-World Example: E-Commerce Checkout
Let us put everything together in a complete example that models an e-commerce checkout flow through an API gateway architecture. This diagram covers authentication, payment processing, inventory management, and notification, which are the core services involved in a typical checkout.
MermaidThis single diagram captures the happy path, the out-of-stock path, the synchronous request/response calls, and the asynchronous event-driven notifications. During an incident review or a design session, a diagram like this gives the entire team a shared reference point.
Best Practices for Microservice Sequence Diagrams
- Order participants left-to-right by call sequence. The initiating service belongs on the far left, and the most downstream dependency goes on the far right.
- Label every message with the operation and key data. Instead of a bare arrow, write "POST /checkout" or "Charge $149.99" so the reader knows exactly what is happening.
- Show error paths explicitly. Use
altblocks to document what happens when a service returns an error. - Keep diagrams focused on one user action. A checkout diagram and a refund diagram should be separate files, not combined into one massive chart.
- Use notes for context. Mermaid supports
Note overandNote right ofsyntax to add explanatory text without cluttering the message flow. - Store diagrams alongside the code they describe. A sequence diagram for the checkout flow belongs in the checkout service repository, not in a separate wiki.
Build Your Sequence Diagram
Paste any example from this tutorial into the SimpleMermaid editor and watch it render in real time.
Open the Editor
Buy Me A Coffee