Elastic Load Balancing
Developer Guide (API Version 2012-06-01)
« PreviousNext »
View the PDF for this guide.Go to the AWS Discussion Forum for this product.Go to the Kindle Store to download this guide in Kindle format.Did this page help you?  Yes | No |  Tell us about it...

Create Sticky Sessions

By default, a load balancer routes each request independently to the application instance with the smallest load. However, you can use the sticky session feature (also known as session affinity) which enables the load balancer to bind a user's session to a specific application instance. This ensures that all requests coming from the user during the session will be sent to the same application instance.

The key to managing the sticky session is determining how long should your load balancer consistently route the user's request to the same application instance. If your application has its own session cookie, then you can set Elastic Load Balancing to create the session cookie to follow the duration specified by the application's session cookie. If your application does not have its own session cookie, then you can set Elastic Load Balancing to create a session cookie by specifying your own stickiness duration.You can associate stickiness duration for only HTTP/HTTPS load balancer listeners.

Enable Duration-Based Session 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 this cookie is present in the request. If so, 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 is inserted into the response for binding subsequent requests from the same user to that application instance. The stickiness policy configuration defines a cookie expiration, which establishes the duration of validity for each cookie. The cookie is automatically updated after its duration expires.

In this example, you create a stickiness policy and then use it to enable sticky sessions for a load balancer that has load balancer-generated HTTP cookies. Before you get started, be sure you've done the following:

  • Created a load balancer with Elastic Load Balancing. For information on how to set up a HTTPS/SSL load balancer with the AWS Management Console, command line interface (CLI), or Query API, see Create a HTTPS/SSL Load Balancer . To learn how to set up a HTTP load balancer with the AWS Management Console, see Get Started with Elastic Load Balancing.

  • Installed the Elastic Load Balancing tool that you plan to use to perform load balancing tasks. You can create a stickiness policy using the AWS Management Console, the command line interface (CLI), or the Query API. For information on installing the CLI or the Query API, see Setting Up Elastic Load Balancing Interfaces.

The following sections walk you through the steps for enabling duration-based sticky sessions for your load balancer using the AWS Management Console, the Command Line Interface (CLI), or the Query API.

Using the AWS Management Console

To enable duration-based sticky sessions for a load balancer

  1. Sign in to the AWS Management Console and open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the Amazon EC2 console Resources page, in the EC2 Dashboard pane, under NETWORK & SECURITY, click Load Balancers.

  3. On the Load Balancers page, select your load balancer.

  4. The bottom pane displays the details of your load balancer.

  5. Click (edit) in the Port Configuration: row.

    Enable Duration Based Stickiness

  6. On the Edit Stickiness page, click Enable Load Balancer Generated Cookie Stickiness.

  7. In the Expiration Period box, enter the cookie expiration period. This example creates a cookie stickiness policy with a cookie expiration period of 60 seconds.

    Specify Expiration Period

  8. Click Save. The Port Configurationrow in the Description pane shows that the stickiness policy enabled for the load balancer.

Using the Query API

To enable duration-based sticky sessions for a load balancer

  1. Call CreateLBCookieStickinessPolicy with the following parameters to create a load-balancer-generated cookie stickiness policy with a cookie expiration period of 60 seconds.

    • LoadBalancerName = MyLoadBalancer

    • PolicyName = MyLoadBalancerPolicy

    • CookieExpirationPeriod = 60

  2. Call SetLoadBalancingPoliciesOfListener with the following parameters to enable session stickiness for a load balancer using the MyLoadBalancer policy.

    • LoadBalancerName = MyLoadBalancer

    • LoadBalancerPort = 80

    • PolicyNames = MyLoadBalancerPolicy

Using the Command Line Interface

