When designing APIs and microservices, flowcharts aren't always enough. You need to show time, order, and state. This is where sequence diagrams shine. Let's dive into how to construct robust sequence diagrams using Mermaid.
Sequence diagrams rely on entities communicating with each other. Mermaid allows you to define these explicitly. An actor typically represents a human, while a participant represents a system or service.
sequenceDiagram actor User participant API participant DB
You define communication using arrows. A solid line ->> indicates a request, while a dotted line -->> indicates a response.
sequenceDiagram User->>API: GET /users/123 API-->>User: 200 OK (User Data)
To show that a service is actively processing a request, you can use the activate and deactivate keywords, or the shorthand + and -.
sequenceDiagram User->>+API: Process Payment API->>+Bank: Charge Card Bank-->>-API: Success API-->>-User: Receipt
You can add explanatory notes over participants using the Note keyword. For repetitive tasks or retries, Mermaid supports loop blocks.
Sequence diagrams are incredibly powerful for discovering race conditions and architectural bottlenecks before writing a single line of backend code. Try pasting these snippets into the Mermaid Preview editor to see them in action!