ALB vs NLB: Choosing the Right AWS Load Balancer for Your Web App
Picking the wrong load balancer is a silent architectural mistake — it works until it doesn't, costing you latency, money, or missing features at scale. Understanding where ALB and NLB operate in the OSI model is the fastest way to make the right call.
TL;DR — Quick Comparison
| Attribute | Application Load Balancer (ALB) | Network Load Balancer (NLB) |
|---|---|---|
| OSI Layer | Layer 7 (Application) | Layer 4 (Transport) |
| Protocol | HTTP, HTTPS, gRPC, WebSocket | TCP, UDP, TLS |
| Routing Logic | Path, host, header, query string, method | IP + Port only |
| Target Types | EC2 (instance), IP, Lambda | EC2 (instance), IP |
| Static IP | No (DNS-based, IPs change) | Yes (per-AZ Elastic IP) |
| TLS Termination | Yes (ACM integration) | Yes (pass-through or terminate) |
| WebSockets | Native support | TCP pass-through |
| Latency Profile | Slightly higher (L7 inspection) | Ultra-low (no payload inspection) |
| Sticky Sessions | Cookie-based (duration or app-defined) | Source IP-based |
| Use Case Fit | Web apps, microservices, APIs | Gaming, IoT, financial systems, static IP requirements |
The OSI Layer Difference — Why It Matters
The single most important distinction is where in the network stack each load balancer makes its routing decision.
- ALB (Layer 7) reads the full HTTP request — URL path, hostname, headers, cookies — before deciding where to send traffic. It understands what is being requested.
- NLB (Layer 4) routes based purely on IP address and TCP/UDP port. It never inspects the payload. It only knows where a packet is going, not what it contains.
Analogy: Think of ALB as a hotel concierge who reads your reservation details and directs you to the right room type (suite, standard, accessible). NLB is the building's elevator — it moves you to the correct floor based on the button you pressed, with zero knowledge of why you're going there.
Architecture: How Each Load Balancer Routes Traffic
- Client Request: A browser or API client sends an HTTP/S request.
- ALB Listener: Inspects the full HTTP request at Layer 7.
- Content-Based Routing: ALB evaluates rules — path
/api/*routes to the API target group; path/routes to the web frontend target group. - Target Groups: Each target group contains EC2 instances, ECS tasks, or Lambda functions registered by instance ID or IP.
- NLB Listener: Receives raw TCP/UDP traffic and routes based on port number only — no payload inspection.
- NLB Targets: Forwards packets directly to registered EC2 instances or IP addresses with minimal processing overhead.
Deep Dive: ALB — Layer 7 Intelligence
Content-Based Routing Rules
ALB listener rules are evaluated in priority order. Each rule can match on:
- Path pattern:
/api/*,/images/* - Host header:
api.example.comvswww.example.com - HTTP method: GET, POST, etc.
- Query string:
?version=2 - HTTP headers: Custom headers or
User-Agent - Source IP (CIDR): Restrict access by IP range
ALB Listener Rule — Terraform Example
🔽 Click to expand — ALB Listener Rule (Terraform)
resource "aws_lb_listener_rule" "api_routing" {
listener_arn = aws_lb_listener.https.arn
priority = 100
condition {
path_pattern {
values = ["/api/*"]
}
}
action {
type = "forward"
target_group_arn = aws_lb_target_group.api_tg.arn
}
}
resource "aws_lb_listener_rule" "web_routing" {
listener_arn = aws_lb_listener.https.arn
priority = 200
condition {
path_pattern {
values = ["/*"]
}
}
action {
type = "forward"
target_group_arn = aws_lb_target_group.web_tg.arn
}
}
ALB Target Types
- instance: Routes to EC2 instance by instance ID (uses primary private IP).
- ip: Routes to any IP address — useful for ECS Fargate, on-premises targets via VPN/Direct Connect, or containers with multiple IPs.
- lambda: Invokes a Lambda function directly — ALB serializes the HTTP request into a JSON event.
Deep Dive: NLB — Layer 4 Performance
When NLB Is the Right Tool
- Static IP requirement: NLB assigns one Elastic IP per Availability Zone. Clients or firewall rules that require whitelisting a fixed IP need NLB.
- Ultra-low latency: NLB operates without payload inspection, making it ideal for latency-sensitive workloads (gaming, HFT, real-time telemetry).
- Non-HTTP protocols: UDP (DNS, syslog, SNMP), raw TCP (custom protocols, MQTT for IoT).
- Preserving source IP: NLB preserves the client's source IP address natively (no X-Forwarded-For header needed).
- PrivateLink: NLB is required as the backing load balancer for AWS PrivateLink service endpoints.
NLB Target Types
- instance: Routes to EC2 instance by instance ID.
- ip: Routes to any IP address within a VPC CIDR, a peered VPC, or on-premises via Direct Connect/VPN.
Sequence Diagram: Request Flow Comparison
- ALB flow: The load balancer terminates the TCP connection, reads the full HTTP request, evaluates routing rules, then opens a new connection to the selected backend. The backend sees the ALB's IP, not the client's (use
X-Forwarded-Forheader to retrieve the original client IP). - NLB flow: NLB does not terminate the connection. It performs connection-level load balancing and forwards packets directly. The backend sees the original client IP address natively.
Handling the "Fixed IP + HTTP Routing" Requirement
A common architectural challenge is needing both static IPs (for firewall whitelisting) and Layer 7 content-based routing. NLB alone provides static IPs but no HTTP routing. ALB provides HTTP routing but has dynamic IPs. Two supported patterns address this:
Pattern 1: AWS Global Accelerator + ALB
AWS Global Accelerator provides two static anycast IP addresses that front your ALB. Traffic enters the AWS global network at the nearest edge location and is routed to your ALB. The ALB retains full Layer 7 routing capability.
- Global Accelerator provides static IPs for firewall whitelisting.
- ALB handles all content-based routing rules behind Global Accelerator.
- This is the AWS-recommended pattern when static IPs + HTTP routing are both required.
Pattern 2: NLB with IP Targets Pointing to Backend Instances
If your requirement is purely static IP + TCP routing (no HTTP-level routing needed), register your backend EC2 or container IPs directly as NLB IP targets. This avoids any intermediary and keeps latency minimal.
Decision Flowchart: ALB or NLB?
IAM & Security Considerations
- ALB WAF Integration: ALB integrates natively with AWS WAF for Layer 7 protection (SQL injection, XSS, rate limiting). NLB does not support WAF.
- Security Groups: ALB supports security groups. NLB does not support security groups on the load balancer itself — control access via target security groups or NACLs.
- TLS: Both support TLS termination using ACM certificates. NLB additionally supports TLS pass-through (end-to-end encryption to the backend).
- Least Privilege: When using Lambda as an ALB target, grant only
lambda:InvokeFunctionto the ALB service principal (elasticloadbalancing.amazonaws.com).
🔽 Click to expand — Lambda Resource Policy for ALB Target
{
"Version": "2012-10-17",
"Statement": [
{
"Sid": "AllowALBInvoke",
"Effect": "Allow",
"Principal": {
"Service": "elasticloadbalancing.amazonaws.com"
},
"Action": "lambda:InvokeFunction",
"Resource": "arn:aws:lambda:us-east-1:123456789012:function:MyWebHandler",
"Condition": {
"StringEquals": {
"aws:SourceArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:targetgroup/my-lambda-tg/1234567890abcdef"
}
}
}
]
}
Scenario-Based Recommendation Summary
| Scenario | Recommended Choice | Reason |
|---|---|---|
| Standard web app / REST API | ALB | Path/host-based routing, WAF, ACM |
| Microservices on ECS/EKS | ALB | IP target type, gRPC support, per-service routing |
| Serverless backend (Lambda) | ALB | Native Lambda target type |
| Gaming / real-time UDP | NLB | UDP support, ultra-low latency |
| IoT / MQTT | NLB | TCP pass-through, source IP preservation |
| Static IP for firewall rules | NLB | Per-AZ Elastic IP assignment |
| Static IP + HTTP routing | Global Accelerator + ALB | Static anycast IPs with full L7 routing |
| AWS PrivateLink endpoint | NLB | Required by PrivateLink architecture |
| On-premises hybrid targets | ALB or NLB (IP targets) | Both support IP targets via Direct Connect/VPN |
Glossary
| Term | Definition |
|---|---|
| OSI Layer 7 | Application layer — understands HTTP, HTTPS, gRPC. Enables content-aware routing decisions. |
| OSI Layer 4 | Transport layer — understands TCP/UDP ports and IP addresses only. No payload inspection. |
| Target Group | A logical grouping of backend targets (EC2, IP, Lambda) that a load balancer routes traffic to. |
| Listener Rule | A condition-action pair on an ALB listener that determines how incoming requests are routed. |
| AWS Global Accelerator | A networking service that provides static anycast IPs and routes traffic over the AWS global network to regional endpoints like ALBs. |
Next Steps
- 📖 ALB Official Documentation
- 📖 NLB Official Documentation
- 📖 AWS Global Accelerator Documentation
- 🔧 Use the ELB Feature Comparison page to validate the latest supported features before finalizing your architecture.
Comments
Post a Comment