What is session persistence used for?

Session persistence is a method to forward requests from the same client to the same backend server.

How do I enable session persistence?

You can enable session persistence when you configure a Classic Load Balancer (CLB) listener. You can configure different session persistence policies for different listeners. A session can be persisted for at most 86400 seconds, which is equivalent to 24 hours.

What types of session persistence does CLB support?

  • CLB persists TCP and UDP sessions based on source IP addresses at Layer 4. A session can be persisted for at most 3600 seconds at Layer 4.
  • CLB persists HTTP and HTTPS sessions based on cookies at Layer 7. A cookie-based session can be persisted for at most 86400 seconds, which is equivalent to 24 hours.

What are the methods that can be used to handle cookies?

HTTP and HTTPS listeners support cookie inserts and cookie rewrites.

  • Insert a cookie: If you select this option, you need only to specify the timeout period of the cookie. CLB inserts a cookie (SERVERID) into the first HTTP or HTTPS response packet that is sent to a client. The next request from the client contains this cookie, and CLB forwards this request to the recorded Elastic Compute Service (ECS) instance.
  • Rewrite a cookie: If you select this option, you can specify the cookie to be carried in an HTTP or HTTPS response. You must configure the timeout period and the lifetime of a cookie on an ECS instance. When CLB detects a user-defined cookie, CLB overwrites the original cookie with the user-defined cookie. The next request from the client carries the user-defined cookie, and CLB forwards this request to the recorded backend server. For more information about how to configure cookies on a server, see Configure session persistence.

Can CLB persist sessions based on domain names?

Yes, CLB can persist sessions based on domain names.

CLB can rewrite cookies to persist sessions based on domain names.

How long is the timeout period of a cookie?

  • You can specify the timeout period of a cookie (SERVERID) to be inserted by CLB from 1 to 86400 seconds in the console.
  • For a user-defined cookie with which you want CLB to overwrite, you must configure the timeout period on the ECS instance.

How do I view a cookie?

Open the browser and press F12 to check whether SERVERID or a user-defined cookie is inserted in the response. You can also run the curl www.example.com -c /tmp/cookie123 command to save a cookie and then run the curl www.example.com -b /tmp/cookie123 command to view the cookie.

Why does session persistence fail?

  • Check whether session persistence is enabled for the listener.
  • HTTP and HTTPS listeners cannot persist sessions by inserting cookies into responses that carry 4xx status codes.

    Solution: Use TCP listeners instead of HTTP or HTTPS listeners. TCP listeners persist sessions based on client IP addresses. Backend servers can also insert or even validate cookies to ensure that sessions are persisted.

  • HTTP 302 redirects change the SERVERID string for persisting a session.

    When CLB inserts a cookie into a response that carries the HTTP status code 302, the SERVERID string is changed. As a result, the session cannot be persisted.

    To verify the cause, check the requests and responses by using your browser or packet capture software. Then, check whether a 302 status code is included in the packets and whether the SERVERID string in the cookie is changed.

    Solution: Use TCP listeners instead of HTTP or HTTPS listeners. TCP listeners persist sessions based on client IP addresses. Backend servers can also insert or even validate cookies to ensure that sessions are persisted.

  • The timeout period is set to a small value. You can set the timeout period to a greater value.

How do I verify session persistence by using the Linux curl command?

  1. Create a test page.

    Create a test page on each backend server. You can view the private IP address of the backend server on the test page. The following figure shows an example of a test page. The private IP address indicates the backend server to which requests are distributed. The private IP address is used to check whether CLB can persist sessions.

  2. Run the curl command in Linux.

    In this example, the IP address of the CLB instance that runs Linux is 10.170.XX.XX and the URL of the created page is http://10.170.XX.XX/check.jsp.

    1. Log on to a server that runs Linux.
    2. Run the following command to query the cookie inserted by the backend server:
      curl -c test.cookie http://10.170.XX.XX/check.jsp
      Note By default, CLB persists sessions by inserting cookies. However, curl does not send or save cookies. Therefore, you must save a cookie before you perform the test. Otherwise, the curl test result may show that session persistence is invalid.
    3. After you save the cookie, run the following command:
      for ((a=1;a<=30;a++));
          do curl  -b test.cookie http://10.170.XX.XX/check.jsp  | grep '10.170.XX.XX';
          sleep 1;
      done
      Note a<=30 indicates the number of tests to be performed. You can set this value based on your business requirements. Set the IP address in grep '10.170.XX.XX' to the private IP address of your ECS instance.
    4. Check the IP addresses returned in the preceding tests. If the same IP address is returned, it indicates that CLB can persist sessions.