To enable duration-based sticky sessions for a load balancer

  1. Use the elb-create-lb-cookie-stickiness-policy command to create a load-balancer-generated cookie stickiness policy with a cookie expiration period of 60 seconds.

    PROMPT>elb-create-lb-cookie-stickiness-policy example-lb --policy-name MyLoadBalancerPolicy --expiration-period 60

    Elastic Load Balancing returns the following:

    OK-Creating LB Stickiness Policy
  2. Use the elb-set-lb-policies-of-listener command to enable session stickiness for a load balancer using the MyLoadBalancerPolicy.

    PROMPT>elb-set-lb-policies-of-listener example-lb --lb-port 80 --policy-names MyLoadBalancerPolicy

    Elastic Load Balancing returns the following:

    OK-Setting Policies

Enable Application-Controlled Session Stickiness

The load balancer uses a special cookie to associate the session with the original server that handled the request, but follows the lifetime of the application-generated cookie corresponding to the cookie name specified in the policy configuration. The load balancer only inserts a new stickiness cookie if the application response includes a new application cookie. The load balancer stickiness cookie does not update with each request. If the application cookie is explicitly removed or expires, the session stops being sticky until a new application cookie is issued.

If an application server fails or is removed, the load balancer will try to route the sticky session to another healthy application server. The load balancer will try to stick to new healthy application server and continue routing to currently stick application server even after the failed application server comes back. However, it is up to the new application server on how it'll respond to a request which it has not seen previously.

In this example, you configure a load balancer for session stickiness when the life of the session follows that of an application-generated cookie. Before you get started, be sure you've done the following:

  • Created a load balancer with Elastic Load Balancing. For information on how to set up a HTTPS/SSL load balancer with the AWS Management Console, command line interface (CLI), or Query API, see Create a HTTPS/SSL Load Balancer . To learn how to set up a HTTP load balancer with the AWS Management Console, see Get Started with Elastic Load Balancing.

  • Installed the Elastic Load Balancing tool that you plan to use to perform load balancing tasks. You can create a session stickiness policy using the AWS Management Console, the command line interface (CLI) or the Query API. For information on installing the CLI or the Query API, see Setting Up Elastic Load Balancing Interfaces.

The following sections walk you through the steps for enabling application-controlled sticky sessions for your load balancer using the AWS Management Console, the Command Line Interface (CLI), or the Query API.

To Enable Application-Controlled Session Stickiness

  1. Sign in to the AWS Management Console and open the Amazon EC2 console at https://console.aws.amazon.com/ec2/.

  2. On the Amazon EC2 console Resources page, in the EC2 Dashboard pane, under NETWORK & SECURITY, click Load Balancers.

  3. On the Load Balancers page, select your load balancer.

  4. The bottom pane displays the details of your load balancer.

  5. Click (edit) in the Port Configuration: row.

    Enable Duration Based Stickiness

  6. On the Edit Stickiness page, click Enable Application Generated Cookie Stickiness.

  7. In the Cookie Name box, enter the name for your cookie, for example my-cookie.

  8. Click Save.

    Cookie Name

  9. The Port Configurationrow in the Description pane shows the newly created cookie stickiness policy.

To Enable Application-Controlled Session Stickiness

  1. Call CreateAppCookieStickinessPolicy with the following parameters to create an application-generated cookie stickiness policy.

    • LoadBalancerName = my-load-balancer

    • PolicyName = my-app-cookie-lb-policy

    • CookieName = my-cookie

  2. Call SetLoadBalancingPoliciesOfListener with the following parameters to enable session stickiness for a load balancer using the my-load-balancer policy.

    • LoadBalancerName = my-load-balancer

    • LoadBalancerPort = 80

    • PolicyNames = my-app-cookie-lb-policy

To Enable Application-Controlled Session Stickiness

  1. Use the elb-create-app-cookie-stickiness-policy command to create a load application-generated cookie stickiness policy .

    PROMPT>elb-create-app-cookie-stickiness-policy my-load-balancer -p my-app-cookie-lb-policy -c my-cookie

    Elastic Load Balancing returns the following:

    OK-Creating App Stickiness Policy
  2. Use the elb-set-lb-policies-of-listener command to enable session stickiness for a load balancer using the my-load-balancer policy.

    PROMPT>elb-set-lb-policies-of-listener example-lb --lb-port 80 --policy-names my-app-cookie-lb-policy

    Elastic Load Balancing returns the following:

    OK-Setting Policies