Microservices architecture creates a problem that monoliths never had: dozens or hundreds of services, each with its own dependencies, communication patterns, and deployment configurations. Try drawing that on a whiteboard and you'll spend more time erasing and redrawing than actually explaining anything. That's exactly where diagram-as-code tools come in. Instead of dragging boxes around in a GUI, you describe your architecture in plain text, and the tool renders a diagram for you. This approach keeps your diagrams version-controlled, reproducible, and always in sync with your actual system.

What does "diagram as code" actually mean for microservices?

Diagram as code means you write your architecture diagrams using a text-based syntax similar to how you'd write code instead of using a visual editor. The tool reads that text file and generates a diagram automatically.

For microservices specifically, this matters because your architecture changes frequently. Services get added, renamed, decommissioned, and split. With a visual tool, every change means opening the file, dragging shapes, relabeling boxes, and hoping nobody else is editing at the same time. With diagram as code, you change a line of text, commit it to Git, and your team sees the update in a pull request.

The underlying concept is simple: treat your architecture diagrams the same way you treat application code version them, review them, and generate them from a source of truth.

Why do teams working with microservices prefer text-based diagramming?

There are a few practical reasons this approach fits microservices so well:

  • Version control. Your diagrams live in the same repository as your service definitions. You can see who changed what, when, and why.
  • Automation. You can generate diagrams as part of your CI/CD pipeline. Push a change to your service mesh config, and the updated diagram appears in your documentation automatically.
  • Consistency across teams. When 15 teams maintain 80 services, a visual diagraming tool creates chaos. Code-based diagrams enforce a standard format that everyone follows.
  • Speed. Editing text is faster than clicking through a GUI for anyone comfortable with a terminal and a text editor.

If you're comparing this approach to traditional tools, our comparison of architecture diagram tools for developers covers the tradeoffs in more detail.

Which diagram-as-code tools work best for microservices?

1. Structurizr

Structurizr was built specifically for the C4 model, which breaks architecture into four levels: context, containers, components, and code. This layered approach maps directly to how microservices teams think about their systems. You describe your architecture using a DSL (domain-specific language), and Structurizr renders clean, layered diagrams.

Why it fits microservices: The C4 model forces you to think about the right level of abstraction. At the container level, each microservice appears as a box with its technology and responsibilities. You can zoom into a specific service to see its internal components.

Limitation: The DSL has a learning curve. If your team hasn't used the C4 model before, expect a few days of adjustment.

2. Mermaid

Mermaid is a JavaScript-based diagramming tool that renders diagrams from a simple markdown-like syntax. It supports flowcharts, sequence diagrams, class diagrams, and more. GitHub, GitLab, and many documentation platforms render Mermaid natively.

Why it fits microservices: Most developers already know markdown, so Mermaid feels familiar. Sequence diagrams are particularly useful for showing how microservices communicate during a request. You can embed these directly in README files or architectural decision records.

Limitation: Mermaid doesn't have a dedicated microservices or C4 diagram type. You have to adapt its generic diagram types, which works but can feel clumsy for large architectures.

3. PlantUML

PlantUML has been around since 2009 and supports a wide range of diagram types: sequence, deployment, component, state, and activity diagrams. Its syntax is text-based, and it integrates with many IDEs and CI tools.

Why it fits microservices: Deployment diagrams in PlantUML let you show which services run on which nodes, how they connect, and what protocols they use. Component diagrams map cleanly to service boundaries. There's also a C4 extension for PlantUML that brings C4 model support.

Limitation: PlantUML's default visual style looks dated compared to newer tools. Customizing themes requires extra work.

4. D2

D2 is a newer diagram scripting language that emphasizes readability and modern styling. Its syntax is clean, and it produces visually polished diagrams without much configuration.

Why it fits microservices: D2 handles large, complex diagrams well. It has automatic layout optimization, which matters when you're diagramming 30+ services. Connections between services are easy to define, and you can add icons, shapes, and styles declaratively.

Limitation: The ecosystem is still maturing. Fewer integrations with documentation platforms compared to Mermaid or PlantUML.

5. Diagrams (Python)

Diagrams is a Python library that lets you create cloud architecture diagrams using Python code. It supports AWS, Azure, GCP, Kubernetes, and other cloud-native tooling out of the box.

Why it fits microservices: If your microservices run on Kubernetes or a cloud provider, Diagrams lets you use official cloud provider icons and describe your infrastructure in Python. It's particularly good for showing the deployment topology load balancers, service meshes, databases, queues, and the services that connect them.

