Visualizing Workflow Architectures with Mermaid

    A practical guide to documenting complex backend systems and state machines using Mermaid diagrams directly in your markdown files.

    Tob

    Tob

    Backend Developer

    5 min readDocumentation
    Visualizing Workflow Architectures with Mermaid

    One of the biggest challenges in backend development is communicating complex state machines and system architectures to other developers. While tools like Figma or Draw.io are great, they often become outdated the moment the code changes.

    By embedding Mermaid diagrams directly into our markdown documentation (and blog posts!), we can version-control our architecture diagrams alongside our code.

    State Machine Example

    Here's an example of how we might document a typical e-commerce order fulfillment state machine using Mermaid:

    stateDiagram-v2 [*] --> Pending Pending --> Processing : Payment Confirmed Pending --> Cancelled : Payment Failed Processing --> Shipped : Quality Check Passed Processing --> Cancelled : Out of Stock Shipped --> Delivered : Customer Received Shipped --> Returned : Delivery Failed Delivered --> [*] Returned --> [*] Cancelled --> [*]

    System Architecture

    We can also use Mermaid to visualize how different microservices communicate with each other. Here is a simplified view of a notification service architecture:

    flowchart LR Client([Client App]) --> API[API Gateway] API --> Auth{Auth Service} Auth -- Valid --> Core[Core Service] Auth -- Invalid --> Error[401 Response] Core --> Queue[(Message Queue)] Queue --> Worker1[Email Worker] Queue --> Worker2[Push Worker] Queue --> Worker3[SMS Worker] Worker1 --> Provider1((SendGrid)) Worker2 --> Provider2((FCM)) Worker3 --> Provider3((Twilio))

    Why This Matters

    1. Version Control: Your diagrams live in Git. You can see how the architecture evolved over time through commit history.
    2. Developer Experience: No context switching. Write docs in your IDE, see the diagrams render automatically.
    3. Accuracy: If you change a component name in the text, it takes seconds to update the diagram source code.
    4. Collaboration: Easy to share and review diagrams with your team.

    Mermaid brings diagramming into the "Doc-as-code" era, and integrating it with Next.js is easier than ever.

    Related Blog

    Visualizing Workflow Architectures with Mermaid | Tob