Introducing a new way to review your PRs.

Read the announcement

basecamp / kamal-proxy

Lightweight proxy server for Kamal

View on GitHub

15 documents · Updated Nov 18, 2026

docs / service-and-target-lifecycle

Services and upstream targets

A service is the proxy's named unit of traffic ownership. It binds incoming host and path rules to one active group of upstream targets and may also hold a separate rollout group. The service registry keeps these bindings available to the router while target health and draining determine which upstream instances can receive traffic.

Service registry

The service registry maps service names to routing-ready services. Each service describes the host and path space it owns, while the registry builds the lookup view used to select the longest matching path for a request.

Services can use an exact host, a wildcard subdomain, or an empty host as the default route. An empty path list means the root path. The registry rejects overlapping host and path ownership when a service is installed, so one request maps to one service before target selection begins.

References: internal/…/service_map.go• L21-78 internal/…/service_map.go• L81-166 internal/…/router.go• L151-176

Service configuration

Service configuration defines how a service is reached and how its requests are prepared for its targets. It covers host and path bindings plus service-wide TLS, prefix, client address, metrics, and writer-affinity behavior; target configuration is kept alongside it so all targets in a group share the same upstream rules.

The configuration is normalized before it enters the registry. TLS settings must belong to a root-path service, and a canonical host must be one of the service hosts. A path-only service can inherit TLS behavior from the root service for the same host.

Configuration groupPurpose
Hosts and path prefixesSelect the service for incoming traffic.
TLS and canonical hostSecure traffic and choose the preferred public host.
Prefix and client-IP handlingShape the request seen by the target.
Metrics exclusions and writer affinityControl service-level request accounting and response stickiness.

References: internal/…/service.go• L87-179 internal/…/service_map.go• L169-215 internal/…/router.go• L151-166

Target lifecycle

An upstream target is one address that can fulfill requests for a service. The router creates target groups for active traffic or rollout traffic, waits for the group to become ready, and replaces old groups only after the new group is installed.

The lifecycle moves through these stages:

  1. The target is created in the adding state after its address and shared target settings are accepted.
  2. Health checks run until the target proves that its health endpoint returns a successful response.
  3. A healthy target enters the eligible pool used by the load balancer.
  4. A replaced or removed target enters draining; new requests stop while in-flight work gets time to finish.
  5. Health checks stop when the target group is disposed.

This diagram shows how deployment turns target addresses into an eligible request pool and then retires the old pool.

flowchart LR
    A[Target addresses] --> B[Target group]
    B --> C[Health checks]
    C -->|all ready| D[Eligible targets]
    D --> E[Proxy requests]
    D --> F[Replacement or removal]
    F --> G[Drain in-flight requests]
    G --> H[Dispose health checks]

References: internal/…/target.go• L30-37 internal/…/target.go• L105-130 internal/…/router.go• L151-176 internal/…/router.go• L363-379

Target health

Target health is the gate between an address and request eligibility. A health checker sends a GET request to the configured endpoint at a fixed interval and marks the target healthy only for a response in the 2xx range; connection errors, timeouts, and other status codes count as failures.

The target reports state changes to its load-balancing group. A failed check moves a healthy target to unhealthy, and a later successful check makes it healthy again. A new target stays adding until its first successful check. The load balancer rebuilds its writer and reader pools from healthy targets, so an unhealthy target receives no new traffic.

StateRequest role
addingWaiting for its first successful health check.
healthyEligible for selection according to its writer or reader role.
unhealthyExcluded until a later check succeeds.
drainingRejects new work while existing work is retired.

References: internal/…/health_check.go• L23-25 internal/…/health_check.go• L62-123 internal/…/target.go• L218-275 internal/…/load_balancer.go• L241-271

Target draining

Draining provides a safe boundary for deployment and removal. The target first blocks new requests, then waits for current requests to finish until the drain limit; long-lived hijacked connections and work that remains at the deadline are cancelled.

