Deployment reliability · BharatPe
Cutting deployment-time 502 errors by 99.5%
Closed a 162.5-second gap between ALB drainage and pod termination, then validated the fix through canary delivery and production APM.
The failure pattern
Every pod rotation or Kubernetes rolling deployment of a business-critical service produced HTTP 502 and 504 errors for up to three minutes per pod. In production, this meant hundreds of failed requests during each deployment.
The error burst consistently began about 17 seconds after a pod was deleted. That timing pointed to the termination lifecycle rather than a random application failure.
The 162.5-second dead zone
The measured timeline exposed a mismatch between two independent drainage mechanisms:
- At T+0.6 seconds, AWS ALB marked the target as draining and stopped routing new connections to it.
- At T+17.5 seconds, the JVM exited and existing connections were reset.
- At T+180 seconds, ALB finally removed the target and ended its drainage window.
The pod was dead for 162.5 seconds while ALB still retained it in the drain pool. Existing keep-alive connections could remain open during that window. Any request still in flight when the JVM exited could receive a TCP reset, which callers observed as an HTTP 502.
A 15-second preStop hook and the default non-graceful application shutdown left the pod alive for only about 17.5 seconds. ALB’s 180-second deregistration delay was therefore the dominant part of the failure window.
Establishing a repeatable baseline
I reproduced the failure in a controlled chaos test with five pods, 5,000 concurrent workers, and a 120-second load window. One pod was force-deleted during the test.
The baseline run processed 112,697 requests and produced:
- 2,530 HTTP 502 responses.
- 222 HTTP 504 responses.
- 2.4% total request errors.
The first major 502 burst appeared approximately 17 seconds after deletion, matching the JVM termination timestamp from the root-cause analysis.
Aligning the full request lifecycle
The fix coordinated application shutdown, Kubernetes termination, ALB drainage, and replacement-pod readiness instead of changing one timeout in isolation.
Drain before terminating the application
I reduced the ALB deregistration delay from 180 seconds to 30 seconds and increased preStop from 15 seconds to 35 seconds. This inverted the broken relationship: the pod now remains alive after ALB completes its 30-second drain window.
The termination grace period increased from 60 seconds to 90 seconds to cover the complete sequence without prematurely killing the pod.
Complete in-flight requests
The application moved to graceful shutdown with a 40-second shutdown-phase timeout. After the 35-second preStop, the JVM could finish active requests before exiting. The timeout placed an upper bound on shutdown rather than allowing it to block indefinitely.
The resulting sequence was:
- ALB starts draining at approximately T+0.6 seconds.
- ALB completes drainage at approximately T+30.6 seconds.
preStopends at T+35 seconds and the application receives its termination signal.- Graceful shutdown finishes remaining requests before the pod terminates.
Bring replacements into service sooner
The original startup delay also kept replacement pods out of service for too long, forcing surviving pods to absorb the shifted load. I reduced the startup probe’s initial delay from 90 seconds to 30 seconds while retaining enough failure allowance for slow starts.
ALB health checks became faster as well: the interval changed from 10 seconds to 5 seconds, and healthy and unhealthy thresholds changed from 4 to 2. A healthy replacement could become ALB-eligible in about 10 seconds instead of 40 seconds.
Validating the same failure conditions
I repeated the chaos test with the same five pods, 5,000 concurrent workers, and 120-second duration. The after-fix run processed 270,699 requests with:
- 12 HTTP 502 responses, down from 2,530: a 99.5% reduction.
- 12 HTTP 504 responses, down from 222: a 94.5% reduction.
- 0.008% total request errors, down from 2.4%: a 99.1% reduction in total errors.
The termination-related burst at the 17-second mark was gone. The remaining errors did not follow the old lifecycle pattern: the 504s occurred before deletion during initial queueing, while the remaining 502s appeared later under extreme load as surviving pods absorbed the traffic shift.
Those residual failures informed the probe optimization rather than being hidden behind the headline result.
Production outcome
The change was deployed through Argo Rollouts using a canary strategy and validated with Coralogix APM. For one of the company’s most critical production services, deployment-time errors are now practically zero.
This service became the rollout benchmark to be implemented across 350+ production services. The result is a reusable lifecycle standard, not just a one-service timeout adjustment.