Overview
C4 (Context, Container, Component, Code) diagrams visualize software architecture at different levels of abstraction. Sirena renders C4 diagrams with the element types, boundaries, and relationships shown below.
Supported C4 Levels
-
C4Context - System Context diagrams showing the big picture
-
C4Container - Container diagrams showing high-level technology choices
-
C4Component - Component diagrams showing components within containers
-
C4Dynamic - Dynamic diagrams showing runtime behavior
-
C4Deployment - Deployment diagrams showing infrastructure mapping
Basic Usage
Simple C4 Context Diagram
C4Context
title System Context for Banking System
Person(customer, "Customer", "A bank customer")
System(banking, "Banking System", "Core banking application")
System_Ext(email, "Email System", "External email service")
Rel(customer, banking, "Uses")
Rel(banking, email, "Sends emails", "SMTP")
This creates a context diagram showing:
-
A person (customer) interacting with the system
-
The main banking system
-
An external email system
-
Relationships between components
C4 Container Diagram
C4Container
title Container Diagram for Banking System
Person(customer, "Customer", "A bank customer")
Container(web, "Web Application", "React", "Customer-facing web app")
Container(api, "API", "Node.js", "Backend REST API")
ContainerDb(db, "Database", "PostgreSQL", "Stores customer data")
Rel(customer, web, "Uses", "HTTPS")
Rel(web, api, "Calls", "REST/JSON")
Rel(api, db, "Reads/Writes", "SQL")
Element Types
People
Systems
System
Represents a software system.
C4Context
System(app, "Application", "Main application system")
System_Ext
Represents an external system.
C4Context
System_Ext(crm, "CRM System", "Third-party CRM")
SystemDb
Represents a database system.
C4Context
SystemDb(maindb, "Main Database", "Central data store")
SystemDb_Ext
Represents an external database.
C4Context
SystemDb_Ext(legacy, "Legacy DB", "Old system database")
Containers
Container
Represents an application or service container.
C4Container
Container(web, "Web App", "React", "Frontend application")
Where:
-
web- Unique identifier -
"Web App"- Display label -
"React"- Technology stack -
"Frontend application"- Description
Relationships
Basic Relationship
Use Rel() to show a directional relationship:
C4Context
Person(user, "User")
System(app, "App")
Rel(user, app, "Uses")
Parameters:
-
Source element ID
-
Target element ID
-
Relationship label
-
Technology (optional)
Rel(web, api, "Calls API", "REST/HTTPS")
Boundaries
Boundaries group related elements together.
Enterprise Boundary
Groups systems within an enterprise.
C4Context
Enterprise_Boundary(org, "Our Organization") {
System(app1, "App 1")
System(app2, "App 2")
}
System Boundary
Groups containers within a system.
C4Container
System_Boundary(sys, "Banking System") {
Container(web, "Web App", "React")
Container(api, "API", "Node.js")
}
Element Attributes
Layout Configuration
Configure diagram layout using UpdateLayoutConfig():
C4Context
UpdateLayoutConfig($c4ShapeInRow=3, $c4BoundaryInRow=2)
Person(user, "User")
System(app, "App")
Complete Example
C4Context
title Internet Banking System Context
Enterprise_Boundary(bank, "Bank Boundary") {
Person(customer, "Customer", "Bank customer with accounts")
System_Boundary(core, "Core Banking") {
System(banking, "Banking System", "Main application")
SystemDb(db, "Database", "Customer and account data")
}
System_Ext(email, "Email System", "Microsoft Exchange")
}
System_Ext(mainframe, "Mainframe", "Legacy system")
BiRel(customer, banking, "Uses")
Rel(banking, db, "Reads/Writes", "SQL")
Rel(banking, email, "Sends emails", "SMTP")
Rel(banking, mainframe, "Gets data", "XML/HTTPS")
This creates a hierarchical diagram showing:
-
Enterprise boundary containing the bank’s systems
-
System boundary for core banking components
-
Internal and external systems
-
Database and email integration
-
Bidirectional and unidirectional relationships
Implementation Details
Architecture
C4 diagrams are implemented using the 6-file Parslet architecture:
-
Diagram::C4- Data model with elements, boundaries, relationships -
Parser::Grammars::C4- Parslet grammar for C4 syntax -
Parser::Builders::C4- Parse tree to diagram model builder -
Parser::C4- Main parser class -
Layout::C4- Diagram to graph layout -
Renderer::C4- SVG renderer
Layout::C4 positions elements with the fallback grid; layout_engine: :elk is not available for C4.
Rendering Pipeline
The C4 rendering pipeline:
-
Parse - Convert Mermaid source to
C4model -
Graph - Convert to graph structure with positions
-
Layout - Calculate positions for elements and boundaries
-
Render - Generate SVG with C4 styling
Limitations
-
Variable syntax (
${macroName}) has limited support -
Some test-specific constructs may not parse (sprites, links, tags)
-
Layout is a fallback grid, not the hierarchical ELK layout this page previously described
-
Oracle-valid corpus coverage:
11/11 passing (100.0%). -
Outside the compatibility denominator: 16/22
artifactcases currently render; 1/14invalidcases currently render. -
Generated from
scoreboard/corpus.jsonbybundle exec rake docs:compatibility.
Troubleshooting
Common Issues
Parse Errors
Issue: "Syntax error at line X"
Solution: Check for:
-
Proper element syntax:
Type(id, "label", "desc") -
Matching braces in boundaries
-
Comma-separated parameters
-
Quoted strings for labels and descriptions