Scaling Behind a Load Balancer

Topics Covered

Elastic Load Balancing and the Application Load Balancer

What a load balancer provides

The Application Load Balancer

Target types for an ALB

Authentication and security at the ALB

TLS termination and certificates

Client addresses at the targets

Connection draining and slow start

ALB limits worth knowing

Listener rule details that questions test

Target group settings

Internet facing and internal load balancers

Access logs and monitoring

Most web architectures on the exam put a fleet of instances or containers behind a load balancer and let an Auto Scaling group change the size of the fleet. Questions test which load balancer fits a requirement, how health checks and scaling policies behave and how to make the fleet stateless so that scaling is safe. This lesson works through those decisions. The hands on configuration is covered in ALB, NLB and Target Groups and Auto Scaling Groups and Launch Templates in the AWS in Practice course.

What a load balancer provides

Elastic Load Balancing distributes incoming traffic across multiple targets, such as EC2 instances, containers, IP addresses and Lambda functions, in one or more Availability Zones. It gives clients a single DNS name, so the targets behind it can change at any time. It checks the health of targets and only sends traffic to healthy ones. It scales its own capacity automatically with traffic. It can terminate TLS, offloading encryption work from the targets and centralising certificate management through AWS Certificate Manager. And it integrates with Auto Scaling, so new instances register automatically and terminating instances are drained gracefully.

A load balancer is a regional service. It has nodes in each Availability Zone you enable, and for high availability you should enable at least two zones and place targets in each. Cross-zone load balancing spreads traffic evenly across all registered targets in all zones rather than evenly across zones, which matters when zones have different numbers of targets. For ALBs, it is always on; for NLBs and GWLBs, it is off by default and can be turned on, with charges for data crossing zones.

The Application Load Balancer

The Application Load Balancer works at layer 7, the HTTP layer. It understands HTTP and HTTPS requests, including HTTP/2, gRPC and WebSockets, and can route each request based on its content. A listener on a port, typically HTTPS on 443, has rules evaluated in priority order. Each rule has conditions on the host header, the path, HTTP headers, the HTTP method, query string parameters or the source IP address, and an action: forward to one or more target groups, with optional weights, redirect, such as HTTP to HTTPS, return a fixed response, or authenticate users.

An ALB routes HTTP requests by path, host and headers.

Content based routing lets one ALB serve many applications. Requests for api.example.com go to the API target group and requests for www.example.com to the web target group. Requests for /images go to a static content service and /checkout to the checkout service. Weighted target groups send, for example, 90 percent of traffic to the current version and 10 percent to a new one, which supports blue and green and canary deployments without DNS changes.

Target types for an ALB

ALB target groups can contain instances, IP addresses, including addresses on premises reached through Direct Connect or VPN, and Lambda functions. ECS services register their tasks as IP targets automatically, with dynamic port mapping so many tasks can run on one instance. A Lambda target lets an ALB invoke a function per request, a simple way to serve HTTP from Lambda without API Gateway when API Gateway's extra features are not needed.

Authentication and security at the ALB

An ALB listener rule can authenticate users before requests reach the application, through an OpenID Connect identity provider or Amazon Cognito user pools, which in turn support social and SAML identity providers. The application receives the user's claims in headers and no longer needs its own login code. AWS WAF can be attached to an ALB to filter requests. Security groups on the ALB control which clients can connect, and the targets' security groups should allow traffic only from the ALB's security group.

TLS termination and certificates

The ALB terminates TLS using certificates from AWS Certificate Manager, which renews them automatically. Server Name Indication lets one HTTPS listener serve several domains, each with its own certificate. Security policies control which TLS versions and ciphers are accepted. Traffic from the ALB to targets can be plain HTTP inside the VPC or re-encrypted HTTPS when end to end encryption is required. When a question requires TLS all the way to the instance with the certificate private key held only on the instance, the ALB is not suitable, because it must terminate TLS to read HTTP; an NLB with a TCP listener passing traffic through is the answer.

Client addresses at the targets

Because the ALB terminates the connection and opens a new one to the target, the target sees the ALB's private address as the source. The original client IP address is passed in the X-Forwarded-For header, the protocol in X-Forwarded-Proto and the port in X-Forwarded-Port. Applications that log or restrict by client address must read these headers. This is a common detail in questions where an application's logs show only internal addresses after a load balancer was added.

Connection draining and slow start

When a target is deregistered, for example during scale in or a deployment, the deregistration delay, formerly called connection draining, lets in-flight requests finish before the target is removed, 300 seconds by default. Slow start mode gradually increases the share of traffic a newly registered target receives, which helps applications that need to warm caches or compile code before handling full load. Shortening the deregistration delay speeds up deployments for applications with short requests.

ALB limits worth knowing

An ALB has no static IP addresses: its addresses change as it scales, so clients must use its DNS name. When a client or firewall requires fixed IP addresses for allow lists, an ALB alone does not satisfy the requirement. Options are an NLB in front, with static addresses per zone, possibly with the ALB as its target, or AWS Global Accelerator in front of the ALB, which provides two static anycast addresses. This requirement appears frequently and points away from a bare ALB.

Listener rule details that questions test

Rules are evaluated in priority order, lowest number first, and the first matching rule wins, so a broad rule placed above a specific one hides it. Every listener has a default rule that applies when nothing else matches. A rule can combine several conditions, such as a host header and a path, and each condition can hold several values. Redirect actions are the standard way to send all HTTP traffic to HTTPS: a listener on port 80 with a single redirect rule to port 443. Fixed response actions return a simple maintenance page or a 404 without involving any target.

Target group settings

Each target group has its own protocol, port, health check and attributes, such as the deregistration delay, slow start duration, stickiness and the load balancing algorithm. ALB target groups can use round robin or least outstanding requests; the second sends new requests to the target with the fewest requests in progress, which helps when request durations vary widely. Automatic target weights can steer traffic away from targets that return more errors than their peers. Because these settings live on the target group, two services behind the same ALB can behave quite differently.

Internet facing and internal load balancers

An internet facing load balancer has public addresses and must sit in public subnets, while its targets can and usually should be in private subnets with no public addresses. An internal load balancer has only private addresses and serves clients inside the VPC or connected networks, such as a web tier calling an application tier. A common three tier design uses an internet facing ALB in front of the web tier and an internal ALB between the web and application tiers, so each tier can scale independently and only talks to the tier next to it.

Access logs and monitoring

ALB access logs, delivered to S3, record every request with the client address, latency, the target chosen and response codes, which helps investigate errors and slow requests. CloudWatch metrics such as HTTPCode_Target_5XX_Count, TargetResponseTime, HealthyHostCount and RejectedConnectionCount show how the load balancer and its targets are doing. A rise in 5XX errors generated by the load balancer itself, as opposed to by targets, often means no healthy targets are available.

ALBs have no static IP addresses

An ALB's addresses change as it scales. When partners need fixed IP addresses to allow, put an NLB with Elastic IPs or Global Accelerator in front, rather than trying to pin the ALB's addresses.