# basecamp/kamal-proxy architecture

> Architecture diagram of the GitHub repository basecamp/kamal-proxy, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-06-21). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/basecamp/kamal-proxy
- Repository: https://github.com/basecamp/kamal-proxy

## Overview

##### Project Type and Purpose
- This is a Go-based CLI tool plus HTTP proxy service (“Kamal Proxy”) designed to coordinate zero-downtime deployments.
- The project consists of two logical tiers:
– Control Plane (CLI commands)
– Data Plane (long-running HTTP proxy server)

##### High-Level File Structure Analysis
- cmd/kamal-proxy/main.go
– Entry point for the CLI binary. Parses flags/subcommands.
- internal/cmd
– Implements each CLI subcommand (run, deploy, remove, rollback, pause, resume, list, etc.).
– These commands invoke the server’s control API.
- internal/server
– Core HTTP proxy implementation: server.go, router.go, middleware, load_balancer, service_map, controllers (rollout, pause), TLS cert management, error pages.
– Exposes two APIs:
a. Control API (for CLI commands)
b. Proxy API (public-facing HTTP endpoints for application traffic)
- internal/metrics
– Collects and formats Prometheus-style metrics.
- internal/pages
– Embedded HTML templates for error pages (404, 502, etc.).
- example/
– Docker Compose setup demonstrating how to run Kamal Proxy and upstream services.

##### Key Architectural Insights from the README
- CLI “kamal-proxy run” launches the HTTP proxy server with default ports and options (overridable via flags or env vars).
- CLI “deploy/remove/pause/rollout” commands invoke health checks, update the in-memory service map, and orchestrate traffic shifting with zero downtime.
- Data plane: HTTP traffic is received on one or more ports, passed through a chain of middleware (logging, request buffering, request ID), routed by host/path to the appropriate service, passed through a load balancer to healthy targets, and proxied.
- Health checking runs in the background for each deployed target.
- Metrics are exposed for Prometheus and include controller metrics, latency, error rates.
- TLS management (auto and manual) is layered onto the proxy.

##### Main Components
A. Control Plane
1. CLI Binary (kamal-proxy)
2. Control API Server (internal/server/commands.go) – listens on Unix socket or HTTP port
3. Service Map & Controllers
- RolloutController – orchestrates zero-downtime swap of old→new
- PauseController – pauses/resumes services
4. Health Checker – polls targets and updates their status in the Service Map
B. Data Plane
1. HTTP Server
2. Middleware Chain
- RequestStart → RequestID → Logging → Buffering → ResponseBuffering → ErrorPages
3. Router
- Host-based routing
- Path-based routing (with optional prefix stripping)
4. Load Balancer – picks from healthy targets in service map
5. TLS Layer – automatic or manual certificate loader (via cert.go)
6. Error Page Templates (embedded in binary)
C. Observability
1. Metrics Collector (internal/metrics)
2. /metrics endpoint for Prometheus
D. External Services
1. Upstream Application Instances
2. Prometheus (for scraping metrics)
3. Docker / Kubernetes (deployment environments)

##### Relationships and Data Flows
CLI → Control API → Controllers → Service Map → Health Checker
Client HTTP Request → TLS Layer → Middleware Chain → Router → Load Balancer → Upstream Target → Response back through middleware → Client
- Metrics Flow: Server internals → Metrics Collector → /metrics → Prometheus

##### Architectural Patterns and Design Principles
- Separation of Concerns: clear split between Control Plane (CLI + API) and Data Plane (proxying).
- Middleware Pattern: each cross-cutting concern (logging, buffering, request IDs) is a standalone HTTP middleware.
- Controller Pattern: orchestrators (rollout/pause) manage in-memory state.
- Embedded Resources: error pages and static assets are compiled into the binary.

##### Putting It All Together
4. Host- and path-based routing as sub-components of the Router.
5. Zero-downtime flow: the RolloutController updates Service Map entries only after health checks pass, and Load Balancer shifts traffic seamlessly.

## Diagram (Mermaid)

