Sobes.tech
Middle

How does a load balancer work in cloud technologies?

sobes.tech AI

Answer from AI

A Load Balancer distributes incoming network traffic across a group of servers (instances), increasing the availability and performance of an application.

Main functions:

  • Traffic distribution: Redirects requests to available instances.
  • Health Checks: Periodically checks the health of instances and removes unhealthy ones from rotation.
  • Scaling: Facilitates adding and removing instances without downtime.

Traffic distribution algorithms:

  • Round Robin: Sequential distribution in a circular manner.
  • Least Connections: Sends requests to the least loaded instance.
  • IP Hash: Distributes requests based on a hash of the client's IP address, ensuring session persistence.
  • Weighted Round Robin: Distribution considering assigned weights to each instance.

Types of cloud load balancers:

  • Network Load Balancer (NLB): Operates at network level (Layer 4, TCP/UDP). Suitable for high-performance scenarios with large traffic volumes.
  • Application Load Balancer (ALB): Operates at application level (Layer 7, HTTP/HTTPS). Supports routing based on URL, host, headers, and cookies, with SSL termination.
  • Gateway Load Balancer (GWLB): Operates at network level (Layer 3) and is designed for deploying and scaling third-party network virtual appliances (firewalls, IDS/IPS) transparently.

Health Check: The load balancer sends requests (e.g., GET /health) or checks TCP ports on instances. If the response meets expectations (e.g., HTTP 200 OK or successful TCP connection), the instance is considered healthy.

Example configuration in AWS CLI:

# Create a target group
aws elbv2 create-target-group \
    --name MyTargetGroup \
    --protocol HTTP \
    --port 80 \
    --vpc-id vpc-xxxxxxxxxxxxxxxxx

# Register instances to the target group
aws elbv2 register-targets \
    --target-group-arn arn:aws:elasticloadbalancing:us-east-1:xxxxxxxxxx:targetgroup/MyTargetGroup/xxxxxxxxxxxxxxxxx \
    --targets Id=i-xxxxxxxxxxxxxxxxx Id=i-yyyyyyyyyyyyyyyyy

# Create an Application Load Balancer (ALB)
aws elbv2 create-load-balancer \
    --name MyAppAlb \
    --subnets subnet-xxxxxxxxxxxxxxxxx subnet-yyyyyyyyyyyyyyyyy \
    --scheme internet-facing \
    --type application

# Create a listener
aws elbv2 create-listener \
    --load-balancer-arn arn:aws:elasticloadbalancing:us-east-1:xxxxxxxxxx:loadbalancer/app/MyAppAlb/xxxxxxxxxxxxxxxxx \
    --port 80 \
    --protocol HTTP \
    --default-actions Type=forward,TargetGroupArn=arn:aws:elasticloadbalancing:us-east-1:xxxxxxxxxx:targetgroup/MyTargetGroup/xxxxxxxxxxxxxxxxx

# Health check configuration example
aws elbv2 modify-target-group \
    --target-group-arn arn:aws:elasticloadbalancing:us-east-1:xxxxxxxxxx:targetgroup/MyTargetGroup/xxxxxxxxxxxxxxxxx \
    --health-check-protocol HTTP \
    --health-check-port 80 \
    --health-check-path /health \
    --health-check-interval-seconds 30 \
    --health-check-protocol-timeout-seconds 5 \
    --healthy-threshold-count 3 \
    --unhealthy-threshold-count 3

Load balancers are a critical component in cloud infrastructure to ensure high availability, scalability, and reliability of applications.