Visualizing Microservices with the C4 Model: From Context to Code

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 four levels of the C4 model—Context, Container, Component, and Code—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.
What is the C4 Model?
C4 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)?
Level 1: The System Context Diagram
The Context diagram gives readers the widest view. It defines the system boundary, identifies the people (actors) or roles that use it, and highlights the external systems it depends on. This is the “big picture” view intended for non-technical stakeholders as well as technical teams.
Example: Online Retail Platform
Imagine an online retail platform. At the Context level, we focus on the user and the system itself, ignoring internal services.
- Person: Customer (shops for products and places orders).
- Software System: Retail Platform (manages orders and payments).
- External System: Payment Provider (authorizes and settles card payments).
Using Visual Paradigm, you can draft this using VPasCode or the visual editor. The diagram should clearly show relationships, such as the Customer browsing via the platform and the Platform sending payment requests to the Provider.

@startuml
!include https://static.visual-paradigm.com/plantuml-stdlib/C4-PlantUML/master/C4_Context.puml
LAYOUT_TOP_DOWN()
skinparam defaultFontSize 14
title System Context diagram for Retail Platform
Person(customer, "Customer", "Shops for products and places orders online")
System(retail, "Retail Platform", "Browses catalogue, manages orders and payments")
System_Ext(payment, "Payment Provider", "Authorises and settles card payments")
Rel(customer, retail, "Browses and orders via")
Rel(retail, payment, "Sends payment requests to", "HTTPS/REST")
@end 
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:
- 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.
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.

@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 
Level 3: The Component Diagram
A Component diagram zooms into a single container, such as the Order Service. It shows the significant internal building blocks and their relationships. It is not intended to show every class or function, but rather the meaningful responsibilities within that service.
Internal Structure of a Service
For the Order Service, the components might include:
- Order API: Accepts order-related requests.
- Order Application Logic: Coordinates order workflows.
- Order Repository: Reads and writes order data.
- Inventory Client: Requests inventory information from another service.
- Payment Coordinator: Initiates payment processing.
This level is crucial when a team needs to discuss implementation responsibilities, service refactoring, or how a feature should fit within a service boundary.

@startuml
!include https://static.visual-paradigm.com/plantuml-stdlib/C4-PlantUML/master/C4_Component.puml
LAYOUT_TOP_DOWN()
skinparam vpDiagramType C4modelComponentDiagram
skinparam backgroundColor transparent
skinparam shadowing false
title Component diagram for Order Service - Order Service
Container_Boundary(svcOrder, "Order Service") {
Container_Boundary(logic, "Order Logic") {
Component(orderApi, "Order API", "REST Controller", "Accepts order requests")
Component(orderLogic, "Order Logic", "Service", "Coordinates order workflow")
Component(orderRepo, "Order Repository", "Data Access", "Reads/writes order data")
Component(invClient, "Inventory Client", "Client", "Requests inventory info")
Component(payCoord, "Payment Coordinator", "Client", "Initiates payments")
Component(eventPub, "Event Publisher", "Publisher", "Publishes order events")
}
ContainerDb(orderDb, "Order Database", "PostgreSQL", "Stores order records")
System_Ext(invService, "Inventory Service", "Microservice", "Manages stock")
}
Rel(orderApi, orderLogic, "Invokes")
Rel(orderLogic, orderRepo, "Uses", "JDBC")
Rel(orderLogic, invClient, "Calls", "HTTP")
Rel(orderLogic, payCoord, "Triggers", "HTTP")
Rel(orderLogic, eventPub, "Publishes")
Rel(orderRepo, orderDb, "Reads/Writes")
Rel(invClient, invService, "Queries stock", "HTTP")
@enduml 
Level 4: Code-Level Diagrams (Selective Use)
The Code level describes implementation details such as classes or interfaces. In a microservices project, this level is often more useful for a focused design or code review than as a permanent overview. Because code changes frequently, detailed diagrams can become outdated quickly. Create one only when it helps answer a specific question, such as how a specific algorithm works.
Maintaining C4 Diagrams with Visual Paradigm
Visual Paradigm offers a comprehensive workflow to keep your architecture documentation alive and accurate.
1. Automate with AI
You can use AI-assisted C4 diagram generation to move from a written architecture description to an initial diagram. Describe your system (e.g., “An online retail platform with a web app, API gateway, and order service”), and the AI can generate a draft structure. Treat this as a starting point to review and refine.
2. Code-Oriented Workflow (VPasCode)
For teams that prefer working with text, VPasCode 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. You can generate a draft and then edit the code or refine the diagram visually.
3. Keep Diagrams in OpenDocs
To ensure diagrams stay relevant, bring them into OpenDocs. This allows you to present diagrams alongside explanatory documentation. A document might introduce the system’s purpose, embed a Context diagram, explain the main containers, and link those views to relevant design notes. This connects diagram creation with documentation, rather than leaving architecture visuals isolated.
Common Mistakes to Avoid
- Putting everything in one diagram: A single diagram containing every service, data store, and component quickly becomes unreadable. Use separate C4 views.
- Treating every component as a microservice: A component is an internal building block; a container is a deployable unit. These are different levels.
- 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 Context and Container levels.
- Letting diagrams go stale: An outdated diagram is confusing. Assign responsibility for keeping views aligned with architectural changes.
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.