Deployments cause 502 errors for a few seconds behind the load balancer. How do you stop that?
IntermediateScenarioLoad Balancing, Proxies & CDN
Answer
Situation read → Targets are being removed while requests are in flight, or new targets get traffic before they are ready.
What I'd do →
- Set the target group deregistration delay (default 300s) to cover your longest request:
aws elbv2 modify-target-group-attributes --attributes Key=deregistration_delay.timeout_seconds,Value=30 - On shutdown handle SIGTERM: stop accepting new work, finish in-flight requests, then exit
- In Kubernetes add a
preStopsleep and a readiness probe - Ensure the app keep-alive timeout is longer than the LB idle timeout to avoid reused connection resets
Why → 502s on deploy are race conditions between LB state and app lifecycle.
Interview tip → Mention the keep-alive mismatch as the most common hidden cause.
More on Load Balancing, Proxies & CDN
What is the difference between a Layer 4 and a Layer 7 load balancer?BeginnerWhen do you choose an ALB and when an NLB in AWS?BeginnerForward proxy versus reverse proxy: what is the difference?BeginnerHow do health checks work on a load balancer and what makes a good one?BeginnerWhat are sticky sessions and why would you avoid them?BeginnerWhat does a CDN do and when does it help?Beginner