Mermaid architecture diagrams
You can choose a [[flowchart-diagram]] for component boundaries and a [[sequence-diagram]] for order placement, so the engineering post explains both system structure and request flow.
Learning outcomes
- Explain how Mermaid source keeps architecture diagrams version-controlled and reviewable alongside application code.
- Choose a flowchart to show component boundaries and a sequence diagram to show the order-placement request flow.
- Use Mermaid nodes, labels, edges, participants, message arrows, loops and alternatives to describe the Go backend accurately.
- Identify diagram mistakes that can misrepresent the architecture or prevent rendering in GitHub.
When a customer places an order, the Go API writes the order and its outbox event to MySQL. A separate relay publishes the committed event to Redpanda. Someone opening the repository needs to understand both which components make this flow possible and the order in which they exchange messages.
A Mermaid diagram describes a visual structure using text inside a fenced Markdown code block tagged mermaid. GitHub recognizes these blocks and renders them as diagrams in Markdown files. The source stays in the repository alongside the code it documents.
This gives you a practical way to keep architecture documentation reviewable. A change to a component or message appears in a Git diff, so reviewers can inspect the diagram update alongside the implementation. You do not need to maintain a separately exported PNG or SVG file.
The diagram still needs updating when the architecture changes. Mermaid makes that change easier to review, but it does not discover application architecture automatically.
Component boundaries with flowcharts
A flowchart represents a system using nodes and directed edges. Each node represents a component, while an edge represents a relationship or communication path.
Start with the components involved in the food-delivery application:
- The customer and rider apps communicate with the Go API over HTTPS.
- The API uses MySQL for persistent data and Redis for cache and coordination state.
- An outbox relay reads committed events from MySQL and publishes them to Redpanda.
- A notifier consumes events and sends updates to interested clients.
The following flowchart gives another engineer an overview of those boundaries.
flowchart LR
Customer["Customer App"] -->|HTTPS| API["Go API"]
Rider["Rider App"] -->|HTTPS| API
API -->|SQL| MySQL[("MySQL")]
API -->|Cache and coordination| Redis[("Redis")]
MySQL -->|Outbox records| Relay["Outbox Relay"]
Relay -->|Publish events| Kafka["Redpanda (Kafka)"]
Kafka -->|Consume events| Notifier["Notifier"]Notice the distinction between the components and their connections. MySQL stores the durable records, while the relay handles event publication. The diagram does not show every package, handler or database table because those details would distract from the system boundaries.
Direction and nodes
The first line declares the diagram type and direction.
flowchart LR
App["Mobile App"] --> API["Go API"]LR means left to right. TD and TB mean top to bottom. Use LR when the architecture fits across the page. Use TD when a longer chain would otherwise make the diagram too wide.
Each node has an identifier and a displayed label. In the example, App and API are identifiers. Mobile App and Go API are the labels displayed to the reader.
Keep identifiers short because you use them when defining edges. Give labels enough detail to identify the components without making them unnecessarily long.
Edges and database shapes
An edge connects two nodes. The arrow --> creates a directed connection, and text between | characters labels that connection.
flowchart LR
App["Mobile App"] -->|HTTPS| API["Go API"]
API -->|SQL| DB[("MySQL")]Here, HTTPS and SQL explain how the components communicate. The database node uses a cylinder shape through the id[("Label")] syntax. This makes MySQL visually distinct from application components.
Choose labels that describe the actual relationship. An edge labelled SQL communicates a different interaction from one labelled Publish events.
Subgraphs
A subgraph groups related nodes under a shared heading. Use it when grouping helps readers distinguish application components from backend components.
flowchart TD
subgraph Applications
Customer["Customer App"]
Rider["Rider App"]
end
subgraph Backend
API["Go API"]
Redis[("Redis")]
end
Customer --> API
Rider --> API
API --> RedisThe subgraph declaration begins a group, and end closes it. You can connect nodes across groups as well as within them.
Grouping is optional. A small architecture diagram often communicates more clearly without subgraphs. Add them when they explain a real boundary, not as decoration.
Order placement with sequence diagrams
A flowchart tells you which components exist and how they connect. It does not show the order in which a request moves through those components.
A sequence diagram represents participants and the ordered messages they exchange. Use it to explain a specific application flow, such as placing an order through POST /v1/orders.
The request includes an idempotency key. If the customer retries a request with the same key, the API must return the existing result rather than create another order. The following diagram shows the main request and event-publication path.
sequenceDiagram
actor Customer
participant API as Go API
participant DB as MySQL
participant Relay as Outbox Relay
participant Kafka as Redpanda
participant Worker as Notifier
Customer->>API: POST /v1/orders
Note over Customer,API: Request includes Idempotency-Key
API->>DB: Check key
alt Key already exists
DB-->>API: Existing order
API-->>Customer: Existing order response
else New key
API->>DB: Commit order, key result, and outbox event
API-->>Customer: 201 Created
end
Relay->>DB: Read committed outbox events
Relay-)Kafka: Publish order event
Kafka-->>Relay: Acknowledgement
Relay->>DB: Mark event published
Kafka->>Worker: Deliver eventThe diagram separates the synchronous customer request from asynchronous event publication. The customer receives an order response without waiting for the notifier to finish processing the event.
Participants and actors
Each participant represents a component involved in the interaction. Declare participants before using them in messages.
sequenceDiagram
actor Customer as Customer
participant API as Go API
participant DB as MySQLactor displays a participant using an actor symbol. participant declares a regular participant.
The syntax participant API as Go API assigns the short identifier API and the displayed name Go API. You can use the identifier in message definitions without repeating the full label.
Message arrows
A sequence diagram uses different arrows to communicate the kind of interaction.
sequenceDiagram
participant API
participant DB
API->>DB: Insert order
DB-->>API: Insert result
API-)DB: Publish eventThe main arrow styles are:
->>represents a solid arrow. Use it for a request or message.-->>represents a dashed reply.-)represents an asynchronous message, such as publishing an event without waiting for a normal request-response exchange.
Choose arrows that describe the interaction accurately. Using a reply arrow for a request can make the direction of responsibility confusing.
Notes and numbering
autonumber adds sequence numbers to messages. This makes it easier to refer to individual interactions during reviews.
Note over places a note above multiple participants. Use it to explain a constraint without adding another component or message.
sequenceDiagram
participant Customer
participant API
Note over Customer,API: The request carries an Idempotency-Key
Customer->>API: Submit orderThe note explains a property of the request. The message itself shows the interaction.
Loops and alternatives
Backend flows contain repeated operations and conditional behavior. Mermaid provides loop and alt blocks to represent these cases.
A loop block represents repeated work. The outbox relay, for example, reads pending events repeatedly.
sequenceDiagram
participant Relay
participant DB
loop Pending outbox events
Relay->>DB: Fetch next batch
DB-->>Relay: Return events
endAn alt block represents alternative execution paths. When a request uses an existing idempotency key, the API returns the previous result. A new key follows the order-creation path instead.
sequenceDiagram
participant API
participant DB
alt Existing key
DB-->>API: Return existing order
else New key
API->>DB: Create new order
endThe else branch describes the alternative path, and end closes the conditional block. Keep branches focused on meaningful differences in behavior.
Event flow and architectural boundaries
The order sequence also explains why the API does not publish directly to Kafka as part of the request. The transactional outbox stores the order and its corresponding event in the same MySQL transaction.
This protects the system from a dual-write failure. Without the outbox, the API could commit an order and fail before publishing its event. The database would contain an order that downstream components never hear about.
The relay reads committed outbox records and publishes them to Redpanda, which exposes the Kafka API. Redpanda organizes records into partitions. Partitions distribute records and provide ordering within each partition, rather than a global order across the entire topic.
A consumer group allows multiple notifier instances to share the work of consuming events. This is a different relationship from the connection between the relay and the broker, so show it only when it helps explain the architecture.
The relay can also publish an event and fail before marking the outbox record as published. It may publish that event again after restarting. Consumers must tolerate duplicate delivery through idempotent processing.
These distinctions matter when drawing the diagrams. A component flowchart should show the relay between MySQL and Redpanda. A sequence diagram should show the database commit before the relay publishes the event. Drawing the API as a direct publisher would describe a different system with different failure behavior.
Common diagram mistakes
Several mistakes make diagrams misleading or prevent them from rendering correctly.
Unquoted labels
Mermaid interprets some characters as syntax. An unquoted label containing a slash, parentheses, brackets or a colon can cause parsing problems. The lowercase word end can also cause problems in certain labels.
This declaration is unsafe:
flowchart TD
API[Order API / v1]Quote the label instead:
flowchart TD
API["Order API / v1"]Double quotes make it clear that the characters belong to the displayed label rather than the diagram syntax.
Undeclared sequence participants
Every sequence-diagram message endpoint should have a corresponding participant or actor declaration.
An undeclared endpoint can become an accidental participant. A spelling mistake may therefore produce a diagram that still renders but communicates the wrong architecture.
Declare participants explicitly and compare every message endpoint with the declarations. For example, use the same Kafka identifier in both the participant declaration and the publication message.
Oversized diagrams
A flowchart with too many nodes and connections can become wider than the GitHub Markdown preview. Readers then have to scroll sideways to understand the architecture.
Keep the component flowchart focused on major boundaries. Use a separate sequence diagram for message ordering instead of adding every request detail to the flowchart.
The same rule applies to sequence diagrams. Include the interactions needed to explain the behavior, but leave unrelated implementation details out.
Unsupported Mermaid syntax
GitHub's Mermaid version can lag behind the latest version documented by Mermaid. A feature supported by the latest release may not render in GitHub.
Prefer documented, basic syntax when an advanced feature does not add meaningful information. A working diagram is more useful than a complicated one that GitHub cannot render.
Diagram validation
Check Mermaid source before committing it.
The Mermaid Live Editor helps you catch syntax errors while editing. It is also useful for checking whether nodes, arrows, labels and groups communicate the intended structure.
Then open the Markdown preview on GitHub. This checks the diagram against the Mermaid renderer that GitHub actually uses.
Finally, review the source as Markdown. Each diagram should sit next to the prose that explains it. Give readers a short caption before the diagram so they know what they are looking at.
A flowchart caption should describe the component boundaries. A sequence diagram caption should describe the request being traced. Avoid repeating every node or message in the caption.
README diagrams for the engineering post
The Project documentation package exercise uses the repository's README.md as an engineering post. Its Architecture section needs a Mermaid flowchart and a Mermaid sequence diagram.
The flowchart should give another engineer a view of the system's main components and their connections. The sequence diagram should trace order placement, including the database transaction and the later event-publication path.
Keep the two views separate because they answer different questions. A flowchart explains the structure. A sequence diagram explains the behavior over time.
The exercise also requires the README to connect its architecture to the project's measurements, debugging stories and engineering decisions. The diagrams are one part of that explanation, not a replacement for the surrounding prose.
Check your understanding
You are documenting the food-delivery backend for an engineer who needs to identify the main components and their communication paths. Which Mermaid diagram is the best fit?
References
Everything above is explained on this page; these are where it comes from.