Configuring Sticky Sessions for Your Load Balancers

You can create a sticky session policy to bind the session of a user to a specific backend virtual machine (VM).

This policy can be based on a specific duration, or controlled by an application of the VM. For more information, see About Load Balancers > Sticky Sessions.

After creating the policy, you must enable it for it to apply.

Create or Enable the Sticky Session Policy Using OSC CLI

Before you begin: Ensure your load balancer has a listener for the HTTP or HTTPS protocols, as sticky sessions can be configured for these protocols only. For more information, see Adding or Deleting Listeners.

The CreateLoadBalancerPolicy command creates a stickiness policy with sticky session lifetimes defined by the browser lifetime.
The created policy can be used with HTTP or HTTPS listeners only.
If this policy is implemented by a load balancer, this load balancer uses this cookie in all incoming requests to direct them to the specified backend server virtual machine (VM). If this cookie is not present, the load balancer sends the request to any other server according to its load-balancing algorithm.

You can also create a stickiness policy with sticky session lifetimes following the lifetime of an application-generated cookie.
Unlike the other type of stickiness policy, the lifetime of the special Load Balancer Unit (LBU) cookie follows the lifetime of the application-generated cookie specified in the policy configuration. The load balancer inserts a new stickiness cookie only when the application response includes a new application cookie.
The session stops being sticky if the application cookie is removed or expires, until a new application cookie is issued.

For more information, see About Load Balancers.

Request sample: Creating a load balancer policy based on browser
$ osc-cli api CreateLoadBalancerPolicy --profile "default" \
    --LoadBalancerName "example-lbu" \
    --PolicyName "example-browser-policy" \
    --PolicyType "load_balancer"
Request sample: Creating a load balancer policy based on application cookie
$ osc-cli api CreateLoadBalancerPolicy --profile "default" \
    --LoadBalancerName "example-lbu" \
    --PolicyName "example-app-policy" \
    --PolicyType "app" \
    --CookieName "example-cookie"

This command contains the following attributes that you need to specify:

  • CookieExpirationPeriod: (optional) The lifetime of the cookie, in seconds. If not specified, the default value of this parameter is 1, which means that the sticky session lasts for the duration of the browser session.

  • CookieName: (optional) The name of the application cookie used for stickiness. This parameter is required if you create a stickiness policy based on an application-generated cookie.

  • DryRun: (optional) If true, checks whether you have the required permissions to perform the action.

  • LoadBalancerName: The name of the load balancer for which you want to create a policy.

  • PolicyName: The name of the policy. This name must be unique and consist of alphanumeric characters and dashes (-).

  • PolicyType: The type of stickiness policy you want to create: app or load_balancer.

The CreateLoadBalancerPolicy command returns the following elements:

  • LoadBalancer: Information about the load balancer.

  • ResponseContext: Information about the context of the response.

Result sample: Creating a load balancer policy based on browser
{
  "ResponseContext": {
    "RequestId": "0475ca1e-d0c5-441d-712a-da55a4175157"
  },
  "LoadBalancer": {
    "Tags": [],
    "SourceSecurityGroup": {
      "SecurityGroupName": "default",
      "SecurityGroupAccountId": "123456789012"
    },
    "Subnets": [
      "subnet-12345678"
    ],
    "NetId": "vpc-12345678",
    "BackendVmIds": [],
    "ApplicationStickyCookiePolicies": [],
    "SecurityGroups": [
      "sg-12345678"
    ],
    "LoadBalancerType": "internet-facing",
    "AccessLog": {
      "PublicationInterval": 60,
      "IsEnabled": false
    },
    "DnsName": "example-lbu-123456789.eu-west-2.lbu.outscale.com",
    "HealthCheck": {
      "UnhealthyThreshold": 2,
      "Timeout": 5,
      "CheckInterval": 30,
      "Protocol": "TCP",
      "HealthyThreshold": 10,
      "Port": 80
    },
    "LoadBalancerStickyCookiePolicies": [
      {
        "PolicyName": "example-browser-policy",
        "CookieExpirationPeriod": 1
      }
    ],
    "SubregionNames": [
      "eu-west-2a"
    ],
    "Listeners": [
      {
        "BackendPort": 80,
        "BackendProtocol": "HTTP",
        "LoadBalancerPort": 80,
        "LoadBalancerProtocol": "HTTP"
      }
    ],
    "LoadBalancerName": "example-lbu"
  }
}
Result sample: Creating a load balancer policy based on application cookie
{
  "ResponseContext": {
    "RequestId": "0475ca1e-d0c5-441d-712a-da55a4175157"
  },
  "LoadBalancer": {
    "Tags": [],
    "SourceSecurityGroup": {
      "SecurityGroupName": "default",
      "SecurityGroupAccountId": "123456789012"
    },
    "Subnets": [
      "subnet-12345678"
    ],
    "NetId": "vpc-12345678",
    "BackendVmIds": [],
    "ApplicationStickyCookiePolicies": [
      {
        "PolicyName": "example-app-policy",
        "CookieName": "example-cookie"
      }
    ],
    "SecurityGroups": [
      "sg-12345678"
    ],
    "LoadBalancerType": "internet-facing",
    "AccessLog": {
      "PublicationInterval": 60,
      "IsEnabled": false
    },
    "DnsName": "example-lbu-123456789.eu-west-2.lbu.outscale.com",
    "HealthCheck": {
      "UnhealthyThreshold": 2,
      "Timeout": 5,
      "CheckInterval": 30,
      "Protocol": "TCP",
      "HealthyThreshold": 10,
      "Port": 80
    },
    "LoadBalancerStickyCookiePolicies": [],
    "SubregionNames": [
      "eu-west-2a"
    ],
    "Listeners": [
      {
        "BackendPort": 80,
        "BackendProtocol": "HTTP",
        "LoadBalancerPort": 80,
        "LoadBalancerProtocol": "HTTP"
      }
    ],
    "LoadBalancerName": "example-lbu"
  }
}

