C4 Architecture

Tutorial

We model a small online bookshop, one C4 level at a time. Each step is a complete, valid file; the diagrams below are what render produces from it.

Install the runtime first (see Command line), then create a file bookshop.json. Add "$schema" with the path from your file to schema/c4.schema.json to get completion and validation in your editor. The sources below say ../../schema/c4.schema.json because they live in site/models/ of the repository.

1. System context

Start with the big picture: who uses the system, and which other systems it depends on. Nothing technical yet.

site/models/tutorial-1-context.json
{
  "$schema": "../../schema/c4.schema.json",
  "version": 1,
  "name": "Bookshop",
  "description": "Online shop for an independent bookstore",
  "model": {
    "people": [
      { "id": "customer", "name": "Customer", "description": "Buys books online", "external": true }
    ],
    "softwareSystems": [
      { "id": "shop", "name": "Bookshop", "description": "Catalogue, cart and orders" },
      { "id": "payments", "name": "Payment provider", "description": "Card payments", "external": true }
    ],
    "relationships": [
      { "from": "customer", "to": "shop", "description": "Browses and buys books using" },
      { "from": "shop", "to": "payments", "description": "Charges cards using" }
    ]
  },
  "views": [
    { "type": "systemContext", "key": "context", "scope": "shop", "title": "Bookshop - system context" }
  ]
}

Check and render it:

node src/cli.ts validate bookshop.json
node src/cli.ts render bookshop.json bookshop.html
CustomerCustomer[Person]Buys books onlineBookshopBookshop[Software System]Catalogue, cart and ordersPayment providerPayment provider[Software System]Card paymentsBrowses and buys booksusingCharges cards usingPersonSoftware SystemExternalRelationship

2. Containers

Zoom into the bookshop: what runs and is deployed separately. Containers are nested in their software system, and get a technology.

JSON source: site/models/tutorial-2-containers.json
{
  "$schema": "../../schema/c4.schema.json",
  "version": 1,
  "name": "Bookshop",
  "description": "Online shop for an independent bookstore",
  "model": {
    "people": [
      { "id": "customer", "name": "Customer", "description": "Buys books online", "external": true }
    ],
    "softwareSystems": [
      {
        "id": "shop",
        "name": "Bookshop",
        "description": "Catalogue, cart and orders",
        "containers": [
          { "id": "web", "name": "Web shop", "description": "Storefront and cart", "technology": "Next.js", "tags": ["Browser"] },
          { "id": "api", "name": "Order API", "description": "Takes and tracks orders", "technology": "Go" },
          { "id": "db", "name": "Order database", "description": "Orders and stock", "technology": "PostgreSQL", "tags": ["Database"] }
        ]
      },
      { "id": "payments", "name": "Payment provider", "description": "Card payments", "external": true }
    ],
    "relationships": [
      { "from": "customer", "to": "web", "description": "Browses and buys books using", "technology": "HTTPS" },
      { "from": "web", "to": "api", "description": "Places orders using", "technology": "HTTPS/JSON" },
      { "from": "api", "to": "db", "description": "Reads and writes orders in", "technology": "SQL" },
      { "from": "api", "to": "payments", "description": "Charges cards using", "technology": "HTTPS/JSON" }
    ]
  },
  "views": [
    { "type": "systemContext", "key": "context", "scope": "shop", "title": "Bookshop - system context" },
    { "type": "container", "key": "containers", "scope": "shop", "title": "Bookshop - containers" }
  ],
  "styles": {
    "elements": [
      { "tag": "Database", "shape": "cylinder" },
      { "tag": "Browser", "shape": "webBrowser" }
    ]
  }
}

What changed:

Bookshop[Software System]CustomerCustomer[Person]Buys books onlineWeb shopWeb shop[Container: Next.js]Storefront and cartOrder APIOrder API[Container: Go]Takes and tracks ordersOrder databaseOrder database[Container: PostgreSQL]Orders and stockPayment providerPayment provider[Software System]Card paymentsBrowses and buys booksusing[HTTPS]Places orders using[HTTPS/JSON]Reads and writes orders in[SQL]Charges cards using[HTTPS/JSON]PersonSoftware SystemContainerBrowserDatabaseExternalRelationship

