Catalog My learning Blog
AWS Tutorial for Developers: From Account to Production · Phase 2 · Lesson 11 · 3 min read

AWS ALB vs NLB: Elastic Load Balancing Explained

On this page
  1. ALB vs NLB vs Gateway Load Balancer
  2. Listeners, rules and target groups
  3. Path-based routing on an ALB
  4. Load balancer health checks done right
  5. Client IPs, cross-zone balancing and WAF
  6. Check yourself

ALB vs NLB vs Gateway Load Balancer

Clients need one stable name to call, while the servers behind it come and go. Elastic Load Balancing provides that, spread across Availability Zones and scaled by AWS. The first decision is which kind.

Application Load Balancer (ALB)Network Load Balancer (NLB)Gateway Load Balancer (GWLB)
Layer7 (HTTP, HTTPS, gRPC, WebSockets)4 (TCP, UDP, TLS)3 (IP packets)
Routes byHost, path, headers, query, methodPort and protocolSends traffic through appliances
Static IPsNo (use its DNS name)Yes, one per AZ, or your Elastic IPsn/a
ExtrasAuth (OIDC, Cognito), WAF, redirects, fixed responsesVery high throughput, preserves client IP, PrivateLinkFirewalls and inspection fleets
Use forWeb apps and APIsNon-HTTP protocols, static IPs, extreme scaleThird-party network appliances
An ALB routes HTTP requests by path, host and headers; an NLB forwards TCP and UDP connections at very high scale.
An ALB routes HTTP requests by path, host and headers; an NLB forwards TCP and UDP connections at very high scale.

Most web applications want an ALB. Reach for an NLB when you need static IPs for client allowlists, non-HTTP protocols, or to expose a service privately through PrivateLink.

Listeners, rules and target groups

  • Listener: a port and protocol the load balancer accepts (HTTPS:443), with a certificate from ACM (AWS Certificate Manager, free for use on load balancers).
  • Rules: on an ALB, conditions (path, host, header) mapped to actions (forward, redirect, fixed response), checked by priority, lowest number first. A default action catches everything else.
  • Target group: a set of targets (instances, IP addresses, Lambda functions) with a port, protocol and health check.
  • An Auto Scaling group or ECS service registers its instances or tasks in a target group automatically.

Path-based routing on an ALB

Send /assets/* to one target group and everything else to the API (run against a local AWS emulator):

Code
API=$(aws elbv2 create-target-group --name api-tg --protocol HTTP --port 8080 \
  --vpc-id $VPC --health-check-path /health --query 'TargetGroups[0].TargetGroupArn' --output text)
ASSETS=$(aws elbv2 create-target-group --name assets-tg --protocol HTTP --port 8081 \
  --vpc-id $VPC --health-check-path / --query 'TargetGroups[0].TargetGroupArn' --output text)

LIS=$(aws elbv2 create-listener --load-balancer-arn $ALB --protocol HTTP --port 80 \
  --default-actions Type=forward,TargetGroupArn=$API --query 'Listeners[0].ListenerArn' --output text)

aws elbv2 create-rule --listener-arn $LIS --priority 10 \
  --conditions Field=path-pattern,Values='/assets/*' \
  --actions Type=forward,TargetGroupArn=$ASSETS
aws elbv2 describe-rules --listener-arn $LIS \
  --query 'Rules[].[Priority,Conditions[0].Values[0]||`(default)`]' --output text
Code
10	/assets/*
default	(default)

In production, the port 80 listener would only redirect to HTTPS, and the rules would live on a 443 listener with an ACM certificate.

Load balancer health checks done right

  • The target group calls the health check path on each target every interval. A target is marked unhealthy after a few failures, and traffic stops going to it.
  • Make /health cheap and focused on "can this instance serve requests". Don't check every downstream dependency: if the database is slow, every target fails at once and the load balancer has nobody to send traffic to. (When all targets are unhealthy, an ALB "fails open" and routes to all of them anyway.)
  • Deregistration delay (connection draining, default 300 seconds) lets in-flight requests finish when a target is removed. Lower it for fast deploys of short requests.

Client IPs, cross-zone balancing and WAF

  • The app sees the load balancer's IP as the source. Read the client's address from X-Forwarded-For on an ALB; an NLB can preserve the client IP.
  • Cross-zone load balancing spreads traffic across targets in all AZs; it's on by default for ALBs.
  • Attach AWS WAF to an ALB for rate limiting and common attack rules.

Check yourself

A Network Load Balancer, which offers one static IP per Availability Zone, or your own Elastic IPs. ALB IP addresses change as it scales. You can also put an NLB in front of an ALB, or use Global Accelerator for static anycast IPs.

A slow or failing database would mark every target unhealthy at once, and the load balancer would remove all capacity, turning a degraded service into a total outage. Health checks should test whether this instance can serve, not shared dependencies.

It evaluates rules in priority order, lowest number first, and uses the first whose conditions match; the default action handles the rest.

In the X-Forwarded-For header the ALB adds, since connections arrive from the ALB's own addresses. Only trust that header from the load balancer.

The time the target group lets in-flight requests finish on a target being removed before cutting it off. Set it slightly longer than your longest normal request.