Lesson 1 of 2 Elastic Load Balancing and Auto Scaling

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.

Application Load Balancer
Layer7 (HTTP and HTTPS)
RoutingBy path, host, headers or query string
TargetsEC2, IP addresses, containers, Lambda
ExtrasUser authentication with Cognito or OIDC
Network Load Balancer
Layer4 (TCP and UDP)
RoutingBy port
TargetsEC2, IP addresses, containers, ALBs
ExtrasVery high performance and static IP addresses

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.

An Application Load Balancer health check. The load balancer sends an HTTP request to /health on every target in the target group. Healthy targets receive traffic; a target that fails the check stops receiving traffic, and its target health reason explains why, such as a wrong path or port or a blocking security group.
An unhealthy target stays registered but receives no requests until it passes the check again.

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-For header.
  • 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.
Scenario questions

Requests behind a load balancer

Exam skills 4.2.5 · 1.1.3

Practise the settings that shape requests and sessions behind a load balancer.

Log in to try this activity Interactive activities are free with an account.

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.

CodeUsual meaning
502The target closed the connection or sent an invalid response
503No healthy targets in the target group
504The target did not respond in time
Sort into groups

Decode the load balancer error

Exam skills 4.1.1

Match each cause with the error the load balancer returns.

Log in to try this activity Interactive activities are free with an account.
Worked example

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.

SymptomMeaningLikely fix
ALB returns 503No healthy targets in the target groupFix the health check or the application
ALB returns 502The target sent an invalid response or closed the connectionCheck application crashes; keep-alive timeout longer than the ALB's idle timeout
ALB returns 504The target did not respond in timeSpeed up the request or raise the idle timeout
Targets flip between healthy and unhealthyHealth check too strict or path too slowUse a lightweight /health path; adjust thresholds
Client IPs in logs are all the ALB'sThe app reads the connection addressRead 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.

Check yourself

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-For header.
  • 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.