The context view of the same file is unchanged, even though no relationship targets shop any more:

CustomerCustomer[Person]Buys books onlineBookshopBookshop[Software System]Catalogue, cart and ordersPayment providerPayment provider[Software System]Card paymentsBrowses and buys booksusing[HTTPS]Charges cards using[HTTPS/JSON]PersonSoftware SystemExternalRelationship

3. Components

Zoom into one container only where it helps. Here, the Order API is split into three components.

JSON source: site/models/tutorial-3-components.json
{
  "$schema": "../../schema/c4.schema.json",
  "version": 1,
  "name": "Bookshop",
  "description": "Online shop for an independent bookstore",
  "model": {
    "people": [
      { "id": "customer", "name": "Customer", "description": "Buys books online", "external": true }
    ],
    "softwareSystems": [
      {
        "id": "shop",
        "name": "Bookshop",
        "description": "Catalogue, cart and orders",
        "containers": [
          { "id": "web", "name": "Web shop", "description": "Storefront and cart", "technology": "Next.js", "tags": ["Browser"] },
          {
            "id": "api",
            "name": "Order API",
            "description": "Takes and tracks orders",
            "technology": "Go",
            "components": [
              { "id": "orderHandler", "name": "Order handler", "description": "HTTP endpoints for orders", "technology": "net/http" },
              { "id": "checkout", "name": "Checkout", "description": "Prices the cart and takes the payment", "technology": "Go package" },
              { "id": "orderStore", "name": "Order store", "description": "Persists orders", "technology": "pgx" }
            ]
          },
          { "id": "db", "name": "Order database", "description": "Orders and stock", "technology": "PostgreSQL", "tags": ["Database"] }
        ]
      },
      { "id": "payments", "name": "Payment provider", "description": "Card payments", "external": true }
    ],
    "relationships": [
      { "from": "customer", "to": "web", "description": "Browses and buys books using", "technology": "HTTPS" },
      { "from": "web", "to": "orderHandler", "description": "Places orders using", "technology": "HTTPS/JSON" },
      { "from": "orderHandler", "to": "checkout", "description": "Checks out the cart with" },
      { "from": "checkout", "to": "orderStore", "description": "Saves the paid order with" },
      { "from": "checkout", "to": "payments", "description": "Charges cards using", "technology": "HTTPS/JSON" },
      { "from": "orderStore", "to": "db", "description": "Reads and writes orders in", "technology": "SQL" }
    ]
  },
  "views": [
    { "type": "systemContext", "key": "context", "scope": "shop", "title": "Bookshop - system context" },
    { "type": "container", "key": "containers", "scope": "shop", "title": "Bookshop - containers" },
    { "type": "component", "key": "components", "scope": "api", "title": "Order API - components" }
  ],
  "styles": {
    "elements": [
      { "tag": "Database", "shape": "cylinder" },
      { "tag": "Browser", "shape": "webBrowser" }
    ]
  }
}
Order API[Container]Web shopWeb shop[Container: Next.js]Storefront and cartOrder handlerOrder handler[Component: net/http]HTTP endpoints for ordersCheckoutCheckout[Component: Go package]Prices the cart and takes thepaymentOrder storeOrder store[Component: pgx]Persists ordersOrder databaseOrder database[Container: PostgreSQL]Orders and stockPayment providerPayment provider[Software System]Card paymentsPlaces orders using[HTTPS/JSON]Checks out the cart withSaves the paid order withCharges cards using[HTTPS/JSON]Reads and writes orders in[SQL]Software SystemContainerComponentBrowserDatabaseExternalRelationship

4. A dynamic view

Static views show what can talk to what. A dynamic view tells one story: the numbered steps of a scenario.

{
  "type": "dynamic",
  "key": "placeOrder",
  "scope": "shop",
  "title": "Place an order",
  "description": "What happens when a customer checks out.",
  "steps": [
    { "from": "customer", "to": "web", "description": "Submits the cart" },
    { "from": "web", "to": "api", "description": "Creates the order" },
    { "from": "api", "to": "payments", "description": "Charges the card" },
    { "from": "api", "to": "db", "description": "Stores the paid order" }
  ]
}
Bookshop[Software System]CustomerCustomer[Person]Buys books onlineWeb shopWeb shop[Container: Next.js]Storefront and cartOrder APIOrder API[Container: Go]Takes and tracks ordersPayment providerPayment provider[Software System]Card paymentsOrder databaseOrder database[Container: PostgreSQL]Orders and stock1. Submits the cart[HTTPS]2. Creates the order[HTTPS/JSON]3. Charges the card[HTTPS/JSON]4. Stores the paid order[SQL]PersonSoftware SystemContainerBrowserDatabaseExternalRelationship

