avatar

Jezl

Load balancing with Elastic Load Balancer (ELB)

Load balancing refers to distributing user traffic to multiple instances.

A load balancer sits in front of your instances and distibutes traffic depending on instance load.

This diagram explains it well:

Why?

Elastic Load Balancer (ELB)

Managed service that AWS maintains. No need to configure it yourself. Integrates with:

ELB types

Different generation of EBS:

  1. Classic Load Balancer (old gen)
  2. Application Load Balancer (v2)
  3. Network Load Balancer (v2 new gen)
  4. Gateway Load Balancer - 2020 and newest

Load balancers will allow access from anywhere, but the connection betweem EBS and EC2 instances is managed through security groups so EC2 access is restricted.

Load balancer types

It’s important to note that ELB is the overall service, but there are different types of load balancers for different purposes, as noted below

Application Load Balancer (ALB)

This load balancer works at the HTTP level and allows for:

Load balancing can be based on:

ALB is good for microservices where you want to balance load between services and each service might hit a different endpoint, subdomain, container etc

Target groups

Path routing, for example, determines which target group (a set of resources) to route request to. Target groups can be:

For example query param target group routing:

Good to know:

Network Load Balancer (NLB)

This is a lower level load balancer (Layer 4) - affecting the TCP and UDP layer.

It has:

Gateway Load Balancer (GLB)

GLB allows you to analyse all traffic and drop it if necessary.

application load balancer

Target groups can be:

Sticky Sessions

Sticky sessions is a way to keep the same user on the same instance even when using a load balancer.

Usually a load balancer will distribute traffic among different instances, but if you want to keep the same user on the same instance, for example to keep their session information, you can use sticky sessions.

It’s controlled using a cookie, which is either:

Cross-Zone Load balancing

Cross-zone load balancing allows you to distribute load balancer traffic across different AZs. It can be turned on and off per load balancer.

If it’s turned on, all traffic will be spread evenly across AZs. Otherwise it won’t.

It’s automatically on for ALBs, but needs to be turned on manually for NLBs and GLBs and will incur a charge for the latter two.

Connection Draining

This gives the request time to complete while instance is de-registering or “unhealthy”.

It will stop sending requests to instances that are de-registering.

Can be set from 1 to 3600 seconds.

Auto Scaling Group (ASG)

An ASG adds or remove more instances depending on user load. It’s referred to as:

They are free! You only pay for the infra you get. They also work OOTB with load balancers, which connect automatically to new instances added.

You get to define:

You define a launch template that contains all EC2 info that are used as parameters to launch new instances.

CloudWatch alarms

When alarms are triggered (avg CPU or custom metric) the Auto Scaling Group can be enacted.

Auto Scaling Groups - Scaling Policies

Scaling policies allow us to define under what conditions to scale.

Dynamic Scaling

Predictive Scaling

ASG analyses historical load, forecasts load and then scales

Good metrics to scale on

Scaling cooldown

After removing/adding instances, which is a 5 minute cooldown where no instance changes can be made. Allows for instance stablisation