Create or Enable the Sticky Session Policy Using AWS CLI

Create the Sticky Session Policy

Duration-Based Sticky Sessions

Before you begin: Ensure your load balancer has a listener for the HTTP or HTTPS protocols, as sticky sessions can be configured for these protocols only. For more information, see Adding or Deleting Listeners.

To create a duration-based sticky session policy, use the create-lb-cookie-stickiness-policy command following this syntax:

Request sample
$ aws elb create-lb-cookie-stickiness-policy \
    --profile YOUR_PROFILE \
    --load-balancer-name LOAD_BALANCER_NAME \
    --policy-name POLICY_NAME \
    --cookie-expiration-period 3600 \
    --endpoint https://lbu.eu-west-2.outscale.com

This command contains the following attributes that you need to specify:

  • (optional) profile: The named profile you want to use, created when configuring AWS CLI. For more information, see Installing and Configuring AWS CLI.

  • load-balancer-name: The name of the load balancer for which you want to configure the policy.

  • policy-name: A name for the policy.

    This name must consist of alphanumerical characters and dashes (-).

  • (optional) cookie-expiration-period: The duration of the sticky session, in seconds.

    This duration must be greater than 0. By default, the value is 1, which means that the sticky session lasts for the duration of the browser session.

  • endpoint: The endpoint corresponding to the Region you want to send the request to.

The duration-based sticky session policy is created for the specified load balancer.

Application-Controlled Sticky Sessions

Before you begin: Ensure your load balancer has a listener for the HTTP or HTTPS protocols, as sticky sessions can be configured for these protocols only. For more information, see Adding or Deleting Listeners.

To create an application-controlled sticky session policy, use the create-app-cookie-stickiness-policy command following this syntax:

Request sample
$ aws elb create-app-cookie-stickiness-policy \
    --profile YOUR_PROFILE \
    --load-balancer-name LOAD_BALANCER_NAME \
    --policy-name POLICY_NAME \
    --cookie-name COOKIE_NAME \
    --endpoint https://lbu.eu-west-2.outscale.com

This command contains the following attributes that you need to specify:

  • (optional) profile: The named profile you want to use, created when configuring AWS CLI. For more information, see Installing and Configuring AWS CLI.

  • load-balancer-name: The name of the load balancer for which you want to configure the policy.

  • policy-name: A name for the policy.

    This name must consist of alphanumerical characters and dashes (-).

  • cookie-name: The name of the application cookie used for stickiness.

  • endpoint: The endpoint corresponding to the Region you want to send the request to.

The application-controlled sticky session policy is created for the specified load balancer.

Enable the Sticky Session Policy

To enable the sticky session policy, use the set-load-balancer-policies-of-listener command following this syntax:

Request sample
$ aws elb set-load-balancer-policies-of-listener \
    --profile YOUR_PROFILE \
    --load-balancer-name LOAD_BALANCER_NAME \
    --load-balancer-port 80 \
    --policy-names POLICY_NAME \
    --endpoint https://lbu.eu-west-2.outscale.com

This command contains the following attributes that you need to specify:

  • (optional) profile: The named profile you want to use, created when configuring AWS CLI. For more information, see Installing and Configuring AWS CLI.

  • load-balancer-name: The name of the load balancer for which you want to enable the policy.

  • load-balancer-port: The external port of the load balancer (between 1 and 65535, both included).

  • policy-names: The name of the policy you want to enable for the listener.

    • Only one policy can be active at a time. If there is already a policy enabled for the listener, the new one you specify replaces it.

    • To disable an enabled policy, specify [] for this parameter.

  • endpoint: The endpoint corresponding to the Region you want to send the request to.

The sticky session policy is enabled for the specified load balancer.

Related Pages

Corresponding API Methods

AWS™ and Amazon Web Services™ are trademarks of Amazon Technologies, Inc or its affiliates in the United States and/or other countries.