Home Learn Blog Editor Buy Me A Coffee Buy Me A Coffee Start Creating

Sequence Diagram Tutorial for Microservices

By SimpleMermaid Team 9 min read

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:

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.

Mermaid
sequenceDiagram participant Client participant Gateway as API Gateway participant Auth as Auth Service participant Users as User Service participant DB as Users DB Client->>Gateway: GET /api/profile Gateway->>Auth: Validate JWT Auth-->>Gateway: Token Valid Gateway->>Users: Get User Profile Users->>DB: SELECT * FROM users DB-->>Users: User Record Users-->>Gateway: Profile JSON Gateway-->>Client: 200 OK + Profile

Notice 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.

Getting these arrows right tells the reader whether a caller is blocked waiting or whether it can continue processing.

Mermaid
sequenceDiagram participant Orders as Order Service participant Payment as Payment Service participant Queue as Message Queue participant Inventory as Inventory Service participant Email as Email Service Orders->>Payment: Process Payment (sync) Payment-->>Orders: Payment Confirmed Orders-)Queue: OrderPlaced Event (async) Queue-)Inventory: Reserve Stock Queue-)Email: Send Confirmation

In 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.

Mermaid
sequenceDiagram participant C as Client participant G as API Gateway participant S as Search Service participant ES as Elasticsearch C->>+G: Search Request G->>+S: Forward Query S->>+ES: Execute Search ES-->>-S: Search Results S-->>-G: Formatted Results G-->>-C: 200 OK + Results

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.

Mermaid
sequenceDiagram participant C as Client participant G as Gateway participant Cache as Redis Cache participant API as Product Service participant DB as Product DB C->>G: GET /products/123 alt Cache Hit G->>Cache: GET product:123 Cache-->>G: Cached Product Data G-->>C: 200 OK (from cache) else Cache Miss G->>Cache: GET product:123 Cache-->>G: null G->>API: Fetch Product API->>DB: SELECT * FROM products DB-->>API: Product Row API-->>G: Product JSON G->>Cache: SET product:123 (TTL 300s) G-->>C: 200 OK (from DB) end opt User is Authenticated G->>API: Log Product View end

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.

Mermaid
sequenceDiagram participant Client participant GW as API Gateway participant Auth as Auth Service participant Cart as Cart Service participant Inv as Inventory Service participant Pay as Payment Service participant Order as Order Service participant Queue as Event Bus participant Notify as Notification Service Client->>+GW: POST /checkout GW->>+Auth: Validate Session Auth-->>-GW: Session Valid (userId: 42) GW->>+Cart: Get Cart Items (userId: 42) Cart-->>-GW: Cart Items + Totals GW->>+Inv: Reserve Items alt Items Available Inv-->>-GW: Reservation Confirmed GW->>+Pay: Charge $149.99 Pay-->>-GW: Payment Successful GW->>+Order: Create Order Record Order-->>-GW: Order #ORD-7891 GW-->>Client: 201 Created (Order #ORD-7891) GW-)Queue: OrderCompleted Event Queue-)Notify: Send Confirmation Email Queue-)Inv: Convert Reservation to Deduction else Items Out of Stock Inv-->>GW: Insufficient Stock GW-->>Client: 409 Conflict end deactivate GW

This 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

Build Your Sequence Diagram

Paste any example from this tutorial into the SimpleMermaid editor and watch it render in real time.

Open the Editor