Mastering Microservices Visualization: A Step-by-Step Guide to C4 Container Diagrams with VPasCode

VPasCode PlantUML code generating C4 Container diagram for Retail Platform

Microservices architecture promises flexibility and scalability, but it often introduces complexity that makes the system difficult to understand. Without a clear map, teams struggle to explain how customer-facing applications, APIs, databases, and message brokers fit together. The C4 Model offers a practical solution to this problem by visualizing software architecture at different levels of detail. This tutorial guides you through the second level of the C4 model—Containers—using a concrete retail platform example. We will explore how to use Visual Paradigm and its VPasCode tool to create these diagrams, ensuring your architecture documentation remains useful as your system evolves.

Understanding the C4 Model Levels

The C4 model stands for Context, Containers, Components, and Code. These are not mandatory steps but rather levels of abstraction designed to answer specific questions about your system. By separating these concerns, you avoid cluttering a single diagram with too much information.

  • Level 1: System Context – Who uses the system, and which external systems does it interact with?
  • Level 2: Container – What are the major applications, services, data stores, and deployable units?
  • Level 3: Component – What are the main building blocks within a selected container (service)?
  • Level 4: Code – How is a specific component implemented in code (classes and interfaces)?

Diving Deep into Level 2: The Container Diagram

The Container diagram zooms into the system to show its major building blocks. In C4 terminology, a container is a separately running or deployable unit, such as a web application, a backend service, or a database. It is not necessarily a Docker container, but rather a logical unit of deployment.

Key Elements for Microservices

In our retail example, the Container diagram reveals the distributed nature of the architecture. This view clarifies which service owns which data store. For example, the Order Service writes to the Order Database, and the Catalog Service writes to the Product Database. This prevents the misconception that every service can freely access every database.

Practical Example: Online Retail Platform

Imagine an online retail platform. At the Container level, we focus on the major deployable units:

  • Web Application: Displays product listings and shopping workflows.
  • API Gateway: Routes client requests to backend services.
  • Catalog Service: Provides product information.
  • Order Service: Creates and tracks orders.
  • Message Broker: Carries events asynchronously between services.
  • Databases: Product, Order, and Payment databases.

Visualizing with VPasCode: The Code-First Approach

Visual Paradigm’s VPasCode tool allows you to write diagram definitions as code. This makes it easier to review diagram changes alongside other project files and to discuss revisions in a code-focused process.

Below is the code definition for our Retail Platform Container Diagram. You can copy this directly into the VPasCode editor.

@startuml
!include https://static.visual-paradigm.com/plantuml-stdlib/C4-PlantUML/master/C4_Container.puml
LAYOUT_TOP_DOWN()
title Container diagram for Retail Platform

Person(customer, "Customer", "Shops for products and places orders online")

System_Ext(paymentProvider, "Payment Provider", "External payment processor")

System_Boundary(platform, "Retail Platform") {
    Container(webApp, "Web Application", "HTML, JS, CSS", "Displays products and shopping workflows")
    Container(apiGateway, "API Gateway", "Spring Cloud Gateway", "Routes requests to backend services")
    ContainerDb(productDb, "Product Database", "PostgreSQL", "Stores product catalog")
    ContainerDb(orderDb, "Order Database", "PostgreSQL", "Stores order records")
    ContainerDb(paymentDb, "Payment Database", "PostgreSQL", "Stores payment records")
    Container(svcCatalog, "Catalog Service", "Java/Spring Boot", "Manages product information")
    Container(svcOrder, "Order Service", "Java/Spring Boot", "Manages order lifecycle")
    Container(svcPayment, "Payment Service", "Java/Spring Boot", "Coordinates payments")
    Container(svcNotify, "Notification Service", "Node.js", "Sends order updates")
    ContainerQueue(broker, "Message Broker", "RabbitMQ", "Handles asynchronous events")
}

Rel(customer, webApp, "Uses", "HTTPS")
Rel(webApp, apiGateway, "Routes to", "HTTPS/REST")
Rel(apiGateway, svcCatalog, "Gets product details")
Rel(apiGateway, svcOrder, "Places order")
Rel(svcCatalog, productDb, "Reads/writes", "JDBC")
Rel(svcOrder, orderDb, "Reads/writes", "JDBC")
Rel(svcPayment, paymentDb, "Reads/writes", "JDBC")
Rel(svcOrder, broker, "Publishes order event")
Rel(svcNotify, broker, "Consumes order event")
Rel(svcPayment, paymentProvider, "Sends payment request", "HTTPS/REST")
Rel_U(webApp, paymentProvider, "Payment redirect", "HTTPS")
Rel_R(paymentProvider, svcNotify, "Payment status callback", "HTTPS")
@enduml

Decoding the Diagram Elements

When reviewing the output of the code above, notice how the visualizer renders specific shapes for different types of containers:

  • Standard Containers: Represented as rectangles (e.g., Web Application, API Gateway, Services).
  • Databases: Represented as cylinders (e.g., PostgreSQL instances). This distinction is crucial for database owners to quickly identify storage boundaries.
  • Message Brokers: Represented as queues or cylinders (e.g., RabbitMQ), indicating asynchronous communication channels.
  • External Systems: Shown outside the system boundary (e.g., Payment Provider), indicating dependencies on third-party services.

Best Practices for Maintaining Diagrams

To ensure your architecture documentation stays relevant, follow these workflow strategies within Visual Paradigm:

  1. Automate with AI: Use AI-assisted C4 diagram generation to move from a written architecture description to an initial diagram. Describe your system, and the AI can generate a draft structure to review and refine.
  2. Code-Oriented Workflow: Use VPasCode to keep diagram definitions in version control. This allows you to review diagram changes alongside other project files and discuss revisions in a code-focused process.
  3. Keep Diagrams in OpenDocs: Bring diagrams into OpenDocs to present them alongside explanatory documentation. Connect diagram creation with documentation rather than leaving architecture visuals isolated.

Common Mistakes to Avoid

When creating C4 diagrams, be careful not to fall into these common traps:

  • Treating every component as a microservice: Remember, a component is an internal building block; a container is a deployable unit.
  • Leaving relationships unlabeled: An arrow without a meaningful label forces readers to guess the interaction type.
  • Showing implementation details too early: Class names and database tables distract from the architectural story at the Container level.

By following the C4 model and leveraging tools like Visual Paradigm, you can create a set of diagrams that helps people understand the architecture—not just a complete inventory of every technical detail.