Rollout Policy Experimental

What every generated Swarm service contains, how web and worker services roll out, and why.

velyr-pack compose writes every service with the same rules, so a deploy behaves the same for every service of the same kind. There are two kinds: web (the process has health) and worker (everything else).

WebWorker
Update orderstart-first: the new task starts and must be healthy before the old one stopsstop-first: the old task stops before the new one starts
Healthcheckcurl -fsS http://localhost:<port><path>, every 10s, timeout 5s, 3 retries, start_period from the manifestnone
Monitor windowstart_period (default 60s)30s
stop_grace_periodprocess or service grace, default 45sprocess or service grace; Swarm's default (10s) if unset
On update failureroll back, same orderroll back, same order
If the rollback failspausepause

Both kinds update one task at a time (parallelism: 1), run in replicated mode and get init: true and resource limits.

A web service, line by line#

toml
[processes.web]
command = ["php", "artisan", "octane:start", "--server=frankenphp", "--host=0.0.0.0", "--port=8000"]
health = { path = "/up", port = 8000, start_period = "60s" }
yaml
  web:
    <<: *app
    init: true                                   # Docker's init is PID 1: forwards signals, reaps zombies
    entrypoint: ["/cnb/process/web"]
    healthcheck:
      test: ["CMD", "curl", "-fsS", "http://localhost:8000/up"]
      interval: 10s
      timeout: 5s
      start_period: 60s                          # failures here do not count against the task
      retries: 3                                 # 3 failed probes in a row: unhealthy
    stop_grace_period: 45s                       # SIGTERM, then SIGKILL after 45s
    deploy:
      mode: replicated
      replicas: 2
      update_config: {parallelism: 1, order: start-first, failure_action: rollback, monitor: 60s}
      rollback_config: {parallelism: 1, order: start-first, failure_action: pause}
      resources:
        limits:
          cpus: "2"
          memory: 1G

What happens on docker stack deploy with a new image:

  1. Swarm starts one new task next to the old ones (start-first, parallelism: 1).
  2. The new task has start_period to become healthy. Until then the old task keeps serving.
  3. Once healthy, the old task gets SIGTERM and stop_grace_period to finish in-flight requests.
  4. Swarm watches the new task for monitor (the same start_period). If it fails within that window, the update fails and Swarm rolls every updated task back to the previous version, also start-first.
  5. If the rollback itself fails, Swarm pauses the service instead of looping, and waits for you.

Web: no dropped requests#

With start-first, there is always a healthy task serving while another one starts. The healthcheck runs inside the container, so the image needs curl; the velyr-php run images include it.

Give start_period room for a cold start: it is both the time a container may stay unhealthy and the window in which a failure still triggers a rollback. A Laravel app that warms caches or runs config:cache at boot typically needs 60 to 120 seconds under load.

The health route should answer without touching the database or other services. A database outage should not make Swarm restart every web task.

start-first needs room on the node for one extra task during the update. Size the node's free CPU and memory for replicas + 1 web tasks.

Workers: no duplicate consumers#

Workers usually hold a queue, a lock or a consumer group. stop-first makes sure two versions of the same worker never consume at once, at the cost of a short gap between the old task stopping and the new one starting. A worker has no healthcheck: Swarm considers it updated once the new task is running, and watches it for 30 seconds.

Set grace to the longest job you are willing to wait for on shutdown. Laravel's queue:work finishes the current job on SIGTERM; with Swarm's default of 10 seconds, a longer job is killed and retried.

A service that must never run twice, like a scheduler, should stay a worker with one replica, and should not have scale.

Why init: true#

With init: true, Docker runs a tiny init as PID 1 that forwards signals and reaps orphaned processes. Without it, the app is PID 1 and never reaps children it does not wait for. A Laravel scheduler that runs tasks with runInBackground() left one zombie per background task, close to 300 per hour, before this became the default for every service.

Resources#

Every service gets resources.limits (cpus, memory) from the service or the env. There are no reservations: add them with deploy_extends if your scheduler needs them.

Size limits for the peak, not the average. Short-lived child processes add up: a scheduler starting several artisan commands at the top of the minute runs them all at once, each booting the framework, and a cgroup that hits its memory limit kills one of them (exit code 137).

Checking a rollout#

docker stack deploy returns as soon as Swarm accepts the new spec, before any task is replaced. To know whether a deploy converged or rolled back, watch each service's update state:

console
$ docker service inspect shop_web --format '{{.UpdateStatus.State}}'
completed
StateMeaning
updatingStill rolling out.
completedEvery task runs the new version.
rollback_started, rollback_completedThe new version failed and Swarm went back to the previous one.
paused, rollback_pausedSwarm stopped; inspect the tasks with docker service ps --no-trunc <service>.

An empty result means the service was never updated since it was created. A deploy script can poll this until every service is completed and fail otherwise; see CI/CD.