C4 architecture skill

Generate architecture documentation using C4 model Mermaid diagrams.

by davila7·MIT license·★ 32,299 Stars on the repo·GitHub ↗

Use now

Files of C4 architecture

davila7/main1 file shown
SKILL.md
Show the full text296 lines

C4 Architecture Documentation

Generate software architecture documentation using C4 model diagrams in Mermaid syntax.

Workflow

  1. Understand scope - Determine which C4 level(s) are needed based on audience
  2. Analyze codebase - Explore the system to identify components, containers, and relationships
  3. Generate diagrams - Create Mermaid C4 diagrams at appropriate abstraction levels
  4. Document - Write diagrams to markdown files with explanatory context

C4 Diagram Levels

Select the appropriate level based on the documentation need:

Level Diagram Type Audience Shows When to Create
1 C4Context Everyone System + external actors Always (required)
2 C4Container Technical Apps, databases, services Always (required)
3 C4Component Developers Internal components Only if adds value
4 C4Deployment DevOps Infrastructure nodes For production systems
- C4Dynamic Technical Request flows (numbered) For complex workflows

Key Insight: "Context + Container diagrams are sufficient for most software development teams." Only create Component/Code diagrams when they genuinely add value.

Quick Start Examples

System Context (Level 1)
C4Context
  title System Context - Workout Tracker

  Person(user, "User", "Tracks workouts and exercises")
  System(app, "Workout Tracker", "Vue PWA for tracking strength and CrossFit workouts")
  System_Ext(browser, "Web Browser", "Stores data in IndexedDB")

  Rel(user, app, "Uses")
  Rel(app, browser, "Persists data to", "IndexedDB")
Container Diagram (Level 2)
C4Container
  title Container Diagram - Workout Tracker

  Person(user, "User", "Tracks workouts")

  Container_Boundary(app, "Workout Tracker PWA") {
    Container(spa, "SPA", "Vue 3, TypeScript", "Single-page application")
    Container(pinia, "State Management", "Pinia", "Manages application state")
    ContainerDb(indexeddb, "IndexedDB", "Dexie", "Local workout storage")
  }

  Rel(user, spa, "Uses")
  Rel(spa, pinia, "Reads/writes state")
  Rel(pinia, indexeddb, "Persists", "Dexie ORM")
Component Diagram (Level 3)
C4Component
  title Component Diagram - Workout Feature

  Container(views, "Views", "Vue Router pages")

  Container_Boundary(workout, "Workout Feature") {
    Component(useWorkout, "useWorkout", "Composable", "Workout execution state")
    Component(useTimer, "useTimer", "Composable", "Timer state machine")
    Component(workoutRepo, "WorkoutRepository", "Dexie", "Workout persistence")
  }

  Rel(views, useWorkout, "Uses")
  Rel(useWorkout, useTimer, "Controls")
  Rel(useWorkout, workoutRepo, "Saves to")
Dynamic Diagram (Request Flow)
C4Dynamic
  title Dynamic Diagram - User Sign In Flow

  ContainerDb(db, "Database", "PostgreSQL", "User credentials")
  Container(spa, "Single-Page App", "React", "Banking UI")

  Container_Boundary(api, "API Application") {
    Component(signIn, "Sign In Controller", "Express", "Auth endpoint")
    Component(security, "Security Service", "JWT", "Validates credentials")
  }

  Rel(spa, signIn, "1. Submit credentials", "JSON/HTTPS")
  Rel(signIn, security, "2. Validate")
  Rel(security, db, "3. Query user", "SQL")

  UpdateRelStyle(spa, signIn, $textColor="blue", $offsetY="-30")
Deployment Diagram
C4Deployment
  title Deployment Diagram - Production

  Deployment_Node(browser, "Customer Browser", "Chrome/Firefox") {
    Container(spa, "SPA", "React", "Web application")
  }

  Deployment_Node(aws, "AWS Cloud", "us-east-1") {
    Deployment_Node(ecs, "ECS Cluster", "Fargate") {
      Container(api, "API Service", "Node.js", "REST API")
    }
    Deployment_Node(rds, "RDS", "db.r5.large") {
      ContainerDb(db, "Database", "PostgreSQL", "Application data")
    }
  }

  Rel(spa, api, "API calls", "HTTPS")
  Rel(api, db, "Reads/writes", "JDBC")

Element Syntax

