Sub topic 6
Spreading traffic with load balancers and cross-zone load balancing
This chapter covers how Elastic Load Balancing (ELB) distributes traffic across Availability Zones (AZs). It also covers what cross-zone load balancing changes and how to configure it. It assumes you know what a VPC, a subnet, and a target group are, and that you can read the AWS CLI and YAML. The earlier chapters on subnet layout and failure domains are the background.
The working model: nodes in zones
A load balancer is not a single machine. When you create an Application Load Balancer (ALB) or Network Load Balancer (NLB), AWS places load balancer nodes in each subnet you enable, one per AZ. The load balancer's DNS name resolves to the IP addresses of those nodes. Clients pick one address, so traffic arrives at a node in some AZ.
Two decisions then determine where a request ends up:
- Which node receives it. This depends on DNS resolution and the client's choice of IP address.
- Which target that node sends it to. This depends on whether cross-zone load balancing is on.
You enable AZs by choosing subnets. A load balancer can only send traffic to targets in AZs it has enabled. Enable at least two AZs, with one subnet per AZ, for every internet-facing or internal load balancer that fronts a production tier.
The dashed lines exist only when cross-zone load balancing is enabled.
What cross-zone load balancing does
With cross-zone load balancing off, each node sends traffic only to targets in its own AZ. With it on, each node can send traffic to every registered target in all enabled AZs.
Each node receives an equal share of client traffic. Take two AZs where AZ A has 2 targets and AZ B has 8 targets:
| Setting | Each node's traffic share | Load on each AZ A target | Load on each AZ B target |
|---|---|---|---|
| Off | 50% per node | 25% of total | 6.25% of total |
| On | 50% per node | 10% of total | 10% of total |
With cross-zone off, the two AZ A targets carry 25% of all traffic each, while the eight AZ B targets carry about 6% each. Uneven target counts, or a few unhealthy targets in one AZ, create hot spots. Cross-zone on spreads requests per target instead of per node.
Defaults differ by load balancer type
The default setting depends on the load balancer type, and on where you set it.
| Load balancer | Cross-zone default | Where it is configured | Extra data charge |
|---|---|---|---|
| Application Load Balancer | On | Load balancer level, with a target group override | No charge for cross-zone traffic |
| Network Load Balancer | Off | Load balancer level or target group attribute | Inter-AZ data transfer charges apply |
| Gateway Load Balancer | Off | Load balancer attribute | Inter-AZ data transfer charges apply |
| Classic Load Balancer | On for new ones created through the console, and off by default through the API and CLI | Load balancer attribute | No charge for cross-zone traffic |
Verify these defaults against the current ELB documentation before you rely on them, because they have changed over time. The practical consequence is for NLBs. A balanced NLB deployment needs you to decide on purpose, and cross-zone traffic is billed as regional data transfer.
For an ALB, you can override the load balancer's setting per target group. Setting a target group attribute to use_load_balancer_configuration makes it inherit the load balancer value. Setting it to true or false overrides it.
Enable cross-zone load balancing on a target group with the CLI:
aws elbv2 modify-target-group-attributes \
--target-group-arn "$TARGET_GROUP_ARN" \
--attributes Key=load_balancing.cross_zone.enabled,Value=trueConfirm the result:
aws elbv2 describe-target-group-attributes \
--target-group-arn "$TARGET_GROUP_ARN" \
--query 'Attributes[?Key==`load_balancing.cross_zone.enabled`]'Placing the load balancer in subnets
Create the load balancer with one public subnet per AZ for an internet-facing design. Targets usually sit in private subnets in the same AZs. The load balancer must have a subnet in every AZ where you want it to reach targets.
Resources:
WebAlb:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Type: application
Scheme: internet-facing
Subnets:
- !Ref PublicSubnetA
- !Ref PublicSubnetB
SecurityGroups:
- !Ref AlbSecurityGroup
WebTargetGroup:
Type: AWS::ElasticLoadBalancingV2::TargetGroup
Properties:
VpcId: !Ref Vpc
Protocol: HTTP
Port: 80
TargetType: instance
HealthCheckPath: /health
HealthCheckIntervalSeconds: 10
HealthyThresholdCount: 2
UnhealthyThresholdCount: 2The Auto Scaling group in the next chapters registers instances in the target group. The group's TargetGroupARNs property joins the two.
How failure and health interact with zones
Health checks decide which targets receive traffic. When a target fails its checks, the load balancer stops routing to it. What happens next depends on cross-zone.
- Cross-zone on. Every node excludes the unhealthy targets and spreads load over the remaining healthy ones in all AZs. A failure in one AZ does not stop traffic from nodes in other AZs.
- Cross-zone off. A node only considers targets in its own AZ. If every target in AZ A fails, the AZ A node has no healthy target of its own. The load balancer then relies on its failure behavior, described below.
When no targets are healthy in an AZ, the load balancer's DNS-level health handling can remove that AZ's node from DNS answers. This is called zonal shift or DNS failover, and it relies on Route 53 health checks that ELB manages. Clients with cached DNS answers can keep using the old address until the record's time to live (TTL) expires, so recovery is not instant. For ALB and NLB, the nodes' DNS records use a 60-second TTL.
If all targets in all enabled AZs are unhealthy, ELB fails open. It routes to all targets regardless of health, on the assumption that a misconfigured health check is more likely than a total outage.
Zonal shift and Application Recovery Controller
Amazon Application Recovery Controller (ARC) zonal shift lets you move traffic away from an AZ for a load balancer. You start a shift and ELB stops sending traffic to that AZ's node and its targets. You can also enroll resources in zonal autoshift, where AWS starts a shift for you when it detects an AZ impairment. Zonal shift works with cross-zone load balancing on or off, but with cross-zone off the shift is only safe if the remaining AZs can absorb the load. The next chapters on static stability cover capacity.
Start a shift with the CLI. The resource identifier is the load balancer ARN:
aws arc-zonal-shift start-zonal-shift \
--resource-identifier "$ALB_ARN" \
--away-from us-east-1a \
--expires-in 30m \
--comment "Testing zonal shift"Use AZ IDs or names according to the API's documentation. Shifts expire, so a forgotten test does not leave an AZ out of service for good.
Choosing a setting
Use these criteria.
- Default to cross-zone on for ALBs. It already is the default, and it gives the most even load per target.
- For NLBs, turn cross-zone on when targets are uneven or capacity is thin. Accept the data transfer cost. Keep it off when targets are balanced across AZs, latency matters, and you want to avoid inter-AZ traffic.
- Keep target counts equal per AZ when cross-zone is off. An Auto Scaling group with balanced AZ distribution helps here.
- Check backend limits. Cross-zone on lets every node connect to every target, so a target sees connections from nodes in all AZs. For small fleets this is rarely an issue.
- Plan for the loss of an AZ. With two AZs, losing one doubles the load per remaining target. Size the fleet for that.
Common errors
- Enabling only one AZ on the load balancer. Targets in other AZs never receive traffic. Check the load balancer's subnet list.
- Assuming NLB behaves like ALB. An NLB with cross-zone off and 2 targets in one AZ and 6 in another gives lopsided load.
- Targets in an AZ the load balancer has not enabled. Registration succeeds but the targets stay unused or are reported as unused.
- A health check path that depends on a downstream service. A database blip marks every target unhealthy at once, and the load balancer fails open or serves errors. Keep the health endpoint cheap and local, or separate shallow and deep checks.
- Ignoring client DNS caching. Failover through DNS takes at least the TTL, and some clients cache longer.
- Missing security group rules. The targets' security groups must allow traffic from the load balancer's security group on the traffic and health check ports.
Exercise
An NLB has cross-zone load balancing off. AZ A has 1 healthy target and AZ B has 3 healthy targets, and each load balancer node receives 50% of client traffic. What share of total traffic does the AZ A target receive, and what share does each AZ B target receive? What are the shares after you turn cross-zone on?
Show answer
With cross-zone off, the AZ A target receives 50% of total traffic, and each AZ B target receives about 16.7% (50% divided by 3). With cross-zone on, traffic spreads over all 4 targets, so each receives 25%. Turning cross-zone on removes the hot spot on the AZ A target, but on an NLB it adds inter-AZ data transfer charges.
Exercise
You run an ALB with cross-zone on and a fleet of 4 instances in each of two AZs. During a zonal shift away from AZ A, what happens to the per-instance load on AZ B, and why does this matter for capacity planning?
Show answer
All traffic goes to the 4 instances in AZ B, so each instance carries twice the load it did before (about 25% of total traffic instead of 12.5%). The remaining AZ must hold enough capacity to absorb the full workload. This is why you provision enough headroom in each AZ ahead of time instead of waiting for scaling to react.