How to Set an Elastic Load Balancer (ELB) with Session Stickiness
Previously we saw how to create an Elastic Load Balancer (ELB). By Default, an ELB routes each request independently to the application instance with the smallest load. If your application uses server session which gets registered with one of the server instances during the first request, you will have problems when a request from the same user is redirected to another instance (under the same ELB), where the session is not registered. To overcome that, the AWS ELB provides the “sticky session” feature, which enables the Load Balancer to bind a user’s session to a specific application instance. This feature is also known as “session affinity”, both mean that once a session is started, the same instance serves all requests for that session.
The key to managing the sticky session is determining how long your load balancer should consistently route the user’s request to the same instance. Be aware that enabling session stickiness is not a recommended option to make your application scalable. To make an application scalable either don’t use web session or use mechanisms which reduce dependency on a specific web server of individual instance.
In this guide we will show how to create session stickiness with AWS ELB.
For this guide, we created an ELB named “AWSFirstELB”.
Using AWS UI Console
1. Select the specific Elastic Load Balancer and click on the tab “Description”. The stickiness is disabled by default.
[Newvem analyzes your baseline disaster recovery (DR) status, reflecting how well AWS DR best practices have been implemented, and recommends AWS features and best practices to reach optimal availability, increase outage protection, and quick recovery. Learn More about Newvem's features]
2. To enable stickiness, click “(edit)”. It will pop up the following page.
3. To enable session stickiness, there are two options. One is to enable through a Load Balancer generated cookie. That is called duration based stickiness, as explained below.
>>> Duration based stickiness
The Load Balancer uses a special load-balancer-generated cookie to track the application instance for each request. When the load balancer receives a request, it first checks to see if any cookie for that user is present in the request. If it is present, the request is sent to the application instance specified in the cookie. If there is no cookie, the Load Balancer chooses an application instance based on the existing load balancing algorithm. A cookie will be inserted into the response for binding subsequent requests from the same user to that application instance. We need to set the cookie expiry, which establishes the duration of validity for each cookie.
5. Select the option “Enable Load Balancer Generated Cookie Stickiness”.
6. Enter the cookie expiration period. Click “Save”.
7. It will enable session stickiness and set the cookie expiration for each cookie generated on ELB and linked with user request to 60 seconds, as highlighted below.
Using AWS CLI tools
8. If you want to use the command line interface to create session stickiness based on ELB cookie you need to run the commands below.
9. First set the AWS ELB CLI.
10. Set the AWS Region with the following command:
Set AWS_ELB_URL=https://elasticloadbalancing.us-west-2.amazonaws.com
11. Run the command below to create a load-balancer-generated cookie stickiness policy with a cookie expiration period.
elb-create-lb-cookie-stickiness-policy <ELB Name> --policy-name <Policy Name> --expiration-period <Time in Secs>
Provide your ELB name, Policy Name and time in seconds.
12. Enable this policy with the command:
elb-set-lb-policies-of-listener <ELB Name> --lb-port <Port where your listener is configured> --policy-names <Name of the policy created above>
13. The commands with the actual values are shown below.
>>> Application controlled stickiness
Another option to enable session stickiness is “Application controlled stickiness” - In Application controlled stickiness, the Load Balancer uses a special cookie to associate the session with the original server instance that handled the request, but instead of following the duration configured on step #6, here it follows the lifetime of the application-generated cookie corresponding to the cookie name specified. The Load Balancer only inserts a new stickiness cookie if the application response includes a new application cookie. If the application cookie is explicitly removed or expired, the session stops being sticky until a new application cookie is issued in response.
14. On the “Edit Stickiness” window, select the option “Enable Application generated Cookie Stickiness”. Provide the name of the cookie and click “Save”.
15. Once set, it will be displayed on the ELB console as shown below.
16. To setup the application generated cookie through a command line interface, run the commands below.
17. First set the AWS ELB CLI.
18. Set the AWS Region with the following command:
Set AWS_ELB_URL=https://elasticloadbalancing.us-west-2.amazonaws.com
19. Run the command below to load the application-generated cookie stickiness policy.
elb-create-app-cookie-stickiness-policy <ELB Name> -p <Policy Name> -c <Cookie Name>
Here provide your ELB name, Policy Name and time in seconds.
20. Enable the policy with the following command:
elb-set-lb-policies-of-listener <ELB Name> --lb-port <Listener Port> --policy-names <Name of the policy created above>
21. The commands with the actual values are shown below.
[Newvem analytics tracks you AWS cloud utilization:
- Hourly Utilization Pattern Analysis
- Reserved Instances Decision Tool
- Resource Resizing Opportunities
Create Your Free Account or Learn More about Newvem]
Keywords: Amazon web services, Amazon AWS console, AWS S3, Amazon Cloud Services, AWS Management Console, AWS ELB, Elastic Load Balancer, CLI, Command Line Tools, ELB Session Stickiness, AWS API, Session, session affinity, AWS Console, Application controlled stickiness, Duration based stickiness
You must be logged in to post a comment.