ELB: ALB vs. NLB

Difficulty: Intermediate

Question

What is the difference between an Application Load Balancer (ALB) and a Network Load Balancer (NLB), and how do you choose between them?

Answer

An Application Load Balancer works at Layer 7 (HTTP/HTTPS): it reads the request and can route on path, host, headers, query strings, or methods, terminate TLS, and authenticate users. A Network Load Balancer works at Layer 4 (TCP, UDP, TLS): it forwards connections without looking at HTTP, handles very high throughput with very low latency, preserves the client IP, and gives you a static IP per Availability Zone. Use ALB for web apps and APIs, NLB for raw TCP/UDP, extreme performance, or when clients need fixed IPs.

How each one handles a request

ALB terminates the client connection, parses the HTTP request, evaluates listener rules in priority order, and opens its own connection to a target in the matching target group. Because it understands HTTP, it can do things like send /api/ to one service and /images/ to another, or route payments.example.com and orders.example.com to different backends from a single load balancer.

NLB picks a target using a flow hash (protocol, source and destination IP and port) and forwards packets for that connection to the same target. It does not inspect HTTP, so it cannot route by path. It scales to millions of requests per second and is designed for sudden spikes.

Feature comparison

FeatureALBNLB
OSI layer7 (HTTP, HTTPS, gRPC, WebSocket)4 (TCP, UDP, TLS)
RoutingPath, host, header, query, method, source IPPort and protocol only
Static IPNo (DNS name only; use Global Accelerator for static IPs)Yes, one per AZ, can use Elastic IPs
Client IP at targetVia X-Forwarded-For headerPreserved natively (for instance/IP targets)
Target typesInstance, IP, LambdaInstance, IP, ALB
ExtrasAWS WAF, OIDC/Cognito auth, sticky sessions, redirects, fixed responsesPrivateLink endpoint services, TLS passthrough
Cross-zone load balancingOn by defaultOff by default

Choosing in practice

  • Microservices behind one domain: ALB with path-based rules into separate target groups, often fronting ECS or EKS services.
  • A game server, MQTT broker, or custom TCP protocol: NLB, because ALB only speaks HTTP.
  • A partner bank or payment gateway that allowlists your IPs: NLB with Elastic IPs, or NLB in front of an ALB if you also need L7 routing.
  • Exposing a service privately to other VPCs or accounts via PrivateLink: NLB (PrivateLink endpoint services are built on NLB or Gateway Load Balancer).

The other load balancers

  • Gateway Load Balancer (GWLB): Layer 3, used to insert third-party firewalls or inspection appliances transparently.
  • Classic Load Balancer: previous generation, do not use for new designs.

Gotchas

  • Health checks belong to the target group, not the load balancer. A wrong health check path is the most common reason targets show "unhealthy".
  • NLBs historically had no Security Groups; they have supported them since 2023, but must be configured at creation time.
  • Since NLB preserves client IPs, the target's Security Group must allow the clients' IPs, not just the load balancer.
ALB path-based routing to microservices. The ALB reads the HTTP path and sends each request to a different target group.
  1. Client request: GET https://shop.example.com/api/orders
  2. HTTPS listener on :443: Terminates TLS with an ACM certificate.
  3. Listener rules: /api/* goes to api-tg, /static/* goes to web-tg.
  4. Target group api-tg: Health-checked ECS tasks registered by IP.
  5. Healthy target: Receives the request with X-Forwarded-For set.

Code examples

Adding a path-based rule to an ALB listener

aws elbv2 create-rule \
  --listener-arn arn:aws:elasticloadbalancing:ap-south-1:111122223333:listener/app/shop-alb/abc/def \
  --priority 10 \
  --conditions Field=path-pattern,Values='/api/*' \
  --actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:ap-south-1:111122223333:targetgroup/api-tg/123

# NLB: a plain TCP listener, no rules to evaluate
aws elbv2 create-listener \
  --load-balancer-arn arn:aws:elasticloadbalancing:ap-south-1:111122223333:loadbalancer/net/mqtt-nlb/xyz \
  --protocol TCP --port 8883 \
  --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:ap-south-1:111122223333:targetgroup/mqtt-tg/456

The ALB rule matches on HTTP path; the NLB listener just forwards TCP connections on a port, which is all a Layer 4 balancer can do.

Key points

  • ALB = Layer 7, content-based routing, WAF and auth integration, Lambda targets
  • NLB = Layer 4, TCP/UDP/TLS, static IP per AZ, client IP preserved, very high throughput
  • Need fixed IPs for allowlisting: NLB with Elastic IPs (or Global Accelerator)
  • PrivateLink endpoint services use NLB (or GWLB)
  • Health checks are configured on target groups

Common follow-up questions

  • How does the target see the real client IP behind an ALB?
  • How would you give an ALB a static IP address?
  • What is cross-zone load balancing and why is it off by default on NLB?
  • How would you do a blue/green deployment using ALB weighted target groups?

Interview tips

  • Start with the layer (7 vs 4) and then give a concrete use case for each; a comparison without use cases sounds memorised
  • Mention static IPs and PrivateLink as the classic reasons to pick NLB
  • If you mention Gateway Load Balancer and that Classic is legacy, it shows breadth without wasting time

Concepts covered

Application Load Balancer, Network Load Balancer, Layer 7 Routing, Layer 4, Target Groups

More AWS & Cloud Fundamentals questions