```mermaid
graph TB
    %% Control Plane
    subgraph "Control Plane"
        CLI["CLI Binary"]:::cli
        ControlAPI["Control API Server"]:::server
        ServiceMap["Service Map (in-memory)"]:::db
        Rollout["RolloutController"]:::ctrl
        Pause["PauseController"]:::ctrl
        Health["Health Checker"]:::ctrl
    end

    %% Data Plane
    subgraph "Data Plane"
        HTTPServer["HTTP Server\n(Listener & Bootstrap)"]:::server
        TLS["TLS Layer"]:::server
        subgraph "Middleware Chain"
            ReqStart["RequestStart Middleware"]:::server
            ReqID["RequestID Middleware"]:::server
            Logging["Logging Middleware"]:::server
            ReqBuf["Request Buffering Middleware"]:::server
            RespBuf["Response Buffering Middleware"]:::server
            ErrPages["Error Pages Middleware"]:::server
        end
        Router["Router\n(Host & Path)"]:::server
        LoadBalancer["Load Balancer"]:::server
        Upstream["Upstream Targets"]:::external
        MetricsComp["Metrics Collector & /metrics"]:::metrics
    end

    %% External Services
    Prometheus["Prometheus"]:::external
    DockerK8s["Docker/Kubernetes"]:::external

    %% Control Flow (dashed)
    CLI ---|control flow| ControlAPI
    ControlAPI ---|control flow| Rollout
    ControlAPI ---|control flow| Pause
    ControlAPI ---|control flow| Health
    Rollout ---|control flow| ServiceMap
    Pause ---|control flow| ServiceMap
    Health ---|control flow| ServiceMap

    %% Data Flow (solid)
    HTTPServer --> TLS
    TLS --> ReqStart
    ReqStart --> ReqID
    ReqID --> Logging
    Logging --> ReqBuf
    ReqBuf --> RespBuf
    RespBuf --> ErrPages
    ErrPages --> Router
    Router --> LoadBalancer
    LoadBalancer --> Upstream
    Upstream -->|response| Router
    Router -->|response| ErrPages
    ErrPages -->|response| RespBuf
    RespBuf -->|response| ReqBuf
    ReqBuf -->|response| Logging
    Logging -->|response| ReqID
    ReqID -->|response| ReqStart
    ReqStart -->|response| TLS
    TLS -->|response| HTTPServer

    %% Metrics Flow
    HTTPServer --> MetricsComp
    MetricsComp --> Prometheus

    %% Control Plane to Data Plane annotation
    ControlAPI ---|deploy via Docker/K8s| DockerK8s

    %% Click Events
    click CLI "https://github.com/basecamp/kamal-proxy/blob/main/cmd/kamal-proxy/main.go"
    click ControlAPI "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/commands.go"
    click ServiceMap "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/service_map.go"
    click Rollout "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/rollout_controller.go"
    click Pause "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/pause_controller.go"
    click Health "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/health_check.go"
    click HTTPServer "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/server.go"
    click Router "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/router.go"
    click ReqStart "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/request_start_middleware.go"
    click ReqID "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/request_id_middleware.go"
    click Logging "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/logging_middleware.go"
    click ReqBuf "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/request_buffer_middleware.go"
    click RespBuf "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/response_buffering_middleware.go"
    click ErrPages "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/error_page_middleware.go"
    click LoadBalancer "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/load_balancer.go"
    click TLS "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/cert.go"
    click ServiceMap "https://github.com/basecamp/kamal-proxy/blob/main/internal/server/service_map.go"
    click MetricsComp "https://github.com/basecamp/kamal-proxy/blob/main/internal/metrics/metrics.go"
    click DockerK8s "https://github.com/basecamp/kamal-proxy/blob/main/example/docker-compose.yml"
    click DockerK8s "https://github.com/basecamp/kamal-proxy/blob/main/example/prometheus.yml"
    click Upstream "https://github.com/basecamp/kamal-proxy/blob/main/example/upstream/main.go"

    %% Styles
    classDef server fill:#D0E8FF,stroke:#037BFC,color:#000
    classDef cli fill:#DFF3D0,stroke:#4CAF50,color:#000
    classDef ctrl fill:#E8F5D0,stroke:#8BC34A,color:#000
    classDef db fill:#FFE8B0,stroke:#FB8C00,color:#000
    classDef metrics fill:#F3D0FF,stroke:#9C27B0,color:#000
    classDef external fill:#F0F0F0,stroke:#999,color:#000
```
