4.41 What is load balancing
In this lesson we discover what a load balancer is. A load balancer is a server that redirects all incoming external traffic toward multiple EC2 instances behind it. If three users send requests to the load balancer, it dispatches each request to a different instance, which responds back through the balancer to its user. This is exactly why it is called a load balancer — it spreads the incoming traffic evenly across the downstream instances.
Why use an Elastic Load Balancer
- Spreads the load across multiple downstream instances.
- Exposes a single DNS endpoint to the outside world.
- Gracefully handles instance failures with regular health checks.
- Provides SSL/HTTPS termination for your sites.
- Enforces session stickiness via cookies (covered later).
- Provides high availability across multiple availability zones.
- Separates public traffic from private traffic.
AWS offers four types of load balancers: the Classic Load Balancer (CLB) — version 1 from 2009, supports HTTP, HTTPS and TCP; the Application Load Balancer (ALB) — version 2 from 2016, supports HTTP, HTTPS and WebSocket; the Network Load Balancer (NLB) — version 2 from 2017, supports TCP, UDP and TLS at extremely low latency; and the Gateway Load Balancer. The v2 generations offer more features and are recommended. You can also deploy internal load balancers (private) or external ones (public).
Security is enforced through security groups. A common pattern: users access the load balancer on ports 80 and 443 from 0.0.0.0/0; the EC2 instances behind it only accept inbound traffic on port 80 from the load balancer's security group, never directly from the Internet. The balancer also performs health checks: it probes each instance on a configured port and path; any response other than HTTP 200 marks the instance unhealthy. Common errors include 4xx (client side), 5xx (application side) and 503 (load balancer overloaded or no targets registered). Access logs help debug requests.