Repository navigation
Description
Steps to reproduce
- Save
gateway.dstack.yml,service.dstack.ymlandprobe.shfrom below. - Create the gateway with one replica:
dstack apply -f gateway.dstack.yml -y. - Deploy the service:
STARTUP_DELAY=0 dstack apply -f service.dstack.yml -y -d. Wait until the replica isrunningand its probe passes. - Start a rolling deployment to a version that is slow to start:
STARTUP_DELAY=3600 dstack apply -f service.dstack.yml -y -d. Wait until the new replica isrunning. Its probe keeps failing, so the old replica keeps serving. - Optionally, watch the traffic through the first gateway replica:
./probe.sh <gateway-replica-0-ip> rolling-update-gateway-scale-out.<gateway-domain>. - Scale the gateway out: set
replicas: 2ingateway.dstack.ymland rundstack apply -f gateway.dstack.yml -y.
STARTUP_DELAY=3600 stands in for any new version that takes a while to become ready, such as a model server loading weights. It only makes the rolling deployment last long enough to overlap with the gateway scale-out. The gateway has no certificate only because lets-encrypt doesn't support replicas greater than 1.
gateway.dstack.yml
type: gateway
name: aws-scale-out
default: false
backend: aws
region: eu-west-1
domain: "*.example.com"
# Change to 2 and re-apply to scale out in place
replicas: 1
# `lets-encrypt` is not supported with `replicas` > 1
certificate: nullservice.dstack.yml
type: service
name: rolling-update-gateway-scale-out
port: 8000
gateway: aws-scale-out
# The gateway has no certificate
https: false
auth: false
env:
# Seconds the server waits before it starts listening, like an app loading model weights.
# Changing the value triggers a rolling deployment:
# STARTUP_DELAY=0 dstack apply -f service.dstack.yml -y -d
# STARTUP_DELAY=3600 dstack apply -f service.dstack.yml -y -d
- STARTUP_DELAY
python: "3.12"
commands:
- sleep "$STARTUP_DELAY"
- exec python3 -m http.server 8000
probes:
# A replica only receives traffic once the server is listening
- type: http
url: /
backends: [aws]
spot_policy: on-demand
resources:
cpu: 1..
memory: 1GB..
disk: 20GB..
gpu: 0probe.sh
#!/bin/sh
# Requests the service through one gateway replica once a second and prints the HTTP status.
# The service hostname is sent in the Host header, so no DNS records are needed.
# Usage: ./probe.sh <gateway-replica-ip> <service-hostname>
set -u
ip=$1
host=$2
while true; do
code=$(curl -s -o /dev/null -w '%{http_code}' --max-time 5 -H "Host: $host" "http://$ip/")
echo "$(date -u +%H:%M:%S) $code"
sleep 1
doneActual behaviour
About 10 seconds after the new gateway replica becomes running, dstack stops the old service replica with the scaled down reason, although the new replica is not ready. The service is left without a replica that can serve requests, and the gateway returns 503 until the new replica becomes ready (in this repro, for an hour).
$ dstack event --within-run rolling-update-gateway-scale-out --since 2026-12-01T09:00:00Z
[2026-12-01 09:01:12] [👤dmitry-local-admin] [run rolling-update-gateway-scale-out] Run updated. Deployment: 1. Changed fields: configuration.env
...
[2026-12-01 09:03:57] [job rolling-update-gateway-scale-out-0-1] Job status changed PULLING -> RUNNING
[2026-12-01 09:06:04] [job rolling-update-gateway-scale-out-0-0] Job status changed RUNNING -> TERMINATING. Termination reason: SCALED_DOWN
[2026-12-01 09:06:10] [job rolling-update-gateway-scale-out-0-0, gateway-replica aws-scale-out-0] Service replica unregistered
[2026-12-01 09:06:10] [run rolling-update-gateway-scale-out, gateway-replica aws-scale-out-1] Service registered
[2026-12-01 09:06:43] [job rolling-update-gateway-scale-out-0-0] Job status changed TERMINATING -> TERMINATED
$ dstack event --within-gateway aws-scale-out --since 2026-12-01T09:04:00Z
[2026-12-01 09:04:22] [👤dmitry-local-admin] [gateway aws-scale-out] Gateway updated. Changed fields: replicas
[2026-12-01 09:04:26] [gateway-replica aws-scale-out-1] Gateway replica created. Status: SUBMITTED
[2026-12-01 09:04:49] [gateway-replica aws-scale-out-1] Gateway replica status changed SUBMITTED -> PROVISIONING
[2026-12-01 09:05:55] [gateway-replica aws-scale-out-1] Gateway replica status changed PROVISIONING -> RUNNING
[2026-12-01 09:06:10] [job rolling-update-gateway-scale-out-0-0, gateway-replica aws-scale-out-0] Service replica unregistered
[2026-12-01 09:06:10] [run rolling-update-gateway-scale-out, gateway-replica aws-scale-out-1] Service registered
$ dstack ps -v
NAME BACKEND RESOURCES INSTANCE TYPE PRICE STATUS PROBES SUBMITTED ERROR
rolling-update-gateway-scale-out deployment=1 - - running 17 mins ago
group=0 replica=0 deployment=0 aws (us-east-1) cpu=x86:2 mem=1GB disk=100GB t3.micro $0.0104 terminated 17 mins ago scaled down
replica=1 deployment=1 aws (us-east-1) cpu=x86:2 mem=1GB disk=100GB t3.micro $0.0104 running × 5 mins ago
probe.sh through the first gateway replica:
09:01:09 200
...
09:06:09 200
09:06:10 503
...
09:06:54 503
Expected behaviour
A rolling deployment stops an old replica that is serving requests only once a new replica is ready to take over. Adding a gateway replica doesn't affect which service replicas are serving.
dstack version
master (27a9364b4)
Server logs
dstack server --log-level=debug, filtered to the run and the gateway
INFO 2026-12-01T09:04:22.508 services.events Emitting event: Gateway updated. Changed fields: replicas. Event targets: gateway(1b2a70)aws-scale-out. Actor: user(efa6c3)dmitry-local-admin
INFO 2026-12-01T09:04:26.472 gateways gateway(1b2a70)aws-scale-out: scaling out, adding 1 replica(s)
INFO 2026-12-01T09:04:26.477 services.events Emitting event: Gateway replica created. Status: SUBMITTED. Event targets: gateway-replica(ae636b)aws-scale-out-1. Actor: system
INFO 2026-12-01T09:04:49.615 services.events Emitting event: Gateway replica status changed SUBMITTED -> PROVISIONING. Event targets: gateway-replica(ae636b)aws-scale-out-1. Actor: system
INFO 2026-12-01T09:05:55.147 gateway_replicas gateway-replica(ae636b)aws-scale-out-1: gateway replica connected
INFO 2026-12-01T09:05:55.149 services.events Emitting event: Gateway replica status changed PROVISIONING -> RUNNING. Event targets: gateway-replica(ae636b)aws-scale-out-1. Actor: system
INFO 2026-12-01T09:06:04.612 services.events Emitting event: Job status changed RUNNING -> TERMINATING. Termination reason: SCALED_DOWN. Event targets: job(286972)rolling-update-gateway-scale-out-0-0. Actor: system
INFO 2026-12-01T09:06:10.344 gateway_replicas gateway-replica(ae636b)aws-scale-out-1: gateway config submitted
INFO 2026-12-01T09:06:10.466 services.events Emitting event: Service replica unregistered. Event targets: job(286972)rolling-update-gateway-scale-out-0-0, gateway-replica(a9a617)aws-scale-out-0. Actor: system
INFO 2026-12-01T09:06:10.567 services.events Emitting event: Service registered. Event targets: run(1b8307)rolling-update-gateway-scale-out, gateway-replica(ae636b)aws-scale-out-1. Actor: system
INFO 2026-12-01T09:06:43.793 services.events Emitting event: Job status changed TERMINATING -> TERMINATED. Event targets: job(286972)rolling-update-gateway-scale-out-0-0. Actor: system
Additional information
Cause. During a rolling deployment, _build_rolling_deployment_maps stops two kinds of out-of-date replicas:
- replicas that are not receiving traffic, right away;
- replicas that are receiving traffic, but only as many as exceed the desired count, so a new replica has to be serving before an old one goes.
is_replica_receiving_traffic considers a replica behind a gateway to receive traffic only if it is registered on every RUNNING gateway replica. But a new gateway replica becomes RUNNING before any service replica is registered on it. Registration happens on its next pass, which comes at least 15 seconds later (09:05:55 → 09:06:10 in the logs above). Until then, no service replica counts as receiving traffic, so every out-of-date replica falls into the first group and is stopped at once. A stopped replica is not registered on the new gateway replica later either, because only RUNNING jobs are registered.
It isn't a narrow race. The run pipeline processes each run every 5 seconds, so it processes the run at least twice during the window. Any rolling deployment that is still in progress when a gateway is scaled out hits the bug. A rolling deployment can stay in progress for a long time, such as when the new version crash-loops or never passes its probes. The old replicas are the only ones serving during that time.
Scope. All out-of-date replicas are stopped at once, not just one. If none of the new replicas is serving yet, the service goes down until one becomes ready. If some are, the service loses capacity. The same happens when the gateway has no RUNNING replicas at all (is_replica_receiving_traffic returns False for every replica). Services without a gateway are not affected.
Possible fix. Check traffic differently for the two groups:
- stop an old replica right away only if it is registered on no
RUNNINGgateway replica; - count a replica toward the excess only if it is registered on every
RUNNINGgateway replica, as today.
A replica that is registered on some gateway replicas but not all then stays until the gateway sync registers it everywhere. The rolling deployment pauses for that time rather than getting stuck.
Activity
Keep serving replicas during gateway scale-out (dstackai#4565)