People and Systems
Person(alias, "Label", "Description")
Person_Ext(alias, "Label", "Description")       # External person
System(alias, "Label", "Description")
System_Ext(alias, "Label", "Description")       # External system
SystemDb(alias, "Label", "Description")         # Database system
SystemQueue(alias, "Label", "Description")      # Queue system
Containers
Container(alias, "Label", "Technology", "Description")
Container_Ext(alias, "Label", "Technology", "Description")
ContainerDb(alias, "Label", "Technology", "Description")
ContainerQueue(alias, "Label", "Technology", "Description")
Components
Component(alias, "Label", "Technology", "Description")
Component_Ext(alias, "Label", "Technology", "Description")
ComponentDb(alias, "Label", "Technology", "Description")
Boundaries
Enterprise_Boundary(alias, "Label") { ... }
System_Boundary(alias, "Label") { ... }
Container_Boundary(alias, "Label") { ... }
Boundary(alias, "Label", "type") { ... }
Relationships
Rel(from, to, "Label")
Rel(from, to, "Label", "Technology")
BiRel(from, to, "Label")                        # Bidirectional
Rel_U(from, to, "Label")                        # Upward
Rel_D(from, to, "Label")                        # Downward
Rel_L(from, to, "Label")                        # Leftward
Rel_R(from, to, "Label")                        # Rightward
Deployment Nodes
Deployment_Node(alias, "Label", "Type", "Description") { ... }
Node(alias, "Label", "Type", "Description") { ... }  # Shorthand

Styling and Layout

Layout Configuration
UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="1")
  • $c4ShapeInRow - Number of shapes per row (default: 4)
  • $c4BoundaryInRow - Number of boundaries per row (default: 2)
Element Styling
UpdateElementStyle(alias, $fontColor="red", $bgColor="grey", $borderColor="red")
Relationship Styling
UpdateRelStyle(from, to, $textColor="blue", $lineColor="blue", $offsetX="5", $offsetY="-10")

Use $offsetX and $offsetY to fix overlapping relationship labels.

Best Practices

Essential Rules
  1. Every element must have: Name, Type, Technology (where applicable), and Description
  2. Use unidirectional arrows only - Bidirectional arrows create ambiguity
  3. Label arrows with action verbs - "Sends email using", "Reads from", not just "uses"
  4. Include technology labels - "JSON/HTTPS", "JDBC", "gRPC"
  5. Stay under 20 elements per diagram - Split complex systems into multiple diagrams
Clarity Guidelines
  1. Start at Level 1 - Context diagrams help frame the system scope
  2. One diagram per file - Keep diagrams focused on a single abstraction level
  3. Meaningful aliases - Use descriptive aliases (e.g., orderService not s1)
  4. Concise descriptions - Keep descriptions under 50 characters when possible
  5. Always include a title - "System Context diagram for [System Name]"
What to Avoid

See references/common-mistakes.md for detailed anti-patterns:

  • Confusing containers (deployable) vs components (non-deployable)
  • Modeling shared libraries as containers
  • Showing message brokers as single containers instead of individual topics
  • Adding undefined abstraction levels like "subcomponents"
  • Removing type labels to "simplify" diagrams

Microservices Guidelines

Single Team Ownership

Model each microservice as a container (or container group):

C4Container
  title Microservices - Single Team

  System_Boundary(platform, "E-commerce Platform") {
    Container(orderApi, "Order Service", "Spring Boot", "Order processing")
    ContainerDb(orderDb, "Order DB", "PostgreSQL", "Order data")
    Container(inventoryApi, "Inventory Service", "Node.js", "Stock management")
    ContainerDb(inventoryDb, "Inventory DB", "MongoDB", "Stock data")
  }
Multi-Team Ownership

Promote microservices to software systems when owned by separate teams:

C4Context
  title Microservices - Multi-Team

  Person(customer, "Customer", "Places orders")
  System(orderSystem, "Order System", "Team Alpha")
  System(inventorySystem, "Inventory System", "Team Beta")
  System(paymentSystem, "Payment System", "Team Gamma")

  Rel(customer, orderSystem, "Places orders")
  Rel(orderSystem, inventorySystem, "Checks stock")
  Rel(orderSystem, paymentSystem, "Processes payment")
Event-Driven Architecture

Show individual topics/queues as containers, NOT a single "Kafka" box:

