# ELB

[Sticky session](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/sticky-sessions.html) |

## Overview

* Distribute traffic across AZs.&#x20;
* AWS has 4 kinds of load balancers:
  * 3 popular ELB are:

<table><thead><tr><th width="112">-</th><th width="320">ALB (L7)</th><th width="171">NLB (L4)</th><th>CLB (old)</th></tr></thead><tbody><tr><td>protocol</td><td>HTTP, HTTPS, Websocket, <mark style="color:red;"><strong>gRPC</strong></mark></td><td>TCP, TLS (secure TCP), <mark style="color:red;"><strong>UDP</strong></mark></td><td>HTTP, HTTPS, TCP, SSL</td></tr><tr><td>DNS Name</td><td>Yes</td><td>Yes</td><td>Yes</td></tr><tr><td>Static IP</td><td>No</td><td>Yes</td><td>No</td></tr><tr><td>Benefit</td><td>route traffic to different Target group <mark style="color:red;"><strong>based on </strong></mark><em><mark style="color:red;"><strong>URL Path, Hostname, HTTP headers, and Query Strings</strong></mark>.</em></td><td><ul><li>Highest performance</li><li>Lowest latency</li></ul></td><td></td></tr><tr><td>Target group</td><td></td><td><ul><li>EC2 instance</li><li>private IP add</li><li>ALB</li></ul></td><td></td></tr><tr><td>Health check</td><td></td><td>Support HTTP, HTTPS, TCP</td><td></td></tr><tr><td>Note</td><td><ul><li>Reserved cookie names: <code>AWSALB</code>, <code>AWSALBAPP</code>, <code>AWSALBTG</code>.</li><li>To get the client's IP address, ALB adds an additional header called <mark style="color:red;"><strong><code>X-Forwarded-For</code></strong></mark> contains the client's IP address.</li></ul></td><td>has 1 static IP /AZ -> can attach Elastic IP</td><td></td></tr></tbody></table>

* Gateway Load Balancer (L3 - Network layer)

### Use cases

* Adding elasticity to your application. (+ with Route53 [Weighted Routing policy](https://mamawhocode.gitbook.io/aws/network/route53#routing-policy)).

***

## Features

### ELB Sticky Session

* Ensures traffic for the same client is always redirected to the same target (e.g., EC2 instance). This helps that the client does not lose his session data. To use sticky sessions, ***the client must support cookies***.
* Sticky sessions are enabled at the target group level.
* ALB use the `Expires` attribute in the cookie header instead of the `Max-Age` attribute.
* Sticky sessions *<mark style="color:red;">rely on a cookie</mark>* that is *<mark style="color:red;">not going to be consistent across devices</mark>*.

{% hint style="info" %}
Sticky sessions are not supported if [cross-zone load balancing is disabled](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/disable-cross-zone.html).&#x20;

With cross-origin resource sharing (CORS) requests, some browsers require `SameSite=None; Secure` to enable stickiness.&#x20;
{% endhint %}

### ELB Health Checks

* When you enable `ELB Health Checks`, your ELB won't send traffic to unhealthy (crashed) EC2 instances.
* When an EC2 instance fails the ALB Health Checks, it is marked unhealthy and will be terminated while the ASG launches a new EC2 instance.

### Cross Zone Load Balancing

When Cross-Zone Load Balancing is enabled, ELB distributes traffic <mark style="color:red;">evenly</mark> <mark style="color:red;">across</mark> all registered EC2 <mark style="color:red;">instances</mark> in all AZs.

| ALB                | NLB                 | CLB                 |
| ------------------ | ------------------- | ------------------- |
| enabled by default | disabled by default | disabled by default |
| free               | $ for inter-AZ      | free                |

### SSL certificate

Manage certificates using ACM (AWS Certificate Manager)

<figure><img src="https://2259236002-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fuh9xZDZ53qGqmMCM44PU%2Fuploads%2FhhSaAjr79CzZpR359QHu%2Fimage.png?alt=media&#x26;token=444500d5-8e8f-42d9-bb49-5526770653ad" alt=""><figcaption><p>X.509 certificate</p></figcaption></figure>

### Server Name Indication

Allow you load multi SSL certificates on one listener.

***

## Trivia

* Only NLB provides ***both*** static <mark style="color:red;">DNS name</mark> and <mark style="color:red;">static IP</mark>. While, ALB & CLB provide a static <mark style="color:red;">DNS name</mark> but it does NOT provide a static IP.
* When using an ALB to distribute traffic to your EC2 instances, the IP address you'll receive requests from will be the ALB's private IP addresses. To get the client's IP address, ALB adds an additional header called `X-Forwarded-For` contains the client's IP address.
* ELB waits 300 seconds before the completion of the deregistration process, which can help in-flight requests to the target become complete. To change the amount of time that Elastic Load Balancing waits, update the deregistration delay value.
* Only ALB support [gRPC](#concepts).

***

## Concepts

* ***gRPC*** (Remote Procedure Call): a protocol, a way that different parts of a computer program can talk to each other, even if they are on different computers or in different places.
