All Products
Search
Document Center

Server Load Balancer:Configure session persistence

Last Updated:Aug 17, 2026

Some applications, such as those that manage shopping carts, login information, user preferences, or game states, require requests from a single client to be consistently routed to the same backend server. If a client's requests are distributed across different servers, the session state can be lost, degrading the user experience. By enabling session persistence for Application Load Balancer (ALB), you can ensure that requests from the same client are forwarded to the same backend server to maintain a consistent user experience and data integrity.

Background

By default, ALB distributes requests to different backend servers. After you enable session persistence, requests from the same client are forwarded to the same backend server. This allows the backend server to maintain status information and provide continuous service to the client.

  • Session persistence disabled: ALB forwards each request from the same client independently, so consecutive requests from that client may be distributed to different backend servers. In some scenarios, such as scenarios in which you log on to a backend server to obtain interaction information, you may need to log on to the backend server multiple times.

  • Session persistence enabled: Requests from the same client are distributed to the same backend server of ALB. In some scenarios, such as scenarios in which you log on to a backend server to obtain interaction information, you do not need to log on to the backend server multiple times.

When you enable session persistence for ALB, you must select a method for handling cookies. ALB supports two methods: insert cookie and rewrite cookie.

  • Insert cookie: On the client's first request, ALB inserts a cookie into the response. Specifically, ALB inserts two cookies: SERVERID and SERVERCORSID. The SERVERCORSID cookie is based on SERVERID and includes the samesite=None attribute. When the client makes subsequent requests that include this cookie, ALB directs the requests to the previously recorded backend server.

    Note

    The insert cookie method automatically includes SameSite=None, so no additional configuration is required. This effectively resolves issues where browsers cannot save the cookie in cross-origin resource sharing (CORS) scenarios that involve ALB forwarding rules.

  • Rewrite cookie: When ALB detects a user-defined cookie, it rewrites the original cookie. When the client makes subsequent requests with the new cookie, ALB directs the requests to the previously recorded backend server.

Limitations

  • ALB Extensible Edition does not support session persistence.

  • Session persistence is not required for Function Compute server groups. For more information, see Create and delete a server group.

Prerequisites

  • You have an Internet-facing ALB instance in the Running state. For more information, see Create and manage an ALB instance.

  • You have created a server-type or IP-type server group. For more information, see Create and delete a server group.

  • You have created two backend servers, ECS01 and ECS02, to receive requests. Each instance runs a different backend service to return a distinct response. For example, a request to ECS01 returns "Hello World ! This is ECS01.", and a request to ECS02 returns "Hello World ! This is ECS02.". Make sure the security group allows inbound traffic on the required service ports.

  • You have added the backend servers ECS01 and ECS02 to the server group. For more information, see Add and remove backend servers.

  • You have configured a listener for the instance. For more information, see Add an HTTP listener, Add an HTTPS listener, or Add a QUIC listener.

Step 1: Configure session persistence

  1. Log on to the ALB console.

  2. In the top navigation bar, select the region where the server group is deployed.

  3. In the left-side navigation pane, choose ALB > Server Groups.

  4. On the Server Groups page, find the target server group and click Edit Server Group in the Actions column.

  5. In the Modify Basic Information dialog box, enable session persistence.

    Enable Session Persistence and select a Cookie Option.

    • Select Insert Cookie, set the Session Persistence Timeout Period, and then click Save.

    • Select Rewrite Cookie, set the Cookie Name, and then click Save.

      In this example, the cookie name is set to BACKEND_SERVER. This name is for demonstration purposes only. You can specify a custom name.

(Optional) Step 2: Configure cookies on backend servers

Configure the cookie on the backend servers only if you use the rewrite cookie method for session persistence.

  1. Connect to the ECS instance. For more information, see Connection guide for ECS instances.

  2. Configure the cookie based on your web server.

    Note

    Cookie configuration varies by web server. The following examples cover common web servers. If your web server is not listed, see its official documentation for the correct configuration.

    Nginx

    This section provides an example of how to configure an NGINX 1.20.1 server that runs on CentOS 7.9. The configuration may vary based on your environment.

    1. Modify and save the Nginx configuration file. For the required modifications, see the instructions below. Run the nginx -t command to view the path of the configuration file. The configuration file is typically nginx.conf in the /etc/nginx directory, but the actual path may vary depending on your environment.

      http {
        # ...
        server {
          listen       80;
          # Set BACKEND_SERVER to the cookie name that you specified when you configured the rewrite cookie method. You can use a custom string for the value.
          add_header Set-Cookie "BACKEND_SERVER=value";
          # ...
        }
      }
      
    2. Run the following command to reload the NGINX configuration file:

      sudo nginx -s reload

    Apache

    This section provides an example of how to configure an Apache 2.4.6 server that runs on CentOS 7.9. The configuration may vary based on your environment.

    1. Modify and save the Apache service configuration file. For the required changes, see the instructions below. The default path is typically /etc/httpd/conf/httpd.conf, but the actual path depends on your environment.

      # ...
      Listen 80
      # Set BACKEND_SERVER to the cookie name that you specified when you configured the rewrite cookie method. You can use a custom string for the value.
      Header always set Set-Cookie "BACKEND_SERVER=value"
      # ...
    2. Run the following command to reload the Apache configuration file and apply the changes:

       sudo systemctl reload httpd.service
  3. Repeat the preceding steps to modify the configurations of the other backend servers in the server group.

Step 3: Verify session persistence

  1. Log on to the ALB console.

  2. In the top navigation bar, select a region. Find the target ALB instance and copy its domain name.

  3. In a browser, enter the domain name. A page from one of your servers is displayed. Refresh the page multiple times. The page content should remain the same.

    For example, if your first request is routed to ECS01, all subsequent requests after refreshing the page are also routed to ECS01.

    If responses alternate between ECS01 and ECS02 after multiple refreshes, session persistence is not working correctly. Check your configuration for errors and test again.

Related documents