0
0
AWScloud~5 mins

ALB vs NLB decision in AWS - CLI Comparison

Choose your learning style9 modes available
Introduction
When you want to send internet traffic to your applications, you need a way to decide how to send that traffic. AWS offers two main types of load balancers: Application Load Balancer (ALB) and Network Load Balancer (NLB). Choosing the right one helps your app work well and handle users smoothly.
When your app needs to understand the content of web requests and route users based on URLs or headers, use ALB.
When you need to handle millions of requests quickly and want very fast network-level routing, use NLB.
When your app uses HTTP or HTTPS protocols and needs features like user authentication or sticky sessions, ALB is best.
When your app requires TCP or UDP traffic handling with very low latency, NLB is the right choice.
When you want to balance traffic across multiple servers in different availability zones with health checks, both ALB and NLB can be used depending on protocol needs.
Commands
This command creates an Application Load Balancer named 'my-application-lb' in two subnets with a security group. We use ALB for HTTP/HTTPS traffic with advanced routing.
Terminal
aws elbv2 create-load-balancer --name my-application-lb --subnets subnet-12345678 subnet-87654321 --security-groups sg-12345678 --type application
Expected OutputExpected
{ "LoadBalancers": [ { "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/my-application-lb/50dc6c495c0c9188", "DNSName": "my-application-lb-1234567890.us-east-1.elb.amazonaws.com", "CanonicalHostedZoneId": "Z35SXDOTRQ7X7K", "CreatedTime": "2024-06-01T12:00:00.000Z", "LoadBalancerName": "my-application-lb", "Scheme": "internet-facing", "VpcId": "vpc-1a2b3c4d", "State": { "Code": "provisioning" }, "Type": "application", "IpAddressType": "ipv4" } ] }
--type - Specifies the load balancer type: application for ALB
--subnets - Defines the subnets where the load balancer will be placed
--security-groups - Assigns security groups to control traffic
This command creates a Network Load Balancer named 'my-network-lb' in two subnets. NLB is used for fast, low-level network traffic routing.
Terminal
aws elbv2 create-load-balancer --name my-network-lb --subnets subnet-12345678 subnet-87654321 --type network
Expected OutputExpected
{ "LoadBalancers": [ { "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/my-network-lb/70dc6c495c0c9188", "DNSName": "my-network-lb-1234567890.us-east-1.elb.amazonaws.com", "CanonicalHostedZoneId": "Z35SXDOTRQ7X7K", "CreatedTime": "2024-06-01T12:05:00.000Z", "LoadBalancerName": "my-network-lb", "Scheme": "internet-facing", "VpcId": "vpc-1a2b3c4d", "State": { "Code": "provisioning" }, "Type": "network", "IpAddressType": "ipv4" } ] }
--type - Specifies the load balancer type: network for NLB
--subnets - Defines the subnets where the load balancer will be placed
This command checks the status and details of the Application Load Balancer to confirm it is created and ready.
Terminal
aws elbv2 describe-load-balancers --names my-application-lb
Expected OutputExpected
{ "LoadBalancers": [ { "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/app/my-application-lb/50dc6c495c0c9188", "DNSName": "my-application-lb-1234567890.us-east-1.elb.amazonaws.com", "State": { "Code": "active" }, "Type": "application" } ] }
This command checks the status and details of the Network Load Balancer to confirm it is created and ready.
Terminal
aws elbv2 describe-load-balancers --names my-network-lb
Expected OutputExpected
{ "LoadBalancers": [ { "LoadBalancerArn": "arn:aws:elasticloadbalancing:us-east-1:123456789012:loadbalancer/net/my-network-lb/70dc6c495c0c9188", "DNSName": "my-network-lb-1234567890.us-east-1.elb.amazonaws.com", "State": { "Code": "active" }, "Type": "network" } ] }
Key Concept

If you remember nothing else from this pattern, remember: ALB is for smart web traffic routing, NLB is for fast network traffic handling.

Common Mistakes
Choosing ALB when the app needs TCP or UDP traffic with very low latency.
ALB only supports HTTP/HTTPS protocols and cannot handle raw TCP/UDP traffic efficiently.
Use NLB for TCP/UDP traffic and low latency needs.
Not specifying the correct load balancer type flag when creating the load balancer.
AWS defaults to ALB if type is not specified, which may not match your traffic needs.
Always use --type application for ALB or --type network for NLB explicitly.
Assigning security groups to NLB which does not support them.
NLB operates at the network layer and does not use security groups, so this causes errors.
Do not assign security groups when creating NLB.
Summary
Use 'aws elbv2 create-load-balancer' with --type application to create an ALB for HTTP/HTTPS traffic.
Use 'aws elbv2 create-load-balancer' with --type network to create an NLB for TCP/UDP traffic with low latency.
Verify load balancer creation and status with 'aws elbv2 describe-load-balancers' command.