Architecture Diagrams (C4 Model)
The C4 model provides a hierarchical way to visualize software architecture at different levels of abstraction: Context, Containers, Components, and Code. Perfect for documenting system architecture.
Use Case
Use architecture diagrams when you need to:
- Document system architecture
- Show system boundaries and interactions
- Communicate design to stakeholders
- Plan system components and their relationships
C4 Model Levels
- Context - System in its environment (users, external systems)
- Container - High-level technology choices (apps, databases, services)
- Component - Components within a container
- Code - Class diagrams (covered in UML snippet)
Code
1```mermaid
2C4Context
3 title System Context Diagram
4
5 Person(user, "User", "A user of the system")
6 System(systemA, "System A", "Main system")
7 System_Ext(systemB, "External System", "Third-party service")
8
9 Rel(user, systemA, "Uses")
10 Rel(systemA, systemB, "Calls API")
11```
Result:
Explanation
Person()- User or actorSystem()- Internal systemSystem_Ext()- External systemContainer()- Application, database, etc.Component()- Internal componentRel()- Relationship with label
Examples
Example 1: System Context
1```mermaid
2C4Context
3 title Research Platform - System Context
4
5 Person(researcher, "Researcher", "Conducts research and experiments")
6 Person(admin, "Administrator", "Manages system")
7
8 System(platform, "Research Platform", "Manages research projects, experiments, and data")
9
10 System_Ext(storage, "Cloud Storage", "Stores research data")
11 System_Ext(compute, "HPC Cluster", "Runs computational experiments")
12 System_Ext(auth, "Auth Service", "Handles authentication")
13
14 Rel(researcher, platform, "Uses", "HTTPS")
15 Rel(admin, platform, "Administers", "HTTPS")
16 Rel(platform, storage, "Stores/retrieves data", "S3 API")
17 Rel(platform, compute, "Submits jobs", "SSH/API")
18 Rel(platform, auth, "Authenticates users", "OAuth 2.0")
19```
Result:
Example 2: Container Diagram
1```mermaid
2C4Container
3 title Research Platform - Container Diagram
4
5 Person(user, "Researcher")
6
7 Container(web, "Web Application", "React", "Provides research UI")
8 Container(api, "API Server", "Go", "Handles business logic")
9 Container(worker, "Worker Service", "Python", "Processes experiments")
10 ContainerDb(db, "Database", "PostgreSQL", "Stores research data")
11 ContainerDb(cache, "Cache", "Redis", "Caches results")
12 Container(queue, "Message Queue", "RabbitMQ", "Job queue")
13
14 Rel(user, web, "Uses", "HTTPS")
15 Rel(web, api, "Calls", "REST/JSON")
16 Rel(api, db, "Reads/writes", "SQL")
17 Rel(api, cache, "Reads/writes", "Redis protocol")
18 Rel(api, queue, "Publishes jobs", "AMQP")
19 Rel(worker, queue, "Consumes jobs", "AMQP")
20 Rel(worker, db, "Updates results", "SQL")
21```
Result:
Example 3: Component Diagram
1```mermaid
2C4Component
3 title API Server - Component Diagram
4
5 Container(web, "Web App", "React")
6
7 Component(controller, "API Controller", "Go", "Handles HTTP requests")
8 Component(service, "Business Logic", "Go", "Core logic")
9 Component(repo, "Repository", "Go", "Data access")
10 ComponentDb(db, "Database", "PostgreSQL")
11
12 Rel(web, controller, "Calls", "REST")
13 Rel(controller, service, "Uses")
14 Rel(service, repo, "Uses")
15 Rel(repo, db, "Queries", "SQL")
16```
Result:
Example 4: Simple Architecture with Flowchart
For simpler architectures, you can use flowcharts:
1```mermaid
2graph TB
3 subgraph "Client Layer"
4 Web[Web Browser]
5 Mobile[Mobile App]
6 end
7
8 subgraph "API Layer"
9 Gateway[API Gateway]
10 Auth[Auth Service]
11 end
12
13 subgraph "Business Layer"
14 Service1[User Service]
15 Service2[Data Service]
16 Service3[Analytics Service]
17 end
18
19 subgraph "Data Layer"
20 DB[(Database)]
21 Cache[(Cache)]
22 Storage[(Object Storage)]
23 end
24
25 Web --> Gateway
26 Mobile --> Gateway
27 Gateway --> Auth
28 Gateway --> Service1
29 Gateway --> Service2
30 Gateway --> Service3
31 Service1 --> DB
32 Service2 --> DB
33 Service2 --> Cache
34 Service3 --> Storage
35```
Result:
Notes
- Start with Context diagram for high-level view
- Use Container diagram for deployment architecture
- Component diagram for detailed internal structure
- Keep diagrams focused - one diagram per level
Gotchas/Warnings
- ⚠️ Level mixing: Don't mix C4 levels in one diagram
- ⚠️ Too detailed: Keep appropriate level of abstraction
- ⚠️ Technology: Include technology choices in descriptions
- ⚠️ Boundaries: Clearly show system boundaries
Related Snippets
- Chart.js Bar Chart
Create bar charts for data visualization - Graphviz DOT Diagrams
Create complex graph layouts with Graphviz DOT language - KaTeX Math Examples and Tips
Comprehensive guide to KaTeX math notation with examples and tips - Mermaid Arbitrary Graphs
Create arbitrary graphs and networks with Mermaid - Mermaid Block Diagrams
Create block diagrams for system architecture with Mermaid - Mermaid Charts (Pie, Bar, Line)
Create pie charts, bar charts, and line charts with Mermaid - Mermaid Class Diagrams (UML)
Create UML class diagrams with Mermaid - Mermaid Entity Relationship Diagrams
Create Entity Relationship Diagrams (ERD) with Mermaid - Mermaid Flowchart
Create flowcharts and decision trees with Mermaid - Mermaid Gantt Chart
Create Gantt charts for project timelines and scheduling - Mermaid Git Diagrams
Create Git branch and commit diagrams with Mermaid - Mermaid Kanban Boards
Create Kanban boards for project management with Mermaid - Mermaid Mindmaps
Create mindmaps for brainstorming and organizing ideas with Mermaid - Mermaid Packet Diagrams
Create packet diagrams for network protocols with Mermaid - Mermaid Quadrant Charts
Create quadrant charts for prioritization with Mermaid - Mermaid Radar Charts
Create radar charts for multi-dimensional comparisons with Mermaid - Mermaid Requirement Diagrams
Create requirement diagrams for system requirements with Mermaid - Mermaid Sankey Diagrams
Create Sankey diagrams for flow visualization with Mermaid - Mermaid Sequence Diagram
Create sequence diagrams for interactions and API flows - Mermaid State Diagrams
Create state diagrams and state machines with Mermaid - Mermaid Timeline Diagrams
Create timeline diagrams for chronological events with Mermaid - Mermaid Treemap Diagrams
Create treemap diagrams for hierarchical data visualization with Mermaid - Mermaid User Journey Diagrams
Create user journey maps with Mermaid - Mermaid ZenUML Diagrams
Create ZenUML sequence diagrams with Mermaid - P5.js Interactive Visualizations
Create interactive visualizations and animations with P5.js