Updated on 2022-01-25 GMT+08:00

Sticky Session

Sticky sessions are a mechanism that ensures that requests from a client always get routed to the same server before a session elapses.

Here we use an example to explain what sticky sessions can do. Suppose that you have logged in to a server. After a while, you send another request. If sticky sessions are not enabled, the request may be routed to another server, and you will be asked to log in again. If sticky sessions are enabled, all your requests are processed by the same server, and you just need to log in once.

Sticky Sessions at Layer 4

Sticky sessions at Layer 4 use source IP addresses to maintain sessions. Requests from the same IP address are routed to the same backend server during the session.

Sticky sessions at Layer 4 will be invalid in the following scenarios:

  • Source IP addresses of the clients change.
  • Requests from the clients exceed the session stickiness duration.
  • You can set the stickiness duration only when you select Weighted round robin as the load balancing algorithm.
  • The default stickiness duration is 20 minutes, and the maximum duration is 60 minutes (that is, 1 hour).

Sticky Sessions at Layer 7

Sticky sessions at Layer 7 allow you to use load balancer cookies or application cookies to maintain sessions. Choose an appropriate sticky session type to better distribute requests across backend servers.

  • Load balancer cookie: The load balancer generates a cookie after receiving a request from the client. All subsequent requests with the cookie are routed to the same backend server for processing.
  • Application cookie: The application deployed on the backend server generates a cookie after receiving the first request from the client. All requests with the cookie are routed to this backend server.

Sticky sessions at Layer 7 will be invalid in the following scenarios:

  • If requests sent by the clients do not contain a cookie, sticky sessions do not take effect.
  • Requests from the clients exceed the session stickiness duration.
  • You can set the stickiness duration only when you select Weighted round robin as the load balancing algorithm.
  • The default stickiness duration is 20 minutes, and the maximum duration is 1440 minutes (that is, 24 hours).

ELB supports three types of sticky session, including Source IP address, Load balancer cookie, and Application cookie.

Enable Sticky Sessions

  1. Log in to the management console.
  2. In the upper left corner of the page, click and select the desired region and project.
  3. Click Service List. Under Network, click Elastic Load Balance.
  4. Locate the target load balancer and click its name.
  5. Click Backend Server Groups, locate the target backend server group, and click on the right of its name.
  6. Enable the sticky session feature, select the sticky session type, and set the session stickiness duration.
  7. Click OK.