

# Sticky sessions with load balancer generated cookies
<a name="alb-cookies-stickiness"></a>

When you use an Application Load Balancer with a load balancer generated cookie:
+ The Application Load Balancer uses the [target group weight](https://aws.amazon.com/premiumsupport/knowledge-center/elb-make-weighted-target-groups-for-alb/) to determine how to balance the incoming traffic between the target groups.
+ By default, the Application Load Balancer uses the round robin method [to route requests to the EC2 instances](https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-target-groups.html#modify-routing-algorithm) in the destination target group.

  After traffic has been initially routed to an instance, subsequent traffic will stick to that EC2 instance for a specified duration.

**Template:** Use the CloudFormation template `stickysessionslb.yml` (included in the [sample code .zip file](samples/stickiness.zip)) to try out sticky sessions with load balancer generated cookies. 

## Common use cases
<a name="alb-cookies-use-cases"></a>

Use sticky sessions with load balancer generated cookies in these scenarios:
+ PHP web servers
+ Servers that maintain temporary session data such as logs, shopping carts, or chat conversations

## Code changes from basic.yml
<a name="alb-cookies-code-changes"></a>

The relevant code changes are in the target group configuration, to set the stickiness type to `lb_cookie` and the duration to 10 seconds.


****  

|  |  | 
| --- |--- |
| basic.yml | stickysessionslb.yml | 
| <pre>TG1:<br />   Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'<br />   Properties:<br />     Name: TG1<br />     Protocol: HTTP<br />     Port: 80<br />     TargetType: instance<br />     Targets:<br />       - Id: !Ref Instance1<br />       - Id: !Ref Instance2<br />VpcId: !Ref CustomVPC</pre> | <pre>TG1:<br />   Type: 'AWS::ElasticLoadBalancingV2::TargetGroup'<br />   Properties:<br />     Name: TG1<br />     Protocol: HTTP<br />     Port: 80<br />     TargetType: instance<br />     Targets:<br />       - Id: !Ref Instance1<br />       - Id: !Ref Instance2<br />     VpcId: !Ref CustomVPC<br />     TargetGroupAttributes:<br />       - Key: stickiness.enabled<br />         Value: true<br />       - Key: stickiness.type<br />         Value: lb_cookie<br />       - Key: stickiness.lb_cookie.duration_seconds<br />Value: 10</pre> | 

## Steps
<a name="alb-cookies-steps"></a>

**Notes**  
NAT gateways incur a small cost.
Multiple EC2 instances will use up your free tier hours faster than a single EC2 instance.

1. Deploy the CloudFormation template `stickysessionslb.yml` in a lab environment.

1. Wait until the health status of your target group instances changes from **initial** to **healthy**.

1. Navigate to the Application Load Balancer URL in a web browser, using HTTP (TCP/80).

   For example: `http://alb-123456789.us-east-1.elb.amazonaws.com/`

   The webpage displays one of the following: **Instance 1 - TG1**, **Instance 2 - TG1**, **Instance 3 - TG2**, or **Instance 4 - TG1**.

1. Refresh the page multiple times.

## Expected results
<a name="alb-cookies-results"></a>

**Note**  
The CloudFormation template in this example configures the stickiness to last 10 seconds.

The instance that loads the web page should stay the same within the 10-second duration, as reflected in the page text. After approximately 10 seconds, the stickiness is released and the destination instance might change.

## How it works
<a name="alb-cookies-how"></a>
+ In this example, two EC2 instances are present in one target group. The EC2 instances have an Apache web server (`httpd`) installed, and the `index.html` page text on each EC2 instance is hardcoded to be distinct.
+ The Application Load Balancer creates a binding for the user's session, which binds toward the destination, with an expiration time.
+ When you reload the page, the Application Load Balancer checks whether the binding exists and has not expired.
  + If the binding has expired or doesn’t exist, the Application Load Balancer runs its routing logic and determines the destination instance. 
  + If the binding has not expired, the Application Load Balancer routes traffic to the same destination instance.