Mermaid lets developers keep diagrams close to the Markdown, code, and review process they already use. This guide explains how to create flowcharts, sequence diagrams, ERDs, and state diagrams; compare Mermaid with other diagram-as-code options; preview and render diagrams reliably; and maintain them as part of a practical docs-as-code workflow.
Overview
Markdown diagrams are most useful when they are treated as documentation source files rather than exported images. A Mermaid diagram is written as text, committed to version control, reviewed with the surrounding documentation, and rendered by a compatible editor, documentation platform, or static site pipeline.
This approach works well for README files, architecture decision records, RFCs, runbooks, API documentation, and internal wikis. It also makes diagrams easier to update when a service, database relationship, or deployment path changes. Instead of searching for the original drawing file, a contributor can edit the diagram in the same repository as the implementation.
Mermaid is a practical starting point because its syntax covers several common developer diagrams, including flowcharts, sequence diagrams, entity-relationship diagrams, class diagrams, state diagrams, and some architecture-oriented views. Its main limitation is that the final result depends on the renderer. A diagram that works in a local preview may need adjustments for a repository viewer, static site generator, or documentation platform.
For broader modeling needs, compare Mermaid with other text-based options in Mermaid vs PlantUML vs D2 vs Graphviz. For visual, drag-and-drop workflows, the comparison of Mermaid, draw.io, and Lucidchart provides a useful distinction between source-driven and canvas-driven tools.
How to compare options
Choose a diagram workflow by evaluating the documentation system around it, not only the diagram language. The following questions usually reveal whether Mermaid is a good fit.
- Where will the source live? If diagrams belong beside code, Markdown and a text-based syntax are usually convenient. If nontechnical contributors need to edit layouts, a visual editor may be more suitable.
- Who reviews changes? Git-based review is valuable when engineers need to inspect a diagram change as a text diff. It is less useful when reviewers need to manipulate shapes directly.
- Which diagram types are required? A simple service flow may need only a flowchart. A data-heavy project may need an ERD or class diagram tool, while a formal model may be better served by a specialized UML tool.
- How will diagrams render? Confirm the target platform's Mermaid support, supported syntax, theme behavior, and handling of diagrams that fail to parse. Do not assume that every Markdown renderer supports every diagram type.
- What must be controlled? Consider whether diagrams contain internal hostnames, credentials, customer data, network boundaries, or other sensitive details. Rendering may happen locally, in CI, or through a hosted service, so understand the path before publishing restricted documentation.
- How important is layout control? Text-based diagrams offer fast edits and repeatable source, but automatic layout may not provide the same pixel-level control as a canvas-based technical diagram tool.
A useful comparison test is to model the same small system in each candidate. Include one user journey, one asynchronous interaction, and one database relationship. Then evaluate editing speed, reviewability, readability, export behavior, and how easily a new contributor can make a safe change.
Feature-by-feature breakdown
Flowcharts for decisions and processes
Use a flowchart for request handling, deployment gates, incident procedures, or conditional business logic. Keep node labels short and put detail in nearby prose.
flowchart TD
A[Receive request] --> B{Authenticated?}
B -- Yes --> C[Validate input]
B -- No --> D[Return 401]
C -- Valid --> E[Queue work]
C -- Invalid --> F[Return 400]
Choose a direction that matches the reader's path, such as top-to-bottom for a procedure or left-to-right for a system boundary. Avoid connecting every possible exception in one chart. Split complex behavior into focused diagrams.
Sequence diagrams for interactions
Sequence diagrams are useful when timing, ownership, and message order matter. They can clarify an API architecture, authentication exchange, retry path, or event publication flow.
sequenceDiagram
participant Client
participant API
participant Queue
participant Worker
Client->>API: POST /jobs
API->>Queue: Publish job
API-->>Client: 202 Accepted
Queue->>Worker: Deliver job
Worker-->>Queue: Acknowledge
Label messages with meaningful operations rather than implementation trivia. If a retry, timeout, or failure path is important, show it explicitly; otherwise state the assumption below the diagram.
ERDs and class diagrams for structure
An ERD can document tables and relationships without replacing the database schema itself. Keep the diagram aligned with the authoritative migration or schema files, and identify whether a relationship is logical, enforced by a foreign key, or merely used by application code. The database schema and ERD guide offers a deeper approach to modeling tables and relationships.
erDiagram
CUSTOMER ||--o{ ORDER : places
ORDER ||--|{ ORDER_ITEM : contains
PRODUCT ||--o{ ORDER_ITEM : appears_in
CUSTOMER {
int id PK
string email
}
ORDER {
int id PK
int customer_id FK
date created_at
}
Use class diagrams when the reader needs object relationships, interfaces, or inheritance rather than persisted data. Do not use an ERD as a substitute for a complete migration plan: the diagram should explain structure, not become an unverified second source of truth.
State diagrams for lifecycle behavior
State diagrams make status transitions explicit for orders, deployments, incidents, or background jobs. They are especially helpful when an object can be cancelled, retried, suspended, or reopened.
stateDiagram-v2
[*] --> Pending
Pending --> Running: start
Running --> Succeeded: complete
Running --> Failed: error
Failed --> Running: retry
Succeeded --> [*]
Define transition names consistently and identify who or what performs each transition in the surrounding text. This keeps the diagram readable while preserving operational context.
Preview, render, and embed
Use a local preview while writing, but verify the final documentation build before merging. A practical workflow is to keep diagrams in the Markdown file when they are short, place larger diagrams in dedicated files when supported by the documentation system, and run the site's normal build or link-check process in CI.
When a platform cannot render Mermaid, consider a generated SVG or PNG as a build artifact rather than a manually edited image. Keep the Mermaid source as the editable authority, document the rendering command, and check that generated assets are refreshed when source changes. For public documentation, provide a text explanation or accessible alternative so the meaning does not depend entirely on visual layout.
Best fit by scenario
- Repository README: Mermaid is a good fit for a compact component overview, setup flow, or request lifecycle, provided the repository viewer supports the syntax used.
- Architecture decision record: Use a diagram beside the decision and record its scope, assumptions, and date. For larger systems, combine a context or container view with links to focused service diagrams. See the guide to C4 model diagrams for a structured way to document architecture at multiple levels.
- API documentation: Use sequence diagrams to explain common calls, authentication, asynchronous work, and failure handling. Keep the API specification itself authoritative for endpoints and schemas.
- Database documentation: Use an ERD for relationships and a short companion table for constraints or lifecycle rules. Refresh it when migrations change, not only when a release is published.
- DevOps runbook: Use flowcharts for incident decisions and state diagrams for deployment or recovery states. Avoid placing secrets, private addresses, or unnecessary infrastructure detail in widely shared files.
- Highly visual collaboration: Choose a canvas-based online diagram maker when workshops, freeform layout, or nontechnical editing are central requirements. Exported images can still be embedded in docs, but retain ownership and update instructions.
For architecture work, pair diagrams with a review checklist covering clarity, accuracy, security, and ownership. The diagram review checklist for engineering teams is a useful companion to a pull request template.
When to revisit
Revisit a Mermaid-based documentation workflow whenever the rendering environment changes. A documentation platform upgrade, static site generator migration, new theme, or CI change can affect supported syntax, fonts, layout, and generated assets. Treat renderer changes like dependency changes: test representative diagrams before updating the workflow.
Review the approach when diagrams become difficult to read, frequent merge conflicts appear, or contributors bypass the source because editing is too cumbersome. Those are signals to split a large diagram, establish naming conventions, add templates, or compare Mermaid with a Mermaid diagram alternative such as PlantUML, D2, or Graphviz.
Update diagrams when the architecture changes, but also review them on ownership changes and major operational incidents. A diagram with no named owner is likely to become stale. Add a short metadata block or nearby note identifying scope, last review date, and the team responsible for verification.
To put this workflow into practice, start with one small diagram in the repository where it will be maintained. Confirm local and CI rendering, add a review rule that checks source changes, and document the intended renderer. Then expand to sequence diagrams, database schema diagrams, or state models only when they answer a real documentation question. This keeps docs-as-code diagrams useful, reviewable, and connected to the systems they describe.