Sub topic 10
Cross-zone load balancing and deregistration delay
This chapter assumes you know what a load balancer node, a target group and an Availability Zone (AZ) are, and that you can read basic AWS CLI output. It covers two settings that decide how evenly traffic is spread and how gracefully targets leave service: cross-zone load balancing and deregistration delay.
How a load balancer distributes traffic across zones
When you enable an AZ for a load balancer, Elastic Load Balancing places a load balancer node in that zone. DNS returns the addresses of these nodes, and each client request lands on one of them. The node then picks a target.
Cross-zone load balancing controls which targets a node may pick:
- Enabled: every node distributes requests across all registered targets in all enabled zones.
- Disabled: every node distributes requests only across targets in its own zone.
The DNS layer splits client traffic roughly equally between the zones. So with cross-zone disabled, each zone receives about the same share of traffic no matter how many targets it contains.
A worked example
Suppose zone A has 2 targets and zone B has 8 targets, and 100 requests arrive.
| Setting | Zone A target load | Zone B target load |
|---|---|---|
| Cross-zone disabled | 25 requests per target | 6.25 requests per target |
| Cross-zone enabled | 10 requests per target | 10 requests per target |
With cross-zone disabled, each zone gets 50 requests. Each of the two targets in zone A handles four times the load of each target in zone B. With cross-zone enabled, load follows the number of targets, so every target gets the same share.
Defaults differ by load balancer type
The default setting depends on the load balancer type, and the level at which you configure it also differs.
| Load balancer | Default | Where you set it | Data transfer cost between zones |
|---|---|---|---|
| Application Load Balancer | Always on | Target group attribute can override it, the load balancer level is fixed on | No charge for traffic the load balancer sends across zones |
| Network Load Balancer | Off | Load balancer attribute, and target group attribute overrides it | Charged as regional data transfer |
| Gateway Load Balancer | Off | Load balancer attribute | Charged as regional data transfer |
On an Application Load Balancer you can turn cross-zone off for a specific target group by setting the target group attribute load_balancing.cross_zone.enabled to false. The default value use_load_balancer_configuration follows the load balancer, which is always on.
To change the setting on a Network Load Balancer:
aws elbv2 modify-load-balancer-attributes \
--load-balancer-arn "$NLB_ARN" \
--attributes Key=load_balancing.cross_zone.enabled,Value=trueTo override it for one target group:
aws elbv2 modify-target-group-attributes \
--target-group-arn "$TG_ARN" \
--attributes Key=load_balancing.cross_zone.enabled,Value=falseTrade-offs
- Even load: enabling cross-zone makes load per target equal, which reduces hot spots when zones have different target counts. This matters during scale-in or when an Auto Scaling group is unbalanced.
- Failure behavior: with cross-zone disabled, a zone whose targets are all unhealthy can leave the node with nothing to route to. Clients that resolve to that node see errors until DNS stops returning it. With cross-zone enabled, the node forwards to healthy targets in other zones.
- Cost and latency: on a Network Load Balancer, cross-zone traffic is billed as inter-AZ data transfer and adds a small amount of latency. Disabling it keeps traffic inside a zone, which can lower both.
Deregistration delay
Removing a target from a target group is not instant by default. When you deregister a target, or an Auto Scaling group scales it in, the target moves to the draining state. The load balancer:
- Stops sending new requests to the target.
- Lets in-flight requests finish.
- Removes the target once the requests complete or the deregistration delay expires.
The deregistration delay is a target group attribute, deregistration_delay.timeout_seconds. The default is 300 seconds. The range is 0 to 3600.
Set the value to match your longest normal request:
- Too long: scale-in and deployments wait for the full period when connections stay open, such as long-lived WebSocket or keep-alive connections. A 300-second default can slow rolling updates for an API whose requests take 200 milliseconds.
- Too short: requests still running when the delay ends are cut off, and clients see errors.
aws elbv2 modify-target-group-attributes \
--target-group-arn "$TG_ARN" \
--attributes Key=deregistration_delay.timeout_seconds,Value=30Edge cases
- Network Load Balancer: the delay ends in-flight connections at expiry. Setting
deregistration_delay.connection_termination.enabledtotruecloses remaining connections when the timeout is reached. Otherwise they may be reset later. - Unhealthy targets: a target that fails health checks is not draining. The load balancer simply stops routing to it. The deregistration delay applies only when the target is deregistered.
- Auto Scaling interaction: the Auto Scaling group terminates an instance only after the load balancer finishes deregistering it. A long delay therefore lengthens scale-in time and the time instances keep costing money.
- Slow starts: deregistration delay is separate from the health check grace period and from instance warmup, which control scale-out timing.
You can watch the draining state with the function from the troubleshooting chapter:
import boto3
elbv2 = boto3.client("elbv2")
def print_target_health(target_group_arn):
response = elbv2.describe_target_health(TargetGroupArn=target_group_arn)
for description in response["TargetHealthDescriptions"]:
target = description["Target"]["Id"]
state = description["TargetHealth"]["State"]
print(target, state)Exercise
A Network Load Balancer has 3 targets in zone A and 9 targets in zone B, with cross-zone load balancing disabled. 120 requests arrive and DNS splits them equally. How many requests does each target in each zone handle, and what changes if you enable cross-zone?
Show answer
Each zone receives 60 requests. Each zone A target handles 20 and each zone B target handles about 6.7. With cross-zone enabled, all 12 targets share the load, so each handles 10 requests. The trade-off is that the cross-zone traffic is billed as inter-AZ data transfer on a Network Load Balancer.
Exercise
An API serves requests that finish within 2 seconds, and rolling deployments take too long because instances stay in draining for 5 minutes. What attribute do you change, and what value is reasonable?
Show answer
Lower deregistration_delay.timeout_seconds on the target group from the 300-second default to a value slightly above the longest request, for example 30 seconds. Going lower than the longest request would cut off in-flight requests.