Skip to content

Rolling deploy on PaaS without traffic drop: connection draining and the readiness gap

Every rolling deploy that drops traffic is failing one of two things — the readiness probe lies about when the new pod can serve, or the drain on the old pod is shorter than the in-flight request budget.

You roll a deploy across four PaaS pods. The release notes promised zero traffic drop. The dashboard shows a 12-second spike of 502s every time a pod gets replaced. The team writes it off as “normal during deploys” for two months until a customer with a real-time integration complains. Two things are broken: the new pod is being marked ready before it can actually serve, and the old pod is being killed before in-flight requests finish. Both are health-check problems. Neither is the deploy strategy’s fault.

What’s happening

A rolling deploy replaces pods one at a time (or batch-at-a-time) behind the same LB. The LB needs to know two things accurately:

  • When is the new pod ready to take traffic? This is the readiness probe. If it returns 200 before the app has warmed its connection pool, lazily-loaded its config, primed its cache, and registered with the discovery layer, then traffic hits a half-booted process and falls over.
  • When can the old pod be killed? This is the drain window. Between “marked unhealthy in the LB” and “process killed,” the LB needs enough time to stop sending new requests AND for in-flight requests on existing connections to finish. If the drain is shorter than your p99 request duration, you’ll see connection resets on long-running requests.

The “readiness gap” is the window between “LB marks new pod healthy” and “new pod actually serves correctly.” Tighten it by making the probe prove the pod can serve — query a dependency, check the connection pool size, exercise the cache — not just answer 200 from a trivial route.

How to verify

Measure the readiness lie. Hit the deep health endpoint on a newly-started pod immediately after the LB marks it ready, and compare against a steady-state response:

# 1. Time the deep probe across a deploy
while true; do
  date -u +"%Y-%m-%dT%H:%M:%SZ"
  curl -o /dev/null -sS -w "status=%{http_code} total=%{time_total}s\n" \
    "https://app.example.com/healthz/deep"
  sleep 1
done | tee /tmp/healthz-during-deploy.log

Trigger a rolling deploy and watch the trace. If total= jumps to 2–3 seconds for the first 10–30 seconds after a pod rejoins, the readiness probe is reporting ready too soon.

Measure the drain reality. Compare p99 request duration against the configured drain window:

# Real p99 from your access logs (or LB metrics)
curl -sS "https://metrics.example.com/api/v1/query?\
  query=histogram_quantile(0.99, sum(rate(http_request_duration_seconds_bucket[1h])) by (le))"

# Configured drain window on the LB
curl -sS -H "Authorization: Bearer ${PAAS_TOKEN}" \
  "https://app.paas.example.com/1.0/lb/rest/getdrainwindow?envName=app-prod"

If p99 is 4.2s and drain is 5s, you’re cutting it fine; if drain is 2s, the long tail is dropping connections every deploy.

The fix

Step 1 — Make the readiness probe honest. A trivial 200 OK is a lie if the pod can’t actually serve. Wire the probe into the dependencies the app needs to function. Match the failure modes you actually want to detect:

# readiness-probe.yaml (per-pod, executed by the PaaS LB)
readinessProbe:
  httpGet:
    path: /healthz/ready
    port: 8080
  initialDelaySeconds: 5         # don't probe before the app has bound the port
  periodSeconds: 2               # poll every 2s
  timeoutSeconds: 2
  successThreshold: 2            # 2 consecutive successes flip the pod to ready
  failureThreshold: 3            # 3 consecutive failures flip it back to not-ready

# What /healthz/ready returns:
# 200 only if:
#   - DB connection pool reports >= min_pool_size active
#   - Redis/cache PING returns within 50ms
#   - Background workers have registered
#   - Config has been fully loaded (no lazy first-request load)
#   - Any external-dependency circuit breakers are closed
# Returns 503 with a JSON body explaining which dep is failing otherwise.

The endpoint implementation in pseudocode:

GET /healthz/ready:
  checks = []
  try: db_pool.acquire(timeout=200ms); db_pool.release(); checks += ("db", "ok")
  except: return 503, {db: "fail"}

  try: cache.ping(timeout=50ms); checks += ("cache", "ok")
  except: return 503, {cache: "fail"}

  if worker_registry.count_active() < min_workers: return 503, {workers: "starting"}
  if not config.fully_loaded(): return 503, {config: "loading"}

  return 200, {checks: checks}

Step 2 — Match the drain window to p99 request duration. The drain is the time between “stop sending new requests to this pod” and “kill the process.” Set it to p99_request_duration + safety_margin. For most HTTP APIs that’s 30–60 seconds. For long-polling, file uploads, or streaming endpoints, it can be minutes — segment those onto pods with longer drain windows or move them to a separate environment.

