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.
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.
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:
- Propagation Race: Envoy/ALB still holds the terminating pod IP as
HEALTHYfor2–10 safterSIGTERM. - Keep-Alive & HTTP/2 Pinning: Without an HTTP/2
GOAWAYframe orConnection: closeon responses, proxies keep sending new requests down already-open TCP sockets. - Premature Kill: In-flight
25-secondrequests get severed ifterminationGracePeriodSecondsdefaults to30 sminuspreStop.
preStop Sleep (12s) -> SIGTERM Graceful Drain (25s) -> SIGKILL (45s)
The 12s preStop sleep absorbs EndpointSlice propagation lag while Spring Boot continues serving until Envoy drains.
- 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.
When Graceful Shutdown in Rolling Deployments experiences a sudden 5x traffic spike alongside a slow downstream dependency, which engineering choice best protects system stability?
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).
Naive solution (and why server.shutdown=graceful alone still drops 502s)
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.
preStopSleep (12 s): BlocksSIGTERMfor12 swhile/readyzfails andEndpointSlice+ AWS ALB deregister the pod; the server keeps accepting straggler requests routed during propagation. - 2. Graceful Shutdown (
25 stimeout): Att=12 s,SIGTERMtriggers Spring Bootserver.shutdown=graceful(spring.lifecycle.timeout-per-shutdown-phase=25s), sending HTTP/2GOAWAYframes andConnection: closeon HTTP/1.1 responses while finishing active<= 25 srequests. - 3. Bounded Rollout Velocity: Set
maxSurge: 25%,maxUnavailable: 0so capacity never dips during the45 sdrain window.
Serving -> Terminating (preStop 12s) -> Draining (25s) -> Exited
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"]
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+ RPSwith zero 502s. - gRPC & WebSocket Servers: Emitting HTTP/2
GOAWAYframes so long-lived streams migrate cleanly. - Spot / Preemptible Node Drains: Finishing in-flight jobs before
PDB/ autoscaler evictions.
Practice: Make the trade-off yourself.
Why is a preStop sleep (e.g. 12 seconds) needed even when Spring Boot has server.shutdown=graceful enabled?