Skip to main content
← PROBLEMS / PRODUCTION-DEBUGGING
An end-to-end domain learning journey

Graceful Shutdown in
Rolling Deployments.

Stopping 502s during 20k RPS Kubernetes rolling deploys requires a preStop sleep (10–15s) to cover EndpointSlice/ALB deregistration lag, followed by graceful HTTP/1.1 keep-alive & HTTP/2 GOAWAY draining within a 45s terminationGracePeriodSeconds.
Start the journey5 stages
FIRST PRINCIPLES. PRODUCTION SCALE.
The idea to keep with you

When Kubernetes terminates a pod during a rolling update, two things happen **asynchronously in parallel**, never close server sockets immediately on SIGTERM—use a preStop hook sleep to let kube-proxy/Envoy/ALB remove the pod IP first, then drain in-flight requests (up to 25s) with HTTP/2 GOAWAY and Connection: close.

TL;DR30-second simplified summary
▾

Core Mechanics: When Kubernetes terminates a pod during a rolling update, two things happen asynchronously in parallel: kubelet sends SIGTERM to the container immediately, while the control plane propagates EndpointSlice / AWS ALB target.

Visual & Structural Flow: Depicts preStop Sleep (12s) -> SIGTERM Graceful Drain (25s) -> SIGKILL (45s) (Kubelet / API -> ALB / Envoy -> Spring Pod); Serving -> Terminating (preStop 12s) -> Draining (25s) -> Exited (SERVING -> PRESTOP_WAIT -> DRAINING -> EXIT_0).

Trade-Offs & Production: Enabling server.shutdown=graceful without a preStop hook still causes 502s: the moment SIGTERM arrives at t=0s, Tomcat/Netty stops accepting new connections—before. Coordinated preStop + graceful drain is mandatory for zero-downtime cloud deployments: High-QPS API Gateways & Payment Services: Deploying 20+ times/day at 20k+ RPS with zero 502s.

Failure Modes & Application: Coordinated preStop + graceful drain is mandatory for zero-downtime cloud deployments: High-QPS API Gateways & Payment Services: Deploying 20+ times/day at 20k+ RPS with zero 502s.

Full walkthrough · 5 stages
01
STAGE 01 · WHY IT MATTERSL4 · FUNDAMENTALS

Why rolling deployments cause 15–30 second 502 spikes at 20k RPS.

Why this matters: The mechanism: understand how Why rolling deployments cause 15–30 second 502 spikes at 20k RPS works from first principles.

Think of Why rolling deployments cause 15–30 second 502 spikes at 20k RPS as the intuition layer for Graceful Shutdown in Rolling Deployments: The mechanism: understand how Why rolling deployments cause 15–30 second 502 spikes at 20k RPS works from first principles.

When Kubernetes terminates a pod during a rolling update, two things happen asynchronously in parallel: kubelet sends SIGTERM to the container immediately, while the control plane propagates EndpointSlice / AWS ALB target deregistration to kube-proxy and Envoy over 2–10 seconds. If Spring Boot/Netty closes its listener socket immediately on SIGTERM, Envoy and ALB keep routing 20,000 RPS of new and keep-alive requests to a dead socket—spiking 502 Bad Gateway and TCP RSTs.

02
STAGE 02 · IMPACTL5 · APPLIED DESIGN

What happens during the EndpointSlice propagation race.

Why this matters: The problem: see what breaks without What happens during the EndpointSlice propagation race under real concurrency or scale.

Immediate SIGTERM shutdown breaks both new and existing connections:

  1. Propagation Race: Envoy/ALB still holds the terminating pod IP as HEALTHY for 2–10 s after SIGTERM.
  2. Keep-Alive & HTTP/2 Pinning: Without an HTTP/2 GOAWAY frame or Connection: close on responses, proxies keep sending new requests down already-open TCP sockets.
  3. Premature Kill: In-flight 25-second requests get severed if terminationGracePeriodSeconds defaults to 30 s minus preStop.
Zero-Drop Termination Timeline

preStop Sleep (12s) -> SIGTERM Graceful Drain (25s) -> SIGKILL (45s)

Kubelet / APIALB / EnvoySpring Pod1. t=0s: Mark Terminating ->deregister IP2. t=0s: Run preStop sleep 12s(still serving!)3. t=8s: Envoy/ALB removes pod IP4. t=12s: Send SIGTERM -> HTTP/2GOAWAY5. t=12..37s: Drain 25s in-flight -> exi…

The 12s preStop sleep absorbs EndpointSlice propagation lag while Spring Boot continues serving until Envoy drains.