5. Review before you share

node src/cli.ts inspect bookshop.json

inspect lists what each view shows and warns about modelling gaps: missing descriptions or technologies, elements in no view, crowded views. All the files of this tutorial pass without warnings.

The final file, with all four views:

JSON source: site/models/tutorial-4-dynamic.json
{
  "$schema": "../../schema/c4.schema.json",
  "version": 1,
  "name": "Bookshop",
  "description": "Online shop for an independent bookstore",
  "model": {
    "people": [
      {
        "id": "customer",
        "name": "Customer",
        "description": "Buys books online",
        "external": true
      }
    ],
    "softwareSystems": [
      {
        "id": "shop",
        "name": "Bookshop",
        "description": "Catalogue, cart and orders",
        "containers": [
          {
            "id": "web",
            "name": "Web shop",
            "description": "Storefront and cart",
            "technology": "Next.js",
            "tags": [
              "Browser"
            ]
          },
          {
            "id": "api",
            "name": "Order API",
            "description": "Takes and tracks orders",
            "technology": "Go",
            "components": [
              {
                "id": "orderHandler",
                "name": "Order handler",
                "description": "HTTP endpoints for orders",
                "technology": "net/http"
              },
              {
                "id": "checkout",
                "name": "Checkout",
                "description": "Prices the cart and takes the payment",
                "technology": "Go package"
              },
              {
                "id": "orderStore",
                "name": "Order store",
                "description": "Persists orders",
                "technology": "pgx"
              }
            ]
          },
          {
            "id": "db",
            "name": "Order database",
            "description": "Orders and stock",
            "technology": "PostgreSQL",
            "tags": [
              "Database"
            ]
          }
        ]
      },
      {
        "id": "payments",
        "name": "Payment provider",
        "description": "Card payments",
        "external": true
      }
    ],
    "relationships": [
      {
        "from": "customer",
        "to": "web",
        "description": "Browses and buys books using",
        "technology": "HTTPS"
      },
      {
        "from": "web",
        "to": "orderHandler",
        "description": "Places orders using",
        "technology": "HTTPS/JSON"
      },
      {
        "from": "orderHandler",
        "to": "checkout",
        "description": "Checks out the cart with"
      },
      {
        "from": "checkout",
        "to": "orderStore",
        "description": "Saves the paid order with"
      },
      {
        "from": "checkout",
        "to": "payments",
        "description": "Charges cards using",
        "technology": "HTTPS/JSON"
      },
      {
        "from": "orderStore",
        "to": "db",
        "description": "Reads and writes orders in",
        "technology": "SQL"
      }
    ]
  },
  "views": [
    {
      "type": "systemContext",
      "key": "context",
      "scope": "shop",
      "title": "Bookshop - system context"
    },
    {
      "type": "container",
      "key": "containers",
      "scope": "shop",
      "title": "Bookshop - containers"
    },
    {
      "type": "component",
      "key": "components",
      "scope": "api",
      "title": "Order API - components"
    },
    {
      "type": "dynamic",
      "key": "placeOrder",
      "scope": "shop",
      "title": "Place an order",
      "description": "What happens when a customer checks out.",
      "steps": [
        {
          "from": "customer",
          "to": "web",
          "description": "Submits the cart"
        },
        {
          "from": "web",
          "to": "api",
          "description": "Creates the order"
        },
        {
          "from": "api",
          "to": "payments",
          "description": "Charges the card"
        },
        {
          "from": "api",
          "to": "db",
          "description": "Stores the paid order"
        }
      ]
    }
  ],
  "styles": {
    "elements": [
      {
        "tag": "Database",
        "shape": "cylinder"
      },
      {
        "tag": "Browser",
        "shape": "webBrowser"
      }
    ]
  }
}

Next steps