Q: A customer complains of intermittent 502 Bad Gateway errors from an AWS Application Load Balancer. The backend instances show low CPU. What should you look for?
A 502 Bad Gateway from an ALB means the ALB tried to communicate with the target instance, but the connection dropped or the target returned an invalid response.
Want to master this scenario in a live sandbox? KodeKloud's Istio Service Mesh & Advanced Kubernetes Networking Course covers this exact problem with hands-on terminal drills.
🛠️ Production Runbook & Step-by-Step Resolution
Direct Answer: "When an interviewer asks how I diagnose network connectivity, I follow an outside-in OSI model approach. The interviewer is testing: Load balancer timeout configurations, application keep-alive.. I structure my answer around systematic triage first, root cause analysis second, and permanent remediation third."
Production Solution & Architecture
A 502 Bad Gateway from an ALB means the ALB tried to communicate with the target instance, but the connection dropped or the target returned an invalid response. The most common cause, aside from the app actually crashing, is a mismatch in Keep-Alive timeouts. If the backend web server (like Nginx or Node.js) has an idle timeout configured shorter than the ALB's idle timeout (default 60 seconds), the backend might close the TCP connection just as the ALB decides to send a new request down that established pipe. The ALB gets a connection reset and throws a 502. The fix is to ensure the backend application's idle timeout is greater than the ALB's idle timeout.
- Immediate Triage: A 502 Bad Gateway from an ALB means the ALB tried to communicate with the target instance, but
- Run targeted verification commands before modifying configuration.
- Automate permanent guardrails (CI check, alerts, IaC policy) to prevent recurrence.
Senior hiring managers and bar raisers evaluate how you prioritize during outages. Avoid these common anti-patterns:
- Confusing ALB 502 Bad Gateway (target connection reset) with 504 Gateway Timeout (upstream response timeout) when configuring Networking customer complains 502 indicates the backend target closed the TCP connection prematurely or returned malformed HTTP, whereas 504 means the target failed to respond before idle timeout.
- Setting target deregistration delay to zero instead of allowing connection draining when configuring Networking customer complains Instant target deregistration severs in-flight HTTP requests during auto-scaling scale-in or rolling deployments, causing immediate user-facing drops.
-
Failing to query CloudWatch ELB metrics (
HTTPCode_ELB_5XXvsHTTPCode_Target_5XX) during load balancer outages when configuring Networking customer complains Comparing load balancer generated errors against target generated errors immediately isolates whether the issue lies in the proxy tier or application runtime. - Overlooking Security Group rules on backend EC2/ECS instances that block health check traffic from the ALB when configuring Networking customer complains Target instances must explicitly permit inbound HTTP/HTTPS traffic from the ALB security group on the configured container/application port.