Different types of load balancers in EC2

In Amazon EC2, AWS offers three main types of load balancers that are part of the Elastic Load Balancing (ELB) service:

1. Application Load Balancer (ALB)

  • Layer 7 Load Balancer: Operates at the application layer (Layer 7) of the OSI model, which means it can make routing decisions based on the content of the request (e.g., URL, HTTP headers, query parameters).
  • Use Cases: Ideal for HTTP and HTTPS traffic, where advanced request routing is required, such as directing traffic to different backend services based on the URL path.
  • Features: Supports host-based and path-based routing, WebSockets, and can route requests to microservices or containers.

2. Network Load Balancer (NLB):

  • Layer 4 Load Balancers: Operates at the transport layer (Layer 4) of the OSI model, focusing on routing traffic based on IP addresses and TCP/UDP ports.
  • Use Cases: Suitable for applications requiring ultra-low latency, high throughput, or handling volatile network traffic, such as gaming, IoT, or real-time communications.
  • Features: Capable of handling millions of requests per second, maintaining extremely low latencies, and supporting static IP addresses and Elastic IPs.

3. Classic Load Balancer (CLB):

  • Legacy Load Balancers: Operates at both Layer 4 and Layer 7. It was the first load balancer offered by AWS, providing basic load balancing for HTTP, HTTPS, TCP, and SSL traffic.
  • Use Cases: Suitable for applications built on the EC2-Classic network, or those that require simple, straightforward load balancing.
  • Features: Although still supported, it is recommended to use ALB or NLB for new applications due to their advanced capabilities and improved performance.

load balancers

Java Example:

Here’s a simple Java example using AWS SDK to create an Application Load Balancers (ALB):

Example
```java
import software.amazon.awssdk.regions.Region;
import software.amazon.awssdk.services.elasticloadbalancingv2.ElasticLoadBalancingV2Client;
import software.amazon.awssdk.services.elasticloadbalancingv2.model.*;

public class CreateALBExample {

    public static void main(String[] args) {

        Region region = Region.US_WEST_2;
        ElasticLoadBalancingV2Client elbClient = ElasticLoadBalancingV2Client.builder()
                .region(region)
                .build();

        try {
            // Create an ALB
            CreateLoadBalancerRequest createLoadBalancerRequest = CreateLoadBalancerRequest.builder()
                    .name("my-application-lb")
                    .subnets("subnet-12345678", "subnet-87654321")
                    .securityGroups("sg-12345678")
                    .scheme(LoadBalancerSchemeEnum.INTERNET_FACING)
                    .type(LoadBalancerTypeEnum.APPLICATION)
                    .build();

            CreateLoadBalancerResponse createLoadBalancerResponse = elbClient.createLoadBalancer(createLoadBalancerRequest);
            String loadBalancerArn = createLoadBalancerResponse.loadBalancers().get(0).loadBalancerArn();

            System.out.println("Created Application Load Balancer with ARN: " + loadBalancerArn);

            // Optionally, set up listeners, target groups, etc.

        } catch (ElasticLoadBalancingException e) {
            System.err.println(e.awsErrorDetails().errorMessage());
        } finally {
            elbClient.close();
        }
    }
}
```

In this example, the AWS SDK for Java is used to create an Application Load Balancer (ALB). The ALB is set up with specified subnets and security groups and is configured as an internet-facing load balancer.

Homepage

Readmore