# rolling-deploy-config.yaml
strategy:
  type: rolling
  max_surge: 1                   # how many extra pods can exist during deploy
  max_unavailable: 0             # never go below desired count
  pod_replacement:
    pre_stop_hook:               # runs before SIGTERM
      exec:
        command:
          - /bin/sh
          - -c
          - |
            # Tell the LB this pod is going down, give it time to deregister
            curl -X POST http://localhost:8080/_internal/draining
            # Stop accepting new work but let in-flight finish
            sleep 5
    grace_period_seconds: 60     # how long after SIGTERM before SIGKILL
    drain_seconds: 60            # how long the LB waits before considering pod gone

  rollout_pacing:
    min_ready_seconds: 30        # pod must be ready for 30s before moving to next
    pause_seconds_between_pods: 5

Step 3 — Wire the deploy hook so pacing matches reality. A rolling deploy that ignores the pause is a deploy that finds out about failures from your customers:

# .github/workflows/rolling-deploy.yaml
name: rolling-deploy
on:
  workflow_dispatch:
    inputs:
      build_id:
        required: true
        type: string

jobs:
  rolling:
    runs-on: ubuntu-latest
    steps:
      - name: Pre-deploy LB tuning sanity check
        run: |
          DRAIN=$(curl -fsS -H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
            "https://app.paas.example.com/1.0/lb/rest/getdrainwindow?envName=app-prod" \
            | jq -r '.drainSeconds')
          P99=$(curl -fsS "https://metrics.example.com/api/v1/query?\
            query=histogram_quantile(0.99,sum(rate(http_request_duration_seconds_bucket[1h])) by (le))" \
            | jq -r '.data.result[0].value[1]')

          # Refuse to deploy if drain is too short
          if (( $(echo "$DRAIN < $P99 * 1.5" | bc -l) )); then
            echo "Drain window ${DRAIN}s is too short for p99=${P99}s. Tune LB before deploy."
            exit 1
          fi

      - name: Roll the deploy, pod-by-pod
        run: |
          curl -fsS -X POST \
            -H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
            -d "envName=app-prod&strategy=rolling&buildId=${{ inputs.build_id }}&minReadySeconds=30&pauseSeconds=5" \
            https://app.paas.example.com/1.0/environment/control/rest/redeploy

      - name: Watch the deploy
        run: |
          for i in $(seq 1 60); do
            STATE=$(curl -fsS -H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
              "https://app.paas.example.com/1.0/environment/control/rest/getdeployprogress?envName=app-prod" \
              | jq -r '.state')
            PROGRESS=$(curl -fsS -H "Authorization: Bearer ${{ secrets.PAAS_TOKEN }}" \
              "https://app.paas.example.com/1.0/environment/control/rest/getdeployprogress?envName=app-prod" \
              | jq -r '.podsReady')
            echo "state=${STATE} pods_ready=${PROGRESS}"
            [ "${STATE}" = "DONE" ] && break
            [ "${STATE}" = "FAILED" ] && exit 2
            sleep 10
          done

      - name: Post-deploy 502 sanity
        run: |
          # Sample the LB for the next 5 minutes — no 502 spike permitted
          for i in $(seq 1 60); do
            ERR=$(curl -fsS "https://metrics.example.com/api/v1/query?\
              query=sum(rate(http_responses_total{code=~\"5..\"}[1m]))" \
              | jq -r '.data.result[0].value[1]')
            echo "minute ${i}: 5xx_rate=${ERR}"
            sleep 5
          done

Step 4 — SIGTERM-aware app code. The app must catch SIGTERM, stop accepting new connections, and let in-flight requests finish. A pod that exits immediately on SIGTERM cuts connections regardless of the LB drain:

# Pseudocode for the app's shutdown sequence
on SIGTERM:
  log.info("SIGTERM received — beginning graceful shutdown")
  http_server.stop_accepting_new_connections()
  # Wait for in-flight requests to finish, bounded by drain budget
  wait_for_active_requests_to_drain(timeout=55s)
  log.info("drained — exiting")
  exit(0)

If you tune nothing else, get the readiness probe to query the dependencies and match the drain window to p99 + margin. Most rolling-deploy 502 spikes are one of those two things.

Stack Harbor tunes readiness probes, drain windows, and rollout pacing on PaaS environments under Managed Cloud Ops — backed by per-pod metrics in our monitoring plane so the deploy hook can refuse to ship when drain math is wrong.