Sub topic 9
Cross-zone load balancing and deregistration delay
This chapter assumes you know how a load balancer, listener and target group relate, and that you can run aws elbv2 commands. It covers two settings that decide how evenly traffic is spread and how gracefully targets leave.
How traffic is split across zones
A load balancer places a node in each enabled Availability Zone, and DNS spreads clients across those nodes. With cross-zone load balancing off, each node sends requests only to targets in its own zone. With it on, each node sends requests to all registered targets in every enabled zone.
Suppose zone A has two targets and zone B has one. Each zone's node receives 50% of the traffic.
With cross-zone off, the lone target in zone B carries twice the load of each target in zone A. With it on, all three targets receive about a third.
Defaults by load balancer type
| Load balancer | Default | Can you change it? | Inter-zone data charge |
|---|---|---|---|
| Application | On | Not at load balancer level. You can turn it off per target group. | None |
| Network | Off | Yes | Yes, when enabled |
| Gateway | Off | Yes | Yes, when enabled |
Enable it on a Network Load Balancer like this:
aws elbv2 modify-load-balancer-attributes \
--load-balancer-arn "$NLB_ARN" \
--attributes Key=load_balancing.cross_zone.enabled,Value=trueDeregistration delay
When you deregister a target, or Auto Scaling scales in an instance, the target does not disappear at once. It enters the draining state. The load balancer stops sending it new requests, and in-flight requests get time to finish. This time is the deregistration delay, 300 seconds by default (range 0 to 3600). When it expires, the load balancer closes any remaining connections.
If a draining target has no in-flight requests or active connections, deregistration completes right away. The target still shows as draining until the timeout expires.
Match the delay to your longest normal request. A service with 5-second requests gains nothing from 300 seconds, and a slow delay lengthens deployments and scale-in. A file-upload service may need more.
aws elbv2 modify-target-group-attributes \
--target-group-arn "$TG_ARN" \
--attributes Key=deregistration_delay.timeout_seconds,Value=60The Auto Scaling group waits for deregistration to finish before it terminates a scaled-in instance. This is why a long delay also slows scale-in.
Exercise
A Network Load Balancer has cross-zone off, with four targets in zone A and one in zone B. Which target is overloaded, and what does enabling cross-zone cost?
Show answer
The single target in zone B receives half of all traffic, while each zone A target receives about 12.5%. Enabling cross-zone gives each target about 20%, but you pay for the cross-zone data transfer.
Exercise
Your requests finish within 10 seconds, yet scale-in takes five minutes per instance. What setting do you check, and what is a reasonable change?
Show answer
Check the target group's deregistration delay, which defaults to 300 seconds. Lower it, for example to 30 or 60 seconds, so it covers your longest request with some margin.