I configured my load balancer to route traffic equally between Amazon Elastic Compute Cloud (Amazon EC2) instances or across Availability Zones. However, Elastic Load Balancing (ELB) routes more traffic to one Amazon EC2 instance or Availability Zone than the others.
Short description
ELB might unequally route traffic to your targets for the following reasons:
- Clients route the requests to the IP address of a load balancer node with a DNS record that has an expired time to live (TTL).
- You activated sticky sessions (session affinity) for the load balancer.
- Available and healthy EC2 instances aren't evenly distributed across Availability Zones.
- Instances of a specific capacity type aren't evenly distributed across Availability Zones.
- There are long-lasting TCP connections between clients and instances.
Resolution
Confirm that there's a traffic imbalance
To confirm a traffic imbalance, analyze the ELB access logs. ELB adds individual files for each ELB node to your Amazon Simple Storage Service (Amazon S3) bucket. You can compare the number of lines in your access log files over a specific time period.
Run the following command based on your load balancer type to find the number of requests that the load balancer routes to specific applications.
Application Load Balancer:
awk '{print $5}' *.log | awk -F ":" '{print $1}' | sort | uniq -c | sort -r
Classic Load Balancer:
awk '{print $4}' *.log | awk -F ":" '{print $1}' | sort | uniq -c | sort -r
Flush the DNS cache
Routing based on outdated DNS entries results in an imbalanced RequestCount pattern across different Availability Zones.
It's a best practice to use the DNS TTL so that the resolver automatically picks up new records. However, if you encounter a stale cache, then you can flush the client's DNS cache to confirm that the client uses the latest DNS records.
For Linux clients that use nscd for DNS caching, run one of the following commands:
systemctl restart nscd
service nscd restart
For Linux clients that dnsmasq for DNS caching, run one of the following commands:
systemctl restart dnsmasq
service dnsmasq restart
For Linux clients that use BIND for DNS caching, run one of the following commands:
systemctl restart named
service named restart
rndc flush
For Windows clients, run the following command:
ipconfig /flushdns
If you cleared your client's DNS cache but still experience caching issues, then make sure that your client application doesn't cache DNS records.
Update your sticky session configuration
Sticky sessions use cookies to help the client maintain a connection to the same instance over a cookie's lifetime. As a result, sticky sessions can cause imbalances over time.
If you use duration-based session stickiness, then reduce the cookie expiration time.
If you set session stickiness from individual applications, then configure the application to issue session cookies that clear on browser close.
Make sure that your instances are evenly distributed across Availability Zones
There might be an uneven number of available healthy instances in your Availability Zones. If you deactivated cross-zone load balancing, then ELB must balance requests across fewer instances in the affected Availability Zones. To compensate, the remaining healthy instances process a higher number of requests.
Note: A traffic load imbalance across instances or Availability Zones doesn't necessarily mean that resource usage is also imbalanced. For example, an imbalance can happen when one of the load balancer's instances processes requests faster than the others.
To resolve imbalance issues, keep an equal number of instances in each activated Availability Zone. To add more instances as load balancer targets, use the following resources based on your load balancer type:
For Classic Load Balancers and Network Load Balancers, it's a best practice to activate cross-zone load balancing. Cross-zone load balancing distributes requests at the instance level instead of the Availability Zone level.
Note: Application Load Balancers have cross-zone load balancing activated by default.
Check your instance type distribution
A Classic Load Balancer with HTTP or HTTPS listeners might route more traffic to higher-capacity instance types. This occurs to prevent too many outstanding requests in lower-capacity instance types. It's a best practice to use similar instance types and configurations to reduce the likelihood of capacity gaps and traffic imbalances.
A traffic imbalance might also occur if you have instances of similar capacities that run on different Amazon Machine Images (AMIs). However, in this scenario, it's a best practice for the load balancer to favor higher-capacity instance types.
Check for long-running TCP connections
ELB uses the round robin routing algorithm for TCP traffic on Classic Load Balancers and flow hash algorithm for Network Load Balancers. By design, long-running TCP connections between clients and instances cause uneven traffic load distribution. As a result, new instances take longer to reach connection equilibrium.
With TCP listeners, the load balancer distributes traffic only at the connection level. Connections and HTTP requests aren't always one to one. Clients that frequently reuse connections to send and receive multiple HTTP requests might produce unbalanced traffic at the instance level. If your application supports higher-layer network protocols such HTTP, HTTPS, or HTTP2, then migrate to an Application Load Balancer that functions at the application layer.