Sub topic 6
Spreading traffic with load balancers and cross-zone load balancing
This chapter assumes you know what a VPC, a subnet, and a target group are, and that your subnets already span at least two Availability Zones (AZs). It covers how a load balancer spreads requests across AZs and what cross-zone load balancing changes.
How a load balancer uses AZs
When you create an Elastic Load Balancing (ELB) load balancer, you choose the subnets it uses. AWS places a load balancer node in each AZ you select. The load balancer's DNS name resolves to the IP addresses of those nodes. A client picks one address, so traffic arrives at the nodes in roughly equal shares.
Two rules follow from this:
- Select a subnet in every AZ where you run targets. A target in an AZ with no load balancer node is not used by an Application Load Balancer (ALB) in the usual way.
- Select at least two AZs. A load balancer in one AZ is itself a single point of failure.
What cross-zone load balancing does
Each node can either send requests only to targets in its own AZ, or send them to targets in all enabled AZs. The second behavior is cross-zone load balancing.
Suppose AZ A has 2 targets and AZ B has 8, and each AZ's node receives 50% of the traffic.
| Setting | Each target in AZ A gets | Each target in AZ B gets |
|---|---|---|
| Cross-zone off | 25% | 6.25% |
| Cross-zone on | 10% | 10% |
With cross-zone off, the two small-zone targets are overloaded. With it on, the load is even per target.
Defaults by load balancer type
| Type | Cross-zone default |
|---|---|
| Application Load Balancer | On at the load balancer level, and you cannot turn it off there. You can turn it off per target group. |
| Network Load Balancer | Off |
| Gateway Load Balancer | Off |
Check the current ELB documentation for any default changes before you rely on them.
Enable it on a Network Load Balancer target group like this:
aws elbv2 modify-target-group-attributes \
--target-group-arn "$TARGET_GROUP_ARN" \
--attributes Key=load_balancing.cross_zone.enabled,Value=trueTrade-offs and common errors
Cross-zone traffic is not free everywhere. An ALB does not charge for the cross-AZ traffic between its nodes and targets. For a Network Load Balancer, cross-zone traffic is billed as regular inter-AZ data transfer. Cross-zone also adds a small amount of latency, because some requests cross an AZ boundary.
Common mistakes:
- Uneven target counts per AZ with cross-zone off. This is the overload case above. Keep counts equal, or turn cross-zone on.
- Missing subnet for an AZ. Targets in that AZ receive no traffic from the load balancer.
- Health checks that fail an entire AZ. If every target in an AZ is unhealthy and cross-zone is off, that AZ's node has nowhere to send requests. The next chapters cover health checks and failover in detail.
- Assuming the load balancer shifts traffic instantly. Clients with cached DNS answers can keep sending to a failed node for a short time.
Exercise
An NLB has 3 targets in AZ A and 1 target in AZ B. Cross-zone load balancing is off and traffic is evenly split between the two AZ nodes. What share of requests does the single AZ B target receive, and what would it receive with cross-zone on?
Show answer
With cross-zone off, each node serves only its own AZ, so the AZ B target receives 50% of requests. With cross-zone on, requests are spread across all 4 targets, so it receives 25%.