Engineering Decision Framework
Problem
How to implement Graceful Shutdown in Rolling Deployments without hitting unbounded tail latency, state divergence, or operational fragility.
Naive Solution
Use a single unpartitioned path with unbounded retries and synchronous global coordination.
Why It Fails
Global coordination serializes throughput under load, while unbounded retries amplify transient stalls into full outages.
Alternatives
Compare synchronous strict-consistency paths against bounded asynchronous replication, local caching, or partitioned ownership.
Engineering Decision
When Kubernetes terminates a pod during a rolling update, two things happen asynchronously in parallel, never close server sockets immediately on SIGTERM—use a preStop hook sleep to let kube-proxy/Envoy/ALB remove the pod IP first, then drain in-flight requests (up to 25s) with HTTP/2 GOAWAY and Connection: close.
Trade-Offs
Gains predictable p99 latency and fault isolation in exchange for explicit bounding, observability, and slightly higher implementation complexity.
Decision Exercise · Make the Call

When Graceful Shutdown in Rolling Deployments experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?

03
STAGE 03 · NAIVE SOLUTIONL5 · APPLIED DESIGN

Naive solution (and why server.shutdown=graceful alone still drops 502s).

Why this matters: The problem: see what breaks without Naive solution under real concurrency or scale.

Think of Naive solution (and why server.shutdown=graceful alone still drops 502s) as the intuition layer for Graceful Shutdown in Rolling Deployments: The problem: see what breaks without Naive solution under real concurrency or scale.

Enabling server.shutdown=graceful without a preStop hook still causes 502s: the moment SIGTERM arrives at t=0s, Tomcat/Netty stops accepting new connections—before Envoy and AWS ALB finish removing the pod IP (t=2..10s).

Component Topology

Naive solution (and why server.shutdown=graceful alone still drops 502s)

Ingress / LBingressK8s ControlcomputeWorker NodestorageApp Podscontrolactive trafficreconcileprobe / SIGTERM
04
STAGE 04 · ENTERPRISE SOLUTIONL6 · SCALE & TRADE-OFFS

Enterprise solution: preStop Propagation Buffer + Graceful Drain + GOAWAY.

Why this matters: Operational control: configure bounds, failure handling, and safe production defaults.

Zero-downtime rolling deployments coordinate three timers within terminationGracePeriodSeconds: 45:

  • 1. preStop Sleep (12 s): Blocks SIGTERM for 12 s while /readyz fails and EndpointSlice + AWS ALB deregister the pod; the server keeps accepting straggler requests routed during propagation.
  • 2. Graceful Shutdown (25 s timeout): At t=12 s, SIGTERM triggers Spring Boot server.shutdown=graceful (spring.lifecycle.timeout-per-shutdown-phase=25s), sending HTTP/2 GOAWAY frames and Connection: close on HTTP/1.1 responses while finishing active <= 25 s requests.
  • 3. Bounded Rollout Velocity: Set maxSurge: 25%, maxUnavailable: 0 so capacity never dips during the 45 s drain window.
Pod Shutdown Lifecycle

Serving -> Terminating (preStop 12s) -> Draining (25s) -> Exited

Pod deleted / rolling update12s elapsed -> SIGTERMActive requests == 0SERVINGIn Endpoints + ALBPRESTOP_WAIT12s: deregister + serveDRAININGGOAWAY + finish <=25sEXIT_0Clean exit < 45s

12s preStop + 25s drain fits safely inside terminationGracePeriodSeconds: 45 with zero dropped requests.

# Kubernetes PodSpec + Spring Boot 3 graceful shutdown config
# application.yaml:
#   server.shutdown: graceful
#   spring.lifecycle.timeout-per-shutdown-phase: 25s
spec:
  terminationGracePeriodSeconds: 45   # >= preStop (12s) + drain (25s) + buffer (8s)
  containers:
    - name: payment-api
      lifecycle:
        preStop:
          exec:
            command: ["sh", "-c", "sleep 12"]
05
STAGE 05 · SYSTEM DESIGN APPLICATIONSL7 · STAFF / PRINCIPAL

Where Graceful Shutdown applies in Production Kubernetes.

Why this matters: Scope boundary: identify where this pattern fits best and where simpler or async alternatives win.

Coordinated preStop + graceful drain is mandatory for zero-downtime cloud deployments:

  • High-QPS API Gateways & Payment Services: Deploying 20+ times/day at 20k+ RPS with zero 502s.
  • gRPC & WebSocket Servers: Emitting HTTP/2 GOAWAY frames so long-lived streams migrate cleanly.
  • Spot / Preemptible Node Drains: Finishing in-flight jobs before PDB / autoscaler evictions.
Practice · Explain the consequences

Practice: Make the trade-off yourself.

Question 1 of 3
Score: 0/3

Why is a preStop sleep (e.g. 12 seconds) needed even when Spring Boot has server.shutdown=graceful enabled?

Recommended Learning Flow