C4Container
  title Event-Driven Architecture

  Container(orderService, "Order Service", "Java", "Creates orders")
  Container(stockService, "Stock Service", "Java", "Manages inventory")
  ContainerQueue(orderTopic, "order.created", "Kafka", "Order events")
  ContainerQueue(stockTopic, "stock.reserved", "Kafka", "Stock events")

  Rel(orderService, orderTopic, "Publishes to")
  Rel(stockService, orderTopic, "Subscribes to")
  Rel(stockService, stockTopic, "Publishes to")
  Rel(orderService, stockTopic, "Subscribes to")

Output Location

Write architecture documentation to docs/architecture/ with naming convention:

  • c4-context.md - System context diagram
  • c4-containers.md - Container diagram
  • c4-components-{feature}.md - Component diagrams per feature
  • c4-deployment.md - Deployment diagram
  • c4-dynamic-{flow}.md - Dynamic diagrams for specific flows

Audience-Appropriate Detail

Audience Recommended Diagrams
Executives System Context only
Product Managers Context + Container
Architects Context + Container + key Components
Developers All levels as needed
DevOps Container + Deployment

References

1---
2name: c4-architecture
3description: Generate architecture documentation using C4 model Mermaid diagrams. Use when asked to create architecture diagrams, document system architecture, visualize software structure, create C4 diagrams, or generate context/container/component/deployment diagrams. Triggers include "architecture diagram", "C4 diagram", "system context", "container diagram", "component diagram", "deployment diagram", "document architecture", "visualize architecture".
4---
5 
6# C4 Architecture Documentation
7 
8Generate software architecture documentation using C4 model diagrams in Mermaid syntax.
9 
10## Workflow
11 
121. **Understand scope** - Determine which C4 level(s) are needed based on audience
132. **Analyze codebase** - Explore the system to identify components, containers, and relationships
143. **Generate diagrams** - Create Mermaid C4 diagrams at appropriate abstraction levels
154. **Document** - Write diagrams to markdown files with explanatory context
16 
17## C4 Diagram Levels
18 
19Select the appropriate level based on the documentation need:
20 
21| Level | Diagram Type | Audience | Shows | When to Create |
22|-------|-------------|----------|-------|----------------|
23| 1 | **C4Context** | Everyone | System + external actors | Always (required) |
24| 2 | **C4Container** | Technical | Apps, databases, services | Always (required) |
25| 3 | **C4Component** | Developers | Internal components | Only if adds value |
26| 4 | **C4Deployment** | DevOps | Infrastructure nodes | For production systems |
27| - | **C4Dynamic** | Technical | Request flows (numbered) | For complex workflows |
28 
29**Key Insight:** "Context + Container diagrams are sufficient for most software development teams." Only create Component/Code diagrams when they genuinely add value.
30 
31## Quick Start Examples
32 
33### System Context (Level 1)
34```mermaid
35C4Context
36 title System Context - Workout Tracker
37 
38 Person(user, "User", "Tracks workouts and exercises")
39 System(app, "Workout Tracker", "Vue PWA for tracking strength and CrossFit workouts")
40 System_Ext(browser, "Web Browser", "Stores data in IndexedDB")
41 
42 Rel(user, app, "Uses")
43 Rel(app, browser, "Persists data to", "IndexedDB")
44```
45 
46### Container Diagram (Level 2)
47```mermaid
48C4Container
49 title Container Diagram - Workout Tracker
50 
51 Person(user, "User", "Tracks workouts")
52 
53 Container_Boundary(app, "Workout Tracker PWA") {
54 Container(spa, "SPA", "Vue 3, TypeScript", "Single-page application")
55 Container(pinia, "State Management", "Pinia", "Manages application state")
56 ContainerDb(indexeddb, "IndexedDB", "Dexie", "Local workout storage")
57 }
58 
59 Rel(user, spa, "Uses")
60 Rel(spa, pinia, "Reads/writes state")
61 Rel(pinia, indexeddb, "Persists", "Dexie ORM")
62```
63 
64### Component Diagram (Level 3)
65```mermaid
66C4Component
67 title Component Diagram - Workout Feature
68 
69 Container(views, "Views", "Vue Router pages")
70 
71 Container_Boundary(workout, "Workout Feature") {
72 Component(useWorkout, "useWorkout", "Composable", "Workout execution state")
73 Component(useTimer, "useTimer", "Composable", "Timer state machine")
74 Component(workoutRepo, "WorkoutRepository", "Dexie", "Workout persistence")
75 }
76 
77 Rel(views, useWorkout, "Uses")
78 Rel(useWorkout, useTimer, "Controls")
79 Rel(useWorkout, workoutRepo, "Saves to")
80```
81 
82### Dynamic Diagram (Request Flow)
83```mermaid
84C4Dynamic
85 title Dynamic Diagram - User Sign In Flow
86 
87 ContainerDb(db, "Database", "PostgreSQL", "User credentials")
88 Container(spa, "Single-Page App", "React", "Banking UI")
89 
90 Container_Boundary(api, "API Application") {
91 Component(signIn, "Sign In Controller", "Express", "Auth endpoint")
92 Component(security, "Security Service", "JWT", "Validates credentials")
93 }
94 
95 Rel(spa, signIn, "1. Submit credentials", "JSON/HTTPS")
96 Rel(signIn, security, "2. Validate")
97 Rel(security, db, "3. Query user", "SQL")
98 
99 UpdateRelStyle(spa, signIn, $textColor="blue", $offsetY="-30")
100```
101 
102### Deployment Diagram
103```mermaid
104C4Deployment
105 title Deployment Diagram - Production
106 
107 Deployment_Node(browser, "Customer Browser", "Chrome/Firefox") {
108 Container(spa, "SPA", "React", "Web application")
109 }
110 
111 Deployment_Node(aws, "AWS Cloud", "us-east-1") {
112 Deployment_Node(ecs, "ECS Cluster", "Fargate") {
113 Container(api, "API Service", "Node.js", "REST API")
114 }
115 Deployment_Node(rds, "RDS", "db.r5.large") {
116 ContainerDb(db, "Database", "PostgreSQL", "Application data")
117 }
118 }
119 
120 Rel(spa, api, "API calls", "HTTPS")
121 Rel(api, db, "Reads/writes", "JDBC")
122```
123 
124## Element Syntax
125 
126### People and Systems
127```
128Person(alias, "Label", "Description")
129Person_Ext(alias, "Label", "Description") # External person
130System(alias, "Label", "Description")
131System_Ext(alias, "Label", "Description") # External system
132SystemDb(alias, "Label", "Description") # Database system
133SystemQueue(alias, "Label", "Description") # Queue system
134```
135 
136### Containers
137```
138Container(alias, "Label", "Technology", "Description")
139Container_Ext(alias, "Label", "Technology", "Description")
140ContainerDb(alias, "Label", "Technology", "Description")
141ContainerQueue(alias, "Label", "Technology", "Description")
142```
143 
144### Components
145```
146Component(alias, "Label", "Technology", "Description")
147Component_Ext(alias, "Label", "Technology", "Description")
148ComponentDb(alias, "Label", "Technology", "Description")
149```
150 
151### Boundaries
152```
153Enterprise_Boundary(alias, "Label") { ... }
154System_Boundary(alias, "Label") { ... }
155Container_Boundary(alias, "Label") { ... }
156Boundary(alias, "Label", "type") { ... }
157```
158 
159### Relationships
160```
161Rel(from, to, "Label")
162Rel(from, to, "Label", "Technology")
163BiRel(from, to, "Label") # Bidirectional
164Rel_U(from, to, "Label") # Upward
165Rel_D(from, to, "Label") # Downward
166Rel_L(from, to, "Label") # Leftward
167Rel_R(from, to, "Label") # Rightward
168```
169 
170### Deployment Nodes
171```
172Deployment_Node(alias, "Label", "Type", "Description") { ... }
173Node(alias, "Label", "Type", "Description") { ... } # Shorthand
174```
175 
176## Styling and Layout
177 
178### Layout Configuration
179```
180UpdateLayoutConfig($c4ShapeInRow="3", $c4BoundaryInRow="1")
181```
182- `$c4ShapeInRow` - Number of shapes per row (default: 4)
183- `$c4BoundaryInRow` - Number of boundaries per row (default: 2)
184 
185### Element Styling
186```
187UpdateElementStyle(alias, $fontColor="red", $bgColor="grey", $borderColor="red")
188```
189 
190### Relationship Styling
191```
192UpdateRelStyle(from, to, $textColor="blue", $lineColor="blue", $offsetX="5", $offsetY="-10")
193```
194Use `$offsetX` and `$offsetY` to fix overlapping relationship labels.
195 
196## Best Practices
197 
198### Essential Rules
199 
2001. **Every element must have**: Name, Type, Technology (where applicable), and Description
2012. **Use unidirectional arrows only** - Bidirectional arrows create ambiguity
2023. **Label arrows with action verbs** - "Sends email using", "Reads from", not just "uses"
2034. **Include technology labels** - "JSON/HTTPS", "JDBC", "gRPC"
2045. **Stay under 20 elements per diagram** - Split complex systems into multiple diagrams
205 
206### Clarity Guidelines
207 
2081. **Start at Level 1** - Context diagrams help frame the system scope
2092. **One diagram per file** - Keep diagrams focused on a single abstraction level
2103. **Meaningful aliases** - Use descriptive aliases (e.g., `orderService` not `s1`)
2114. **Concise descriptions** - Keep descriptions under 50 characters when possible
2125. **Always include a title** - "System Context diagram for [System Name]"
213 
214### What to Avoid
215 
216See [references/common-mistakes.md](references/common-mistakes.md) for detailed anti-patterns:
217- Confusing containers (deployable) vs components (non-deployable)
218- Modeling shared libraries as containers
219- Showing message brokers as single containers instead of individual topics
220- Adding undefined abstraction levels like "subcomponents"
221- Removing type labels to "simplify" diagrams
222 
223## Microservices Guidelines
224 
225### Single Team Ownership
226Model each microservice as a **container** (or container group):
227```mermaid
228C4Container
229 title Microservices - Single Team
230 
231 System_Boundary(platform, "E-commerce Platform") {
232 Container(orderApi, "Order Service", "Spring Boot", "Order processing")
233 ContainerDb(orderDb, "Order DB", "PostgreSQL", "Order data")
234 Container(inventoryApi, "Inventory Service", "Node.js", "Stock management")
235 ContainerDb(inventoryDb, "Inventory DB", "MongoDB", "Stock data")
236 }
237```
238 
239### Multi-Team Ownership
240Promote microservices to **software systems** when owned by separate teams:
241```mermaid
242C4Context
243 title Microservices - Multi-Team
244 
245 Person(customer, "Customer", "Places orders")
246 System(orderSystem, "Order System", "Team Alpha")
247 System(inventorySystem, "Inventory System", "Team Beta")
248 System(paymentSystem, "Payment System", "Team Gamma")
249 
250 Rel(customer, orderSystem, "Places orders")
251 Rel(orderSystem, inventorySystem, "Checks stock")
252 Rel(orderSystem, paymentSystem, "Processes payment")
253```
254 
255### Event-Driven Architecture
256Show individual topics/queues as containers, NOT a single "Kafka" box:
257```mermaid
258C4Container
259 title Event-Driven Architecture
260 
261 Container(orderService, "Order Service", "Java", "Creates orders")
262 Container(stockService, "Stock Service", "Java", "Manages inventory")
263 ContainerQueue(orderTopic, "order.created", "Kafka", "Order events")
264 ContainerQueue(stockTopic, "stock.reserved", "Kafka", "Stock events")
265 
266 Rel(orderService, orderTopic, "Publishes to")
267 Rel(stockService, orderTopic, "Subscribes to")
268 Rel(stockService, stockTopic, "Publishes to")
269 Rel(orderService, stockTopic, "Subscribes to")
270```
271 
272## Output Location
273 
274Write architecture documentation to `docs/architecture/` with naming convention:
275- `c4-context.md` - System context diagram
276- `c4-containers.md` - Container diagram
277- `c4-components-{feature}.md` - Component diagrams per feature
278- `c4-deployment.md` - Deployment diagram
279- `c4-dynamic-{flow}.md` - Dynamic diagrams for specific flows
280 
281## Audience-Appropriate Detail
282 
283| Audience | Recommended Diagrams |
284|----------|---------------------|
285| Executives | System Context only |
286| Product Managers | Context + Container |
287| Architects | Context + Container + key Components |
288| Developers | All levels as needed |
289| DevOps | Container + Deployment |
290 
291## References
292 
293- [references/c4-syntax.md](references/c4-syntax.md) - Complete Mermaid C4 syntax
294- [references/common-mistakes.md](references/common-mistakes.md) - Anti-patterns to avoid
295- [references/advanced-patterns.md](references/advanced-patterns.md) - Microservices, event-driven, deployment
296 

Discussion

Alternatives