🌐 Elastic Load Balancing (ELB) in AWS – Distribute Traffic Across Multiple Instances

When you build an application that serves many users, one server alone is rarely enough. As your traffic grows, you deploy more EC2 instances. But then comes the real challenge:

👉 How do you distribute traffic evenly across multiple instances without overloading one and leaving others idle?

This is where Elastic Load Balancing (ELB) steps in.

AWS ELB automatically distributes incoming application traffic across multiple EC2 instances, containers, and even IP addresses in multiple Availability Zones.

It acts like a traffic cop at a busy intersection – ensuring requests are routed to healthy servers, improving both performance and fault tolerance.


🔑 Types of Elastic Load Balancers in AWS

AWS offers four types of Load Balancers:

  1. Application Load Balancer (ALB) – Best for HTTP/HTTPS (Layer 7).
  2. Network Load Balancer (NLB) – Best for TCP/UDP (Layer 4).
  3. Classic Load Balancer (CLB) – Legacy, supports Layer 4 & 7.
  4. Gateway Load Balancer (GWLB) – Used for third-party appliances like firewalls.

Each serves a unique purpose. Let’s dive into details, with programming examples.


⚡ 1. Application Load Balancer (ALB)

📌 What It Does

  • Operates at Layer 7 (Application Layer).
  • Routes traffic based on content of the request (e.g., URL, headers, query strings, host).
  • Supports path-based and host-based routing.
  • Ideal for microservices and container-based apps.

🖥️ Example Programs for ALB

Example 1: Create ALB using AWS CLI

Terminal window
aws elbv2 create-load-balancer \
--name my-app-lb \
--subnets subnet-123456 subnet-789012 \
--security-groups sg-12345678 \
--scheme internet-facing \
--type application

Example 2: Define Listener Rules for Path-Based Routing

Terminal window
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:region:123456789012:loadbalancer/app/my-app-lb/50dc6c495c0c9188 \
--protocol HTTP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-targets/73e2d6bc24d8a067

Example 3: Python (Boto3) Example

import boto3
client = boto3.client('elbv2')
response = client.create_load_balancer(
Name='myAppLB',
Subnets=['subnet-123456', 'subnet-789012'],
SecurityGroups=['sg-12345678'],
Scheme='internet-facing',
Type='application'
)
print(response)


⚡ 2. Network Load Balancer (NLB)

📌 What It Does

  • Operates at Layer 4 (Transport Layer).
  • Handles TCP, UDP, TLS traffic at very high performance.
  • Can handle millions of requests per second with ultra-low latency.
  • Best for gaming apps, real-time streaming, and high-performance APIs.

🖥️ Example Programs for NLB

Example 1: Create NLB (CLI)

Terminal window
aws elbv2 create-load-balancer \
--name my-nlb \
--type network \
--subnets subnet-abc123 subnet-def456

Example 2: Add Listener for TCP Port 80

Terminal window
aws elbv2 create-listener \
--load-balancer-arn arn:aws:elasticloadbalancing:region:123456789012:loadbalancer/net/my-nlb/73e2d6bc24d8a067 \
--protocol TCP \
--port 80 \
--default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:region:123456789012:targetgroup/my-nlb-targets/1234567890abcdef

Example 3: Terraform Example for NLB

resource "aws_lb" "my_nlb" {
name = "my-nlb"
internal = false
load_balancer_type = "network"
subnets = ["subnet-abc123", "subnet-def456"]
}


⚡ 3. Classic Load Balancer (CLB)

📌 What It Does

  • The oldest load balancer type in AWS.
  • Supports both Layer 4 and Layer 7, but limited features.
  • Not recommended for new apps, but still seen in legacy systems.

🖥️ Example Programs for CLB

Example 1: Create CLB (CLI)

Terminal window
aws elb create-load-balancer \
--load-balancer-name my-clb \
--listeners "Protocol=HTTP,LoadBalancerPort=80,InstanceProtocol=HTTP,InstancePort=80" \
--availability-zones us-east-1a

Example 2: Register EC2 Instances

Terminal window
aws elb register-instances-with-load-balancer \
--load-balancer-name my-clb \
--instances i-1234567890abcdef0 i-0598c7d356eba48d7

Example 3: Python Example

import boto3
client = boto3.client('elb')
response = client.create_load_balancer(
LoadBalancerName='myCLB',
Listeners=[{
'Protocol': 'HTTP',
'LoadBalancerPort': 80,
'InstanceProtocol': 'HTTP',
'InstancePort': 80
}],
AvailabilityZones=['us-east-1a']
)
print(response)


⚡ 4. Gateway Load Balancer (GWLB)

📌 What It Does

  • Introduced for third-party virtual appliances like firewalls, intrusion detection, and deep packet inspection.
  • Operates at Layer 3 (Network Layer).
  • Routes traffic using the GENEVE protocol.

🖥️ Example Programs for GWLB

Example 1: Create GWLB (CLI)

Terminal window
aws elbv2 create-load-balancer \
--name my-gwlb \
--type gateway \
--subnets subnet-xyz123

Example 2: Create Target Group for GWLB

Terminal window
aws elbv2 create-target-group \
--name gwlb-targets \
--protocol GENEVE \
--port 6081 \
--vpc-id vpc-abc123

Example 3: CloudFormation Example

Resources:
MyGWLB:
Type: AWS::ElasticLoadBalancingV2::LoadBalancer
Properties:
Name: my-gwlb
Type: gateway
Subnets:
- subnet-xyz123


🧠 How to Remember ELB Types (Interview & Exams)

👉 Trick: A N C G = “All Nice Cloud Gateways”

  • A = ALB → Application level, path-based routing.
  • N = NLB → Network level, ultra-fast TCP/UDP.
  • C = CLB → Classic, old but simple.
  • G = GWLB → Gateway for firewalls/appliances.

Quick Associations:

  • ALB → Web apps.
  • NLB → High performance.
  • CLB → Legacy.
  • GWLB → Security appliances.

🎯 Why It’s Important to Learn ELB

  1. Scalability – Distributes millions of requests efficiently.
  2. High Availability – Instances across multiple AZs.
  3. Security – Integrates with AWS WAF, SSL termination.
  4. Cost Efficiency – Prevents over-provisioning.
  5. Certification Exam Relevance – ELB is tested heavily in AWS Solutions Architect, Developer, and SysOps exams.

🌍 Real-World Use Cases

  • E-commerce: ALB for routing /checkout vs /products.
  • Gaming Apps: NLB for handling fast UDP traffic.
  • Enterprises: GWLB for firewall integration.
  • Legacy Apps: CLB for old infrastructure still in use.

📌 Conclusion

Elastic Load Balancing is the backbone of scalable and resilient architectures in AWS. Whether it’s web traffic (ALB), ultra-fast gaming traffic (NLB), legacy workloads (CLB), or security appliances (GWLB), AWS has a load balancer for every use case.

👉 Remember:

  • ALB = Smart routing (Layer 7)
  • NLB = High performance (Layer 4)
  • CLB = Legacy support
  • GWLB = Security appliances

Mastering ELB ensures your apps remain fast, fault-tolerant, and cost-effective.