Repository navigation
Description
Steps to reproduce
- Save
gateway.dstack.yml,service.dstack.ymlandprobe.shfrom below. Replace the domain and the certificate ARN with your own. - Create the gateway with one replica:
dstack apply -f gateway.dstack.yml -y. Point the*.<gateway-domain>DNS record to the load balancer hostname thatdstack gateway listshows. - Deploy the service:
dstack apply -f service.dstack.yml -y -d. Wait untilhttps://alb-gateway-scale-out.<gateway-domain>/marker.txtreturnsservice-ok. - Watch the traffic:
./probe.sh alb-gateway-scale-out.<gateway-domain>. - Scale the gateway out: set
replicas: 2ingateway.dstack.ymland rundstack apply -f gateway.dstack.yml -y. - After the new replica is
runningand the service is registered on it, scale the gateway in: setreplicas: 1and rundstack apply -f gateway.dstack.yml -y.
gateway.dstack.yml
type: gateway
name: aws-alb
backend: aws
region: eu-west-1
domain: "*.example.com"
certificate:
type: acm
arn: arn:aws:acm:eu-west-1:<account-id>:certificate/<certificate-id>
load_balancer:
type: alb
# Change to 2 and re-apply to scale out in place
replicas: 1service.dstack.yml
type: service
name: alb-gateway-scale-out
port: 8000
gateway: aws-alb
auth: false
python: "3.12"
commands:
# probe.sh tells the service's responses from the gateway's nginx default site by this file
- echo service-ok > marker.txt
- exec python3 -m http.server 8000
probes:
- type: http
url: /marker.txt
backends: [aws]
spot_policy: on-demand
resources:
cpu: 1..
memory: 1GB..
disk: 20GB..
gpu: 0probe.sh
#!/bin/sh
# Requests /marker.txt and / of the service through the ALB twice a second and prints who answered:
# service - the service itself
# nginx-welcome - the nginx default site ("Welcome to nginx!") of a gateway replica
# nginx-404 - nginx 404 page (the default site has no /marker.txt)
# other:<...> - anything else (e.g. 502/503/504 pages)
# Usage: ./probe.sh <service-hostname>
set -u
host=$1
classify() {
case "$1" in
*service-ok*|*"Directory listing"*) echo service ;;
*"Welcome to nginx"*) echo nginx-welcome ;;
*"404 Not Found"*) echo nginx-404 ;;
*) echo "other:$(printf '%s' "$1" | tr -s '\r\n\t ' ' ' | cut -c1-40)" ;;
esac
}
probe() {
out=$(curl -s --max-time 5 -w '\n%{http_code}' "https://$host$1")
code=$(printf '%s' "$out" | tail -n1)
body=$(printf '%s' "$out" | sed '$d')
echo "$(date -u +%H:%M:%S.%3N) $1 $code $(classify "$body")"
}
while true; do
probe /marker.txt
probe /
sleep 0.5
doneActual behaviour
Scale-out. The load balancer starts sending requests to the new gateway replica about 5 seconds after dstack registers it in the target group. dstack registers the service on the replica about 16 seconds after that. In between, the replica answers requests for the service with the nginx default site: 200 with "Welcome to nginx!" for /, and 404 for other paths. A request lands there about every other time, because the load balancer splits requests between the two replicas. Just before the service is registered, the replica returns 503 for a moment. The old replica keeps serving correctly the whole time.
12:01:37.115 / 200 service
12:01:38.102 /marker.txt 200 service
12:01:38.459 / 200 nginx-welcome
12:01:39.451 /marker.txt 200 service
12:01:39.805 / 200 nginx-welcome
...
12:01:42.500 / 200 nginx-welcome
12:01:43.358 /marker.txt 404 nginx-404
12:01:43.833 / 200 service
...
12:01:49.174 / 200 nginx-welcome
12:01:50.160 /marker.txt 200 service
12:01:50.512 / 503 other:<html> <head><title>503 Service Temporar
12:01:51.362 /marker.txt 503 other:<html> <head><title>503 Service Temporar
12:01:51.854 / 200 service
Between 12:01:38 and 12:01:51, 12 of 20 requests got a wrong response: 7 got the "Welcome to nginx!" page with status 200, 3 got 404 and 2 got 503.
Scale-in. dstack terminates the removed replica's instance right after it deregisters the replica from the target group. A request 1.5 seconds later failed with 502:
12:03:07.561 /marker.txt 200 service
12:03:07.920 / 502 other:<html> <head><title>502 Bad Gateway</tit
12:03:09.306 /marker.txt 200 service
$ dstack event --within-gateway aws-alb
...
[2026-12-01 12:00:08] [👤dmitry-local-admin] [gateway aws-alb] Gateway updated. Changed fields: replicas
[2026-12-01 12:00:11] [gateway-replica aws-alb-1] Gateway replica created. Status: SUBMITTED
[2026-12-01 12:00:31] [gateway-replica aws-alb-1] Gateway replica status changed SUBMITTED -> PROVISIONING
[2026-12-01 12:01:33] [gateway-replica aws-alb-1] Gateway replica status changed PROVISIONING -> RUNNING
[2026-12-01 12:01:51] [run alb-gateway-scale-out, gateway-replica aws-alb-1] Service registered
[2026-12-01 12:01:51] [job alb-gateway-scale-out-0-0, gateway-replica aws-alb-1] Service replica registered
[2026-12-01 12:02:32] [👤dmitry-local-admin] [gateway aws-alb] Gateway updated. Changed fields: replicas
[2026-12-01 12:02:50] [gateway-replica aws-alb-0] Gateway replica status changed RUNNING -> TERMINATING (Scaled in)
[2026-12-01 12:03:07] [gateway-replica aws-alb-0] Gateway replica status changed TERMINATING -> TERMINATED
Expected behaviour
The load balancer sends requests to a gateway replica only once the replica can serve the services registered on the gateway. When a gateway is scaled in, the removed replica stops getting new requests and finishes the ones in progress before its instance is terminated. Scaling a gateway out or in doesn't affect the responses clients get.
dstack version
master (3ce21fbde)
Server logs
dstack server --log-level=debug, filtered to the gateway and the service
INFO 2026-12-01T12:00:08.464 services.events Emitting event: Gateway updated. Changed fields: replicas. Event targets: gateway(cfbbb3)aws-alb. Actor: user(efa6c3)dmitry-local-admin
INFO 2026-12-01T12:00:11.862 gateways gateway(cfbbb3)aws-alb: scaling out, adding 1 replica(s)
INFO 2026-12-01T12:00:11.866 services.events Emitting event: Gateway replica created. Status: SUBMITTED. Event targets: gateway-replica(a470ea)aws-alb-1. Actor: system
INFO 2026-12-01T12:00:31.966 services.events Emitting event: Gateway replica status changed SUBMITTED -> PROVISIONING. Event targets: gateway-replica(a470ea)aws-alb-1. Actor: system
INFO 2026-12-01T12:01:33.245 gateway_replicas gateway-replica(a470ea)aws-alb-1: gateway replica connected
INFO 2026-12-01T12:01:33.636 gateway_replicas gateway-replica(a470ea)aws-alb-1: registered with load balancer
INFO 2026-12-01T12:01:33.640 services.events Emitting event: Gateway replica status changed PROVISIONING -> RUNNING. Event targets: gateway-replica(a470ea)aws-alb-1. Actor: system
INFO 2026-12-01T12:01:49.815 gateway_replicas gateway-replica(a470ea)aws-alb-1: gateway config submitted
DEBUG 2026-12-01T12:01:49.825 gateway_replicas gateway-replica(a470ea)aws-alb-1: registering service dmitry-local/alb-gateway-scale-out
DEBUG 2026-12-01T12:01:50.003 gateway_replicas gateway-replica(a470ea)aws-alb-1: registering replica 67d040de-497b-4494-b1b7-207bdaea76d7 for service dmitry-local/alb-gateway-scale-out
INFO 2026-12-01T12:01:51.422 services.events Emitting event: Service registered. Event targets: run(04ffb5)alb-gateway-scale-out, gateway-replica(a470ea)aws-alb-1. Actor: system
INFO 2026-12-01T12:01:51.423 services.events Emitting event: Service replica registered. Event targets: job(67d040)alb-gateway-scale-out-0-0, gateway-replica(a470ea)aws-alb-1. Actor: system
INFO 2026-12-01T12:02:32.876 services.events Emitting event: Gateway updated. Changed fields: replicas. Event targets: gateway(cfbbb3)aws-alb. Actor: user(efa6c3)dmitry-local-admin
INFO 2026-12-01T12:02:38.506 gateways gateway(cfbbb3)aws-alb: scaling in, marking 1 replica(s) for scale-in
INFO 2026-12-01T12:02:50.193 services.events Emitting event: Gateway replica status changed RUNNING -> TERMINATING (Scaled in). Event targets: gateway-replica(c59d7e)aws-alb-0. Actor: system
DEBUG 2026-12-01T12:03:06.072 gateway_replicas gateway-replica(c59d7e)aws-alb-0: deregistering from load balancer
INFO 2026-12-01T12:03:06.370 gateway_replicas gateway-replica(c59d7e)aws-alb-0: deregistered from load balancer
DEBUG 2026-12-01T12:03:06.370 gateway_replicas gateway-replica(c59d7e)aws-alb-0: terminating gateway replica
INFO 2026-12-01T12:03:07.007 services.events Emitting event: Gateway replica status changed TERMINATING -> TERMINATED. Event targets: gateway-replica(c59d7e)aws-alb-0. Actor: system
Additional information
Cause (scale-out). A gateway replica is registered with the load balancer and marked RUNNING in the same step. Services are registered on it during its next pass, at least 15 seconds later (12:01:33 → 12:01:49 in the logs above).
The load balancer doesn't wait for that. The target group uses the AWS default health check, GET / on port 80. That request matches no service on the gateway, so the nginx default site answers it. The gateway setup installs the Ubuntu nginx package and leaves its default site in place. So the health check passes as soon as nginx is up, before dstack's gateway app has any services. Requests for the service hostname get the same default site until the service is registered.
Cause (scale-in). The replica is deregistered from the load balancer and its instance terminated in the same pass. Deregistration takes effect with some delay, and the target group's deregistration delay (300 seconds by default) exists so that requests in progress can finish. Terminating the instance right away cuts them off. Long and streaming requests, such as LLM completions, are the most exposed. #4493 is the same problem one level down: the gateway dropping requests in progress when it unregisters a service replica.
Scope.
- It happens on every scale-out of a gateway with a load balancer, for every service on the gateway. In this repro, the window lasted about 13 seconds. With N gateway replicas, about 1/N of requests go to the new one during the window.
- The wrong responses are worse than errors: a
200with the nginx welcome page looks like success to clients and to retry logic. - GCP load balancers (
gcp-cmcertificates) are registered in the same step, and their health check is also HTTP on port 80 with the default path/. So GCP is likely affected too. This hasn't been tested. - Because the health check only reaches the nginx default site, it also keeps passing on a replica whose dstack gateway app is down.
Possible fix.
- Register a gateway replica with the load balancer only after its first successful sync, that is, once its config is submitted and its services and their replicas are registered.
- Optionally, point the health check at something that reflects whether the replica is ready, instead of the nginx default site. This would also take a replica out of rotation when its gateway app is down. Existing target groups and GCP health checks would need to be updated.
- On scale-in, wait until the load balancer has finished deregistering the replica, or for a bounded drain period, before terminating its instance.
Activity