Sub topic 3
Elastic Load Balancing and choosing a load balancer type
This chapter assumes you know basic EC2 and VPC concepts (instances, security groups, subnets, Availability Zones) and can run AWS CLI commands. It covers what Elastic Load Balancing (ELB) does and how to pick among its load balancer types. Listeners, rules, target groups and health checks get their own chapters.
What Elastic Load Balancing does
Elastic Load Balancing is a managed AWS service that accepts incoming traffic and distributes it across registered targets, such as EC2 instances, IP addresses or Lambda functions. It sends traffic only to targets that pass health checks. This gives you two things:
- Horizontal scaling. Clients talk to one stable endpoint while the number of targets behind it changes.
- Availability. If a target or an entire Availability Zone fails, the load balancer stops sending it traffic.
When you create a load balancer, you choose the subnets it uses, at most one per Availability Zone. AWS places load balancer nodes in those subnets. The load balancer's DNS name resolves to the addresses of those nodes. AWS adds and removes nodes as traffic changes, so you do not size the load balancer yourself.
Every load balancer has a scheme:
- internet-facing: nodes get public IP addresses and accept traffic from the internet. The subnets must have a route to an internet gateway.
- internal: nodes get only private addresses and are reachable from inside your VPC or connected networks.
A common pattern is an internet-facing load balancer for the web tier and an internal one between the web tier and the application tier.
The load balancer types
ELB offers three current types and one legacy type. The type is fixed at creation, and you cannot convert one into another.
| Type | OSI layer | Protocols | Typical use |
|---|---|---|---|
| Application Load Balancer (ALB) | 7 | HTTP, HTTPS, gRPC, WebSocket | Web apps, APIs, microservices, content-based routing |
| Network Load Balancer (NLB) | 4 | TCP, UDP, TCP_UDP, TLS | Very high throughput, static IPs, non-HTTP protocols, PrivateLink |
| Gateway Load Balancer (GWLB) | 3 | IP packets over GENEVE | Firewalls, intrusion detection, deep packet inspection appliances |
| Classic Load Balancer | 4 and 7 | HTTP, HTTPS, TCP, SSL | Legacy only. Do not use it for new designs. |
Application Load Balancer
An ALB reads the HTTP request, so it can route on host name, path, headers, query strings and HTTP method. One ALB can serve many applications. For example, /api/* goes to one target group and /images/* to another. It can also return fixed responses, redirect HTTP to HTTPS, and authenticate users through OIDC or Amazon Cognito. Targets can be instances, IP addresses or Lambda functions. The ALB opens its own connection to the target, so the target sees the ALB's address as the source. The original client address arrives in the X-Forwarded-For header.
Network Load Balancer
An NLB forwards connections at layer 4 without inspecting application data. It handles millions of requests per second at low latency. It provides one static IP address per enabled Availability Zone, and you can assign your own Elastic IP addresses. For instance targets, it preserves the client's source IP address. It is the only ELB type besides GWLB that can front a VPC endpoint service (AWS PrivateLink), and it supports UDP. It can also terminate TLS, but it cannot route on HTTP content.
Gateway Load Balancer
A GWLB is for inserting a fleet of third-party or custom network appliances into a traffic path. Traffic reaches it through Gateway Load Balancer endpoints, which you reference in route tables. The GWLB wraps each packet in GENEVE (UDP port 6081), sends it to an appliance, and forwards the returning packet. It keeps the original packet unchanged, so the appliance sees the real source and destination addresses. You will not use it for ordinary web traffic.
Choosing a type
Use these criteria:
- Protocol. UDP, raw TCP or a custom protocol means NLB. Web traffic means ALB.
- Routing logic. Routing on request content requires an ALB. An NLB chooses a target by flow hashing, so it cannot read paths or hosts.
- Static addresses. An ALB's IP addresses change over time, so clients must use its DNS name. If partners need to allow-list fixed IPs, use an NLB. You can also register an ALB as a target of an NLB, or put AWS Global Accelerator in front.
- Source IP. An NLB can pass the client IP straight to instance targets. Behind an ALB, read
X-Forwarded-For. - Appliances or PrivateLink. Inline inspection means GWLB. Exposing a service to other VPCs through PrivateLink means NLB or GWLB.
Inspecting what you have
This command lists each load balancer in the current Region with its type and scheme:
aws elbv2 describe-load-balancers \
--query "LoadBalancers[].[LoadBalancerName,Type,Scheme,join(',',AvailabilityZones[].ZoneName)]" \
--output tableThe Type column shows application, network or gateway. Classic Load Balancers do not appear here. They use the older aws elb command.
Common errors
- Single subnet or single AZ. An ALB requires subnets in at least two Availability Zones. Even where one is allowed, a single zone removes the availability benefit.
- Wrong scheme. An internet-facing load balancer in subnets without a route to an internet gateway is unreachable from the internet. An internal one never gets a public address.
- Choosing NLB only for speed. ALB performance is enough for most web workloads. Picking NLB then costs you path routing, redirects and request-level metrics.
- Forgetting zone behavior. Cross-zone load balancing is always on for ALB. It is off by default for NLB and GWLB, so uneven target counts across zones lead to uneven load. A later chapter covers this.
Exercise
A partner will only allow connections from a fixed set of IP addresses, and your service speaks plain TCP on port 5000. Which load balancer type fits, and why would an ALB be a poor choice?
Show answer
Use a Network Load Balancer. It works at layer 4 for TCP, and it provides one static IP per Availability Zone, which can be your own Elastic IPs. An ALB handles only HTTP, HTTPS and gRPC, not arbitrary TCP, and its IP addresses change over time, so the partner could not allow-list them reliably.