Sub topic 5
Network Load Balancer for TCP, UDP and TLS Traffic
This chapter assumes you know basic EC2 concepts (instances, security groups), VPC subnets and Availability Zones, and the difference between TCP and UDP at a high level. It also assumes you have read the chapter on Elastic Load Balancing types and the one on Application Load Balancers.
What a Network Load Balancer does
A Network Load Balancer (NLB) works at layer 4 of the OSI model, the transport layer. It looks at the IP protocol, source and destination addresses and ports. It does not read HTTP paths, headers or cookies. For each new flow it picks a target and forwards the packets.
Choose an NLB when you need one of these:
- A protocol that is not HTTP, such as a database wire protocol, MQTT, game traffic or DNS (UDP).
- Very high throughput or sudden traffic spikes with low latency.
- A static IP address per Availability Zone, or your own Elastic IP addresses.
- The client's source IP visible to the target without extra headers.
- A service exposed privately to other VPCs through AWS PrivateLink.
If you need path-based routing, host-based routing or redirects, use an Application Load Balancer instead.
Listeners and protocols
An NLB listener accepts connections on a protocol and port. The supported protocols are:
| Listener protocol | Typical use | Notes |
|---|---|---|
| TCP | Databases, custom services | Forwards the TCP stream unchanged |
| UDP | DNS, syslog, game servers | Flow is tracked by a 5-tuple |
| TCP_UDP | One port served over both protocols | Target group must use TCP_UDP too |
| TLS | Encrypted client connections | NLB terminates TLS using an ACM certificate |
An NLB listener has a default action that forwards to a target group. NLB has no content-based rules like an ALB, so each listener sends traffic to a target group (or a weighted set of target groups). To route different ports to different services, create one listener per port.
How flows are routed
For TCP, the NLB selects a target with a flow hash algorithm based on the protocol, source IP and port, destination IP and port, and the TCP sequence number. A given TCP connection stays on one target for its lifetime. For UDP, the same flow hash keeps packets of one flow on one target.
Every NLB node sits in one subnet, so you pick the subnets (one per Availability Zone) when you create it. Each node gets a static IP address in its subnet, or an Elastic IP if you assign one.
Creating an NLB
The commands below create an internet-facing NLB, a TCP target group with a TCP health check, and a listener on port 443 that forwards to the target group. Replace the subnet, VPC and ARN values with your own.
NLB_ARN=$(aws elbv2 create-load-balancer \
--name app-nlb \
--type network \
--scheme internet-facing \
--subnets subnet-aaaa1111 subnet-bbbb2222 \
--query 'LoadBalancers[0].LoadBalancerArn' --output text)
TG_ARN=$(aws elbv2 create-target-group \
--name app-nlb-tg \
--protocol TCP \
--port 443 \
--vpc-id vpc-0123456789abcdef0 \
--target-type instance \
--health-check-protocol TCP \
--query 'TargetGroups[0].TargetGroupArn' --output text)
aws elbv2 create-listener \
--load-balancer-arn "$NLB_ARN" \
--protocol TCP \
--port 443 \
--default-actions Type=forward,TargetGroupArn="$TG_ARN"Compared with the ALB example, there are no HTTP rules and no --security-groups argument. NLBs historically had no security groups. You can now attach security groups to an NLB, but only if you do so at creation time. If your NLB has none, the targets' security groups must allow traffic from the client addresses (when client IP preservation is on) or from the NLB's addresses (when it is off).
Client IP preservation and target types
With instance targets, the NLB preserves the client source IP by default. The target sees the real client address, and the target's security group must allow that client range. With IP targets, client IP preservation is off by default, so the target sees the NLB node's private address.
If you need the client address when preservation is off, enable Proxy Protocol v2 on the target group. The target application must then parse the Proxy Protocol header. If it does not, connections will fail or show garbage at the start of each stream.
A known edge case applies to client IP preservation. If a client connects to an internal NLB from a target registered behind that same NLB, the connection can be routed back to the same target, and the source and destination are the same. The connection then fails. Turn off client IP preservation or use IP targets to avoid this loopback problem.
Health checks on an NLB
NLB health checks can use TCP, HTTP or HTTPS. A TCP check passes when the target completes the TCP handshake on the health check port. This only proves the port is open, not that the application works. If your service speaks HTTP, an HTTP check against /health is more useful. Defaults differ from ALB: the interval is 30 seconds, and a target is healthy after 5 consecutive successes by default. You can lower the interval to 10 seconds.
If every target in all zones is unhealthy, the NLB fails open and sends traffic to all targets. This avoids a total outage caused by a faulty health check, but it can also hide a real failure, so alarm on the UnHealthyHostCount CloudWatch metric.
Terminating TLS on an NLB
A TLS listener lets the NLB decrypt traffic using a certificate from AWS Certificate Manager (ACM), and then forward it to targets as plain TCP or as re-encrypted TLS. Terminating TLS at the NLB keeps certificate handling out of your instances.
aws elbv2 create-listener \
--load-balancer-arn "$NLB_ARN" \
--protocol TLS \
--port 443 \
--certificates CertificateArn=arn:aws:acm:us-east-1:111122223333:certificate/example-id \
--ssl-policy ELBSecurityPolicy-TLS13-1-2-2021-06 \
--alpn-policy HTTP2Preferred \
--default-actions Type=forward,TargetGroupArn="$TG_ARN"Use a security policy that matches your clients. The --alpn-policy option lets the NLB negotiate HTTP/2 with clients that support it. The certificate must be in the same Region as the NLB. If you want the NLB to pass encrypted traffic through untouched, use a TCP listener instead and let the targets terminate TLS.
Cross-zone load balancing and zonal behavior
Cross-zone load balancing is off by default on an NLB. Each node sends traffic only to targets in its own zone. If one zone has 2 targets and another has 8, the zone with 2 targets receives the same share of traffic as the other, so each of those 2 targets is busier. Enable cross-zone load balancing on the load balancer or target group when zone sizes are uneven. Be aware that cross-zone traffic can add inter-AZ data transfer charges on an NLB.
aws elbv2 modify-load-balancer-attributes \
--load-balancer-arn "$NLB_ARN" \
--attributes Key=load_balancing.cross_zone.enabled,Value=trueConnection behavior to know about
- Idle timeout. NLB closes idle TCP connections after 350 seconds by default (configurable for TCP listeners). TLS listeners also have a configurable idle timeout. UDP flows time out after 120 seconds of inactivity. Send keepalives if your clients hold quiet connections open.
- Deregistration delay. When a target is deregistered, existing flows continue until the delay (default 300 seconds) ends. Set connection termination on deregistration if you want connections closed sooner.
- Failed targets. For TCP, a target that becomes unhealthy no longer receives new flows, but existing flows may continue until they are reset or time out.
Common errors
- Targets stay unhealthy. Check that the target's security group allows the health check port from the NLB, and that the application listens on that port.
- Clients time out with instance targets. The security group must allow the real client source range, because the client IP is preserved.
- Intermittent failures from one zone. With cross-zone off, one zone with no healthy targets drops its share of traffic. Keep targets in every enabled zone.
Exercise
An NLB with cross-zone load balancing disabled has 2 targets in zone a and 8 targets in zone b. Roughly how is traffic divided, and what setting changes this?
Show answer
Each zone's node receives about half the traffic, so each of the 2 targets in zone a gets about four times the load of each target in zone b. Enabling cross-zone load balancing lets every node send to all 10 targets and evens out the load, at the cost of possible cross-AZ data transfer charges.
Exercise
Your application behind an NLB needs the original client IP, but targets are registered by IP address and client IP preservation is off. What can you enable, and what must the application do?
Show answer
Enable Proxy Protocol v2 on the target group. The application must parse the Proxy Protocol header at the start of each connection, otherwise connections will break.