What is a Load Balancer?

A load balancer is a service or device that distributes incoming traffic across multiple servers (instances) to ensure no single server is overwhelmed. It improves the availability and scalability of applications, ensuring that traffic is balanced efficiently and performance remains high, even under heavy load. Load balancers are essential for handling high-traffic websites or applications by distributing requests evenly among servers, which increases uptime and reliability.

Why Do We Need a Load Balancer?

Here are a few reasons load balancers are necessary:

  1. Scalability: Distribute traffic across multiple instances to ensure high availability and efficiency.
  2. Redundancy: If one server goes down, the load balancer reroutes traffic to healthy servers.
  3. Improved Performance: Load balancing ensures that no single server becomes a bottleneck, enhancing performance.
  4. Fault Tolerance: By balancing traffic across instances, it ensures that your application stays functional even if one instance fails.
  5. SSL Termination: Offload the task of handling secure connections from the instances to the load balancer, freeing up resources.

Types of Load Balancers

There are several types of load balancers available, with each having its unique use case:

  1. Application Load Balancer (ALB): Primarily used for HTTP/HTTPS traffic. It operates at the application layer (Layer 7), routing traffic based on URL paths or host headers.
  2. Network Load Balancer (NLB): Operates at the transport layer (Layer 4), best suited for handling TCP or UDP traffic. It's fast, supports millions of requests per second, and is ideal for applications requiring low latency.
  3. Classic Load Balancer (CLB): The traditional option, supports both HTTP/HTTPS and TCP protocols. However, it is being replaced by ALB and NLB in modern setups.
  4. Elastic Load Balancer (ELB): This is Amazon Web Services' offering that automatically distributes incoming application traffic across multiple targets, such as EC2 instances, containers, and IP addresses.

What is Elastic Load Balancer (ELB)?

An Elastic Load Balancer (ELB) is a fully managed load balancing service provided by AWS. It automatically adjusts to incoming traffic demands and scales the number of load balancers based on needs. With ELB, you can seamlessly handle varying traffic loads and ensure that applications are highly available and fault-tolerant.

Setting Up a Load Balancer in AWS

Now, let’s walk through a practical setup of a Load Balancer in AWS using EC2 instances and ELB.

Prerequisites:

  1. Three EC2 instances running in different availability zones.
  2. A target group that registers instances.
  3. A health check path to monitor instances.

Steps:

1. Launch EC2 Instances

  • Launch three EC2 instances in three different availability zones for high availability.
  • Assign security groups and ensure your instances have proper SSH or HTTP access.

2. Create a Load Balancer

  1. Navigate to the AWS Management Console:
  • Go to EC2 Dashboard > Load Balancers > Create Load Balancer.
  1. Choose Application Load Balancer.
  2. Configure Load Balancer:
  • Name it, select the scheme (internet-facing or internal), and choose the listeners (usually HTTP or HTTPS).
  1. Choose Availability Zones:
  • Select at least two Availability Zones to ensure fault tolerance.
  • Attach the three EC2 instances in each zone to the load balancer.

3. Health Check Configuration

The Health Check is vital to determine the health of your instances. If an instance fails the health check, the load balancer will stop sending traffic to it.

  • Protocol: HTTP or HTTPS.
  • Path: The endpoint used by the application to verify health (e.g., /health).
  • Port: The port your application is using (usually port 80 for HTTP or 443 for HTTPS).
  • Healthy Threshold: The number of consecutive health checks required before considering an instance healthy.
  • Unhealthy Threshold: The number of consecutive health checks required before considering an instance unhealthy.
  • Timeout: The time the load balancer waits for a response from an instance before considering it failed.
  • Interval: The frequency of health checks.

Once the configuration is complete, click Create.

4. Register EC2 Instances with Target Group

  1. Create a Target Group and register your EC2 instances to it.
  2. Ensure that health checks are working by testing the health status of the instances. If the health check passes, the instance will be marked as In Service. Otherwise, it will be marked as Out of Service.

5. Test Load Balancer DNS

  • After creating the load balancer, you will get a DNS name.
  • Use this DNS in a browser or curl to verify the load balancing process. Refresh the browser repeatedly to observe that the traffic is being routed to different instances.

This will direct you to different instances based on the load balancing algorithm (e.g., round-robin).

6. Simulate Instance Failure

To simulate failure:

  • Stop an instance: Navigate to the EC2 console and stop one of the instances. The load balancer will reroute the traffic to the healthy instances.

7. Disable Direct Access to Instances via IP

To stop accessing the instances directly and only allow access through the load balancer:

  1. Modify Security Groups:
  • Initially, disable SSH/HTTP access from public IPs by removing the relevant inbound rules.
  • Add a rule that allows only the load balancer's security group to access the instance.

8. Configure Security Group for Load Balancer

Create a security group for the load balancer and add rules that allow access from anywhere or only trusted sources (such as specific IPs or other security groups).

Why Not More Elastic Load Balancers?

AWS is continuously improving its offerings, and while Elastic Load Balancers are still widely used, alternatives like Amazon API Gateway, ALB, and NLB provide more specific capabilities. For instance:

  • API Gateway is ideal for handling API traffic with features like rate limiting and request validation.
  • ALB and NLB offer specific benefits over ELB in terms of functionality, flexibility, and handling more complex traffic patterns.

However, ELB is still useful for simple use cases and is automatically included in AWS services when you need a scalable, fully managed load balancing solution.

Conclusion

Load balancers are essential for building scalable, high-availability applications. By distributing traffic across multiple instances, you ensure that your application can handle varying loads without downtime. AWS's Elastic Load Balancer (ELB) makes it easy to set up and manage load balancing for your instances. Through a series of health checks and configuration settings, ELB ensures that your application remains highly available even in case of failure.

By following the practical steps mentioned above, you can configure and test your own load balancer to route traffic effectively and ensure that your instances are in service.