C4 Architecture Model

Introduction

The C4 model is a framework for visualizing the architecture of software systems. It provides a hierarchical approach to modeling, allowing you to create diagrams at different levels of abstraction. The C4 model consists of four main diagram types:

  1. Context Diagram: Shows the system’s context and its interactions with external entities.
  2. Container Diagram: Breaks down the system into containers (applications or services) and their relationships.
  3. Component Diagram: Details the internal structure of a container, showing its components and their interactions.
  4. Code Diagram: Focuses on the code structure within a component, such as classes or modules.

Advantages

Structure

The C4 model uses a specific structure for its diagrams, which includes:

Docs and tools

The c4 model documentation is available at c4model.com . It provides detailed guidelines on how to create C4 diagrams, including examples and best practices.

To create a c4 model diagram, you can use various tools such as:

The following examples uses Structurizr DSL , which is a domain-specific language for defining C4 models. It allows you to create C4 diagrams using a simple text format.

Example code

The following code represents a simple C4 model for a webshop system with a user, a frontend, a backend, and a database. It includes identifiers, models, views, and styles.

(Scroll down to see the generated diagram.)

workspace "Webshop Architecture" "Architecture model for an e-commerce webshop system" {

    !identifiers hierarchical
    
    model {
        # People
        u = person "Customer" "A person who wants to purchase products online"
        admin = person "Administrator" "Staff member who manages products and orders"
        
        # Software System
        sws = softwareSystem "Webshop System" "E-commerce platform for online shopping" {
            
            # Frontend Container
            webShopFrontend = container "Webshop Frontend" "Single-page application providing the user interface" "React, TypeScript" {
                productCatalog = component "Product Catalog" "Shows available products with filtering and search" "React Components"
                shoppingCart = component "Shopping Cart" "Manages items selected for purchase" "React, Redux"
                checkout = component "Checkout Flow" "Handles payment and order completion" "React, Payment API"
                userAccount = component "User Account Panel" "Manages user profile and order history" "React Components"
            }
            
            # Backend Container
            webShopBackend = container "Webshop Backend" "REST API handling business logic and data access" "Spring Boot, Java" {
                productService = component "Product Service" "Manages product catalog and inventory" "Spring @RestController"
                orderService = component "Order Processing Service" "Handles order creation and management" "Spring @Service"
                authService = component "Authentication Service" "Handles user authentication and authorization" "Spring Security"
                paymentGateway = component "Payment Gateway Connector" "Interfaces with external payment processors" "Java HTTP Client"
            }
            
            # Database Container
            db = container "Database" "Stores product catalog, user data, and order information" "PostgreSQL" {
                tags "Database"
            }
        }
        
        # External Systems
        paymentSystem = softwareSystem "Payment Processor" "External payment processing service" "External System"
        
        # Relationships
        u -> sws "Uses"
        u -> sws.webShopFrontend "Accesses website via browser"
        admin -> sws "Manages"
        
        sws.webShopFrontend.productCatalog -> sws.webShopBackend.productService "Fetches product data from" "JSON/HTTPS"
        sws.webShopFrontend.shoppingCart -> sws.webShopBackend.productService "Checks product availability" "JSON/HTTPS"
        sws.webShopFrontend.checkout -> sws.webShopBackend.orderService "Creates orders via" "JSON/HTTPS"
        sws.webShopFrontend.checkout -> sws.webShopBackend.paymentGateway "Processes payments via" "JSON/HTTPS"
        sws.webShopFrontend.userAccount -> sws.webShopBackend.authService "Authenticates using" "JWT/HTTPS"
        
        sws.webShopBackend.productService -> sws.db "Reads/writes product data to" "JPA/JDBC"
        sws.webShopBackend.orderService -> sws.db "Stores orders in" "JPA/JDBC"
        sws.webShopBackend.authService -> sws.db "Reads user credentials from" "JPA/JDBC"
        sws.webShopBackend.paymentGateway -> paymentSystem "Processes payments using" "REST/HTTPS"
    }
    
    views {
        # System Context View
        systemContext sws "SystemContext" "Overview of the webshop system and its users" {
            include *
            autolayout lr
        }
        
        # Container View
        container sws "Containers" "The containers that make up the webshop system" {
            include *
            autolayout lr
        }
        
        # Component Views
        component sws.webShopFrontend "FrontendComponents" "Components within the webshop frontend" {
            include *
            autolayout tb
        }
        
        component sws.webShopBackend "BackendComponents" "Components within the webshop backend" {
            include *
            autolayout tb
        }
        
        styles {
            element "Element" {
                color white
            }
            element "Person" {
                background #116611
                shape person
            }
            element "Software System" {
                background #2D882D
            }
            element "Container" {
                background #55AA55
            }
            element "Component" {
                background #77CC77
            }
            element "Code Element" {
                background #99EE99
                shape Box
            }
            element "Database" {
                shape cylinder
            }
            element "External System" {
                background #999999
            }
        }
    }
}

Output

This code generates the following C4 diagrams. (You can double-click on the elements to zoom into containers and zoom out by “navigate back”.)

Visit in separate tab: C4 Model Example

(Generated by structurizr-cli )

Conclusion

The C4 model is a powerful tool for visualizing and documenting software architecture. By breaking down complex systems into manageable components, it helps teams understand and communicate their designs effectively. Whether you’re working on a small project or a large enterprise application, the C4 model can provide clarity and structure to your architecture discussions.

However, it should not be forgotten that the initial outlay is high but will be worthwhile.