# Elastic load balancer

## Icon

![ELB Icon](https://icon.icepanel.io/AWS/svg/Networking-Content-Delivery/Elastic-Load-Balancing.svg)

## [Introduction](https://github.com/jojijoy15/Notes/blob/main/Cloud/AWS/CloudPractitioner/03_Elastic_LoadBalancer.md)

* Elastic load balancer is a managed service which can spread load across multiple services.
* It is a **regional service**, making them highly available.
* Exposes a single point of access for DNS to your application.
* AWS takes care of the infra and upgrades of load balancers.
* Provides SSL termination for your websites.
* Offers health checks _(health endpoints)_, to make sure the requests are forwarded only to health instances.
* Can be setup as public or private load balancer depending on the scheme.
* Provides high availability across zones.
* Can enforce stickiness with cookies.

## Target groups

* In general, group of AWS resources which would receive traffic from load balancer.
* Allows grouping of
  * EC2 instances
  * ECS tasks
  * IP addresses _(must be private)_
  * Lambda function
  * Application load balancer
* Read more about it [here](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html).

## Listener

* A listener is a process that checks for connection requests, using the protocol and port that you configure.
* The rules that you define for a listener determine how the load balancer routes requests to its registered targets.
* Read more about it [here](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-listeners.html).

## Health Checks

* To enable load balancer to know if the instance is healthy and can forward traffic to it, health checks play crucial role.
* Health check is done using `/health` endpoint _(typically)_ running on port `4567` using `http` protocol.
* They are done at target group level.
* Health checks need to be configured when creating a target group before registering targets in to the target group.

## Types of Load Balancer

### Classic Load Balancer (CLB)

* Deprecated and [retired](https://aws.amazon.com/blogs/aws/ec2-classic-is-retiring-heres-how-to-prepare/), old generation _(v1)_.
* Supports `HTTP`, `HTTPS`, `TCP`, `SSL`.
* Only supported one SSL certificate for `HTTPS` based listeners.

### Application Load Balancer _(ALB)_

* Newer generation, 2016.
* Works on layer 7 of OSI model, supports `HTTP/1.x`, `HTTP/2` `HTTPS`, `WebSocket`, `gRPC`.
* Supports redirect from `HTTP` to `HTTPS`.
* Great fit for microservices and container-based application.
* Supports routing to different target groups based on,
  * Path in URL
  * Hostname in URL
  * Query strings and headers
* Port mapping feature allow to redirect to a dynamic port in ECS.
* The IP address of the client is available in the header `X-Forwarded-For`, the port of the client in header `X-Forwarded-Port`, the protocol in header `X-Forwarded-Proto`.
* Target groups can be EC2 instances, ECS Task, Lambda functions _(requests are translated to `JSON` event)_, Private IP Address.
* Health checks are done at target group level.
* ALB as has a fixed hostname of the form `xxx.region.elb.amazonaws.com`.

### Network Load Balancer _(NLB)_

* Newer generation, 2017.
* Not available in free-tier option.
* Target groups can be EC2 instances, Private IP Addresses, ALB.
* Layer 4 load balancer, supports `TCP`, `TLS`, `UDP`.
* High performance, with ultra-lower latency.
* Has one **static IP** per AZ, with ability to assign elastic IP to each AZ.
* Health check supports `TCP`, `HTTP` and `HTTPS`.

### Gateway Load Balancer _(GWLB)_

* Newer generation, introduced in 2020.
* Operates at network layer 3.
* Target groups can be EC2 instances, Private IP Addresses.
* Can be used to deploy, manage and scale a fleet of 3rd party virtual appliances which includes Firewalls, IDPS, Deep packet inspection etc.
* Uses `GENEVE` protocol at port `6081`.

## References

* [Working of Load balancer](https://docs.aws.amazon.com/elasticloadbalancing/latest/userguide/how-elastic-load-balancing-works.html)
* [ALB v/s NLB](https://blog.cloudcraft.co/alb-vs-nlb-which-aws-load-balancer-fits-your-needs/)