Limitation: Requires Python knowledge. Not ideal for teams that don't use Python in their stack.

6. Kroki

Kroki is a unified API that takes diagram definitions in multiple formats Mermaid, PlantUML, Graphviz, D2, and others and renders them through a single interface. Think of it as a translation layer for diagram-as-code tools.

Why it fits microservices: If different teams prefer different tools, Kroki lets everyone use what they like while producing consistent output. You can self-host it, which matters for organizations with strict security requirements.

For teams exploring more options across cloud and enterprise contexts, our guide to cloud architecture diagramming software covers additional tools suited for larger deployments.

How do you choose the right tool for your microservices setup?

The answer depends on three things:

  1. How many services are you diagramming? For fewer than 10 services, almost any tool works. For 50+, you need strong layout algorithms D2 and Structurizr handle scale better than most.
  2. Where do your diagrams live? If they're in GitHub READMEs, Mermaid is the obvious choice since it renders inline. If they're in a dedicated documentation site, Structurizr or PlantUML give you more control.
  3. What does your team already know? A tool nobody uses produces no diagrams. Mermaid has the lowest barrier to entry. PlantUML and D2 require a short ramp-up period. Diagrams (Python) requires comfort with Python.

Our infrastructure diagram code syntax guide walks through the syntax differences between these tools so you can compare them side by side.

What mistakes do teams make when diagramming microservices as code?

Here are the most common ones:

  • Trying to show everything in one diagram. A single diagram with 60 services, their databases, message queues, and external APIs is unreadable. Use multiple diagrams at different zoom levels instead.
  • Not defining a diagram hierarchy. Without a structure like the C4 model's context/container/component layers your diagrams have no logical progression. Viewers don't know where to start.
  • Letting diagrams go stale. The whole point of diagram-as-code is keeping diagrams current. If your diagrams live outside the repository or aren't updated during service changes, they become misleading fast.
  • Ignoring communication patterns. Showing that Service A connects to Service B isn't enough. Label the protocol (gRPC, REST, async messaging), the data flow direction, and the relationship type (sync call, event subscription, shared database).
  • Over-styling. Adding colors, icons, and custom shapes everywhere makes diagrams look busy. Use visual encoding sparingly for example, red for external dependencies and blue for internal services and keep everything else neutral.

What does a practical diagram-as-code workflow look like?

Here's a workflow that works for teams with 10–50 microservices:

  1. Store diagram source files in the monorepo or a dedicated docs repo. Name them by domain: order-service-context.d2, payment-sequence.mmd.
  2. Include diagram updates in the definition of done for any service change. If you add a new service or change a dependency, the diagram PR is part of the same feature branch.
  3. Use CI to render and publish diagrams. A GitHub Action or GitLab CI job can take your text files, render them using the tool's CLI, and push the output to your documentation site or a static HTML page.
  4. Create a layered diagram set: one system context diagram, one container diagram per bounded context, and component diagrams only for services where internal complexity justifies it.
  5. Review diagrams in pull requests. Since the source is text, reviewers can read the diff and comment on architectural changes directly.

Which tool produces the best-looking output without custom themes?

If visual quality matters for example, if these diagrams go into client-facing documentation or presentations D2 produces the cleanest output out of the box. Its default fonts, spacing, and color palette look modern and professional without any configuration.

Structurizr comes second. Its C4 model diagrams have a clear, standardized look that's easy to read at any size.

PlantUML's default output is functional but plain. Mermaid is similar. Both can look good with custom themes, but that requires additional work.

Quick checklist: getting started with diagram as code for microservices

  • Pick one tool based on your team's skill set and where diagrams will live
  • Adopt a diagram hierarchy C4 model is a solid default for microservices
  • Start with a system context diagram showing all services and external actors
  • Create container-level diagrams for each bounded context or domain
  • Store source files in version control alongside your service code
  • Set up CI to render diagrams automatically on every push
  • Add diagram reviews to your pull request checklist
  • Schedule a quarterly audit to catch stale or missing diagrams
  • Keep diagrams focused one concern per diagram, never more

Next step: If you haven't picked a tool yet, write a single system context diagram in both Mermaid and D2. Spend 15 minutes on each. The one that feels more natural to your team is the right starting point. You can always switch later since the investment per diagram is small that's the whole advantage of keeping diagrams as code.