The same retirement rule applies when an active group is replaced or when a rollout group is removed. A service stop also drains its target groups before it returns. This lets the router change the target set without sending new traffic to an old address while still giving normal requests time to complete.

References: internal/…/target.go• L157-215 internal/…/load_balancer.go• L156-171 internal/…/service.go• L448-517 internal/…/router.go• L164-174 internal/…/router.go• L242-258

Target configuration

Target configuration controls the shared contract between the proxy and every upstream address in a target group. It sets the health endpoint and timing, response timeout, optional request and response buffering, body limits, header forwarding and logging, and cookie path scoping.

Health checks use the target address unless a separate health-check port is set. They can also send a configured host value, which supports upstreams that serve health endpoints through host-based routing. Buffering enables the related request or response size limit; without buffering, those limits are not enforced by that buffering path.

Target settingSystem effect
Health path, port, host, interval, timeoutDefines how upstream readiness is measured.
Response timeoutLimits how long the proxy waits for upstream response headers.
Request or response buffering and limitsControls body buffering and size checks.
Forward headersDecides whether trusted incoming forwarding values continue upstream.
Cookie path scopingKeeps upstream cookies within the routed service path when enabled.

References: internal/…/target.go• L64-89 internal/…/target.go• L279-304 internal/…/target.go• L344-359 internal/…/target.go• L465-475

Active and rollout target groups

Each service can hold an active target group and one rollout target group. The active group serves normal traffic. The rollout group is a parallel candidate group whose use is controlled by rollout rules described in Traffic pause and rollout controls.

Installing a new group starts its health checks and normally waits for all targets to become healthy before the group is exposed. The old group is then disposed and drained. The Service deployment workflow uses this boundary when it changes target groups. State snapshots preserve service options and target addresses so a restart can rebuild the groups and restore them as ready targets.

The target group also distinguishes writable targets from read-only targets. That role affects which requests can use an address and is covered in Upstream load balancing. The Service operations commands expose the related runtime actions for service and rollout state.

References: internal/…/service.go• L200-312 internal/…/service.go• L382-446 internal/…/router.go• L180-205 internal/…/router.go• L407-414

Change checklist

When these files or sections are changed, remember to consider these:

  • Keep service host and path conflict checks aligned with the normalized values used for request lookup.
  • Keep the health-check path and prefix-stripping rules aligned so the probe tests the path the target actually sees.
  • Keep target state changes connected to the load balancer's eligible pools. A state update without a pool refresh can send traffic to a target that is no longer healthy.
  • When changing target-group replacement, preserve the order that installs the new group before disposing and draining the old group.
  • When changing saved service state, preserve active and rollout target roles so restart does not rebuild the wrong traffic groups.

Important failure modes

TriggerConsequenceGuard
A target never returns a 2xx health response before the deployment timeoutThe new group is not installed and deployment fails.Set a reachable health endpoint and allow a suitable deployment timeout.
All targets in a group become unhealthyThe load balancer has no eligible target and returns service unavailable.Keep health checks valid and monitor target health.
A target group is replaced while requests remain activeOld requests can be cancelled when the drain limit expires.Use a drain period that matches the request and connection lifetime.
Service host and path bindings overlapThe new service is rejected to avoid ambiguous routing.Give each service a unique normalized host and path binding.
Health-check host or port does not match the upstream listenerThe target stays unhealthy even when its normal traffic address is reachable.Configure the health-check host and port for the upstream health route.

Key files

FileWhy to read it
internal/…/service_map.goShows how named services become host and path routing bindings.
internal/…/service.goShows service configuration, active and rollout groups, and service-level retirement.
internal/…/router.goShows deployment, group installation, replacement, and service removal.
internal/…/target.goShows target states, health-check ownership, request admission, and draining.
internal/…/health_check.goShows the health probe contract and success criteria.
internal/…/load_balancer.goShows how health state turns into eligible writer and reader pools.