An ALB returns 502 intermittently during deployments. How do you stop that?
IntermediateScenarioEC2, Auto Scaling and Load Balancing
Answer
Situation read → Targets are being deregistered with in-flight requests, or the app closes keep-alive connections early.
What I'd do →
- Set deregistration delay to match the longest request, such as 30 to 60 seconds
- Make the app handle SIGTERM and finish requests
- Set app keep-alive timeout greater than the ALB idle timeout (60 s default)
- Use slow start for new targets
- Check
HTTPCode_ELB_502_Countand target response time metrics
Why → A backend closing idle connections sooner than the ALB expects causes 502s on reused connections.
Interview tip → State the rule: backend keep-alive greater than load balancer idle timeout.