Routing Traffic with Load Balancers and Health Checks
This lesson explains how Elastic Load Balancing spreads incoming traffic across targets and sends it only to targets that pass their health checks.
Learning outcomes
After completing this lesson, you will be able to:
- Choose between an Application Load Balancer and a Network Load Balancer.
- Explain how target group health checks decide which targets receive traffic.
- Describe how client IP addresses and sticky sessions reach the application.
- Diagnose 502, 503 and 504 errors returned by a load balancer.
ALB vs NLB
Application Load Balancers and Network Load Balancers operate at different layers of the network, which determines what they can see in each request and therefore how they can route it.
An Application Load Balancer (ALB) works at layer 7 and understands HTTP and HTTPS. Its listener rules can route each request by host name, path, HTTP headers, method or query string, so one ALB can send /api/* to a container service and /images/* to another, or shop.example.com and blog.example.com to different targets. Targets can be EC2 instances, IP addresses, containers or Lambda functions. An ALB can also authenticate users with Cognito or any OpenID Connect provider before requests reach the application, and supports WebSockets and sticky sessions.
A Network Load Balancer (NLB) works at layer 4 and forwards TCP, UDP and TLS connections without inspecting their content, so it can route only by port. It handles very high volumes of connections with low latency, provides a static IP address in each Availability Zone, which clients can add to firewall allow lists, and preserves the client's IP address. It suits non-HTTP protocols, extreme performance and services exposed through AWS PrivateLink. An ALB can be a target of an NLB, combining static addresses with HTTP routing. The following table summarises the differences.
Both can terminate TLS using ACM certificates.
Health checks
Each target group has a health check, such as an HTTP request to /health. Unhealthy targets stop receiving traffic, and the target health reason explains why a check fails, for example a wrong path or port or a blocking security group.
The following diagram shows how a health check decides which targets receive traffic.

Requests and sessions
A load balancer affects how requests and sessions reach the application. The following list describes the relevant behaviour and settings.
- The client's real IP address reaches the application in the
X-Forwarded-Forheader. - Sticky sessions keep a user on one target, but storing state externally is usually better.
- The deregistration delay (connection draining) lets in-flight requests finish before a target is removed.
Requests behind a load balancer
Common error codes
When a load balancer cannot obtain a valid response from a target, it returns an error code of its own, and each code points to a different cause.
A 502 Bad Gateway means the load balancer reached a target but the connection failed or the response was unusable: the application closed the connection, crashed while processing the request, or returned a malformed response, such as an invalid Lambda result. A common cause is an application keep-alive timeout shorter than the load balancer's idle timeout, so the application closes connections that the load balancer is about to reuse. A 503 Service Unavailable means there was no target to send the request to, usually because no targets are registered or all have failed their health checks. A 504 Gateway Timeout means the target accepted the request but did not respond within the idle timeout, which is 60 seconds by default, so the request is too slow or a dependency of the target is not answering. The following table summarises the codes.
| Code | Usual meaning |
|---|---|
502 | The target closed the connection or sent an invalid response |
503 | No healthy targets in the target group |
504 | The target did not respond in time |
Decode the load balancer error
Reading load balancer errors
Load balancer error codes indicate specific problems with targets. The following table gives the meaning of several common symptoms and the likely remedy for each.
| Symptom | Meaning | Likely fix |
|---|---|---|
| ALB returns 503 | No healthy targets in the target group | Fix the health check or the application |
| ALB returns 502 | The target sent an invalid response or closed the connection | Check application crashes; keep-alive timeout longer than the ALB's idle timeout |
| ALB returns 504 | The target did not respond in time | Speed up the request or raise the idle timeout |
| Targets flip between healthy and unhealthy | Health check too strict or path too slow | Use a lightweight /health path; adjust thresholds |
| Client IPs in logs are all the ALB's | The app reads the connection address | Read the X-Forwarded-For header |
Errors that come from targets themselves appear as HTTPCode_Target_5XX_Count, while errors generated by the load balancer appear as HTTPCode_ELB_5XX_Count, which tells the developer where to look first.
Which load balancer routes requests by URL path to different target groups?
Show answer
An Application Load Balancer, which works at layer 7 and supports path-based and host-based routing. A Network Load Balancer works at layer 4 and routes TCP and UDP connections.
Exam Tips
Remember for the exam
- To route requests by URL path to different services, use Application Load Balancer rules.
- To get the client's IP address behind an ALB, read the
X-Forwarded-Forheader. - An ALB that returns 503 has no healthy targets in the target group.
- To let in-flight requests finish during deployments, set a deregistration delay.