Sub topic 4
Network Load Balancer for TCP, UDP and TLS traffic
This chapter assumes you know what a target group and a listener are, and that you can run AWS CLI commands. It covers when to pick a Network Load Balancer (NLB) over an Application Load Balancer (ALB), and how to set one up.
What an NLB does
An NLB works at layer 4 of the network stack. It routes connections by IP address, port and protocol. It does not read HTTP headers, paths or cookies, so it has no path-based or host-based rules. Because it does less work per connection, it handles very high connection rates with low latency.
NLB listeners support these protocols:
| Listener protocol | Use |
|---|---|
| TCP | Databases, custom protocols, raw TCP services |
| UDP | DNS, game servers, streaming and telemetry |
| TCP_UDP | One port that serves both protocols |
| TLS | Encrypted TCP, with the certificate held on the NLB |
When to choose an NLB
- Your protocol is not HTTP, or you need UDP.
- You need a static IP address per Availability Zone. An internet-facing NLB can use an Elastic IP in each subnet. Clients can then allow-list those addresses.
- You need the client's source IP to reach the application. With instance targets, the NLB preserves it.
- You need to handle sudden traffic spikes or millions of requests per second.
If you need routing by URL path or header, use an ALB instead.
Request flow
Creating an NLB with a TLS listener
This example creates an internet-facing NLB and a TCP target group. It then adds a TLS listener that uses an ACM certificate.
aws elbv2 create-load-balancer \
--name api-nlb \
--type network \
--scheme internet-facing \
--subnets subnet-aaaa1111 subnet-bbbb2222
aws elbv2 create-target-group \
--name api-tcp-tg \
--protocol TCP \
--port 8080 \
--vpc-id vpc-0123456789abcdef0 \
--target-type instance \
--health-check-protocol TCP
aws elbv2 create-listener \
--load-balancer-arn "$NLB_ARN" \
--protocol TLS \
--port 443 \
--certificates CertificateArn="$CERT_ARN" \
--ssl-policy ELBSecurityPolicy-TLS13-1-2-2021-06 \
--default-actions Type=forward,TargetGroupArn="$TG_ARN"The NLB decrypts TLS and forwards plain TCP to port 8080 on the targets. Set $NLB_ARN, $TG_ARN and $CERT_ARN from the output of the earlier commands and your ACM console.
Behaviors to watch
- Health checks. An NLB can check with TCP, HTTP or HTTPS. A TCP check only confirms that the port accepts a connection. It does not confirm the application works. Use an HTTP check when the service has a health endpoint.
- Cross-zone load balancing. It is off by default on an NLB. Each load balancer node sends traffic only to targets in its own zone. Uneven target counts per zone then cause uneven load. Enable it if your zones are not balanced.
- Target types. You can register instances, IP addresses, or an ALB. Registering an ALB lets you combine static IPs with layer 7 routing.
- Security groups. Target security groups must allow the client or NLB traffic. Whether this is the client IP or the NLB depends on target type and settings. Check this first when targets fail health checks.
Exercise
A game service receives UDP traffic and its partners must allow-list fixed IP addresses. Which load balancer fits, and why?
Show answer
A Network Load Balancer. It supports UDP listeners and provides one static IP address per Availability Zone, which can be an Elastic IP. An ALB handles only HTTP and HTTPS and gives no static addresses.