In distributed Function Compute scenarios, different requests may be routed to different instances, breaking session state continuity. Cookie affinity resolves this by automatically inserting a cookie on the server side. Requests carrying the same cookie value are routed to the same function instance, ensuring session state consistency.
Core configuration
On the function configuration page, go to Advanced Configuration > Isolation and Affinity, turn on the Session Affinity toggle, select Cookie Affinity, configure Concurrent Sessions per Instance and Session Lifecycle, then click Deploy to enable it.
On the first request, the system automatically inserts a cookie in the response header. The client must parse, store, and include this cookie in all subsequent requests to ensure routing to the same instance.
Applicability
-
General limitations: Read General limitations and principles of session affinity before use.
-
Server-side cookie insertion only: On the client’s first visit, Function Compute automatically inserts a cookie via the
Set-Cookieheader in the response. The client must parse and save this cookie and include it in future requests. -
Request limits:
-
A single instance can handle multiple sessions simultaneously (default: 20, maximum: 200). When the session limit is reached, the system automatically creates a new instance.
-
All sessions on an instance share its concurrency quota of 200.
-
-
Session API support: Use the Session API to manage session lifecycle operations such as create, update, query, and terminate.
Configure Cookie affinity
Overview
Configuring Cookie affinity involves three steps: enable session affinity, select Cookie type, and set parameters then deploy. You can configure this when creating a function or update an existing function.
Enable session affinity
-
Log on to the Function Compute console.
-
Go to the function list and select a target function or create a function.
When creating a function, you can directly find the Advanced Settings section and locate the Isolation & Affinity configuration item to complete setup before creation.
-
On the function details page, click the Configuration tab.
-
In the Advanced Settings section, find the Isolation & Affinity configuration item.
-
Click Isolation & Affinity to expand the configuration panel.
-
Turn on the Session affinity toggle.
Select Cookie affinity type
-
In the Session affinity configuration area, select the Cookie Affinity radio button.
-
The system automatically displays Cookie affinity configuration options.
Configure session parameters
Purpose: Set the number of sessions, lifecycle duration, and idle timeout to control session binding and resource usage.
Procedure:
-
Concurrent Sessions per Instance: Set the maximum number of sessions a single instance can handle simultaneously.
-
Default: 20
-
Range: 1–200
-
Recommendation: Use a smaller value (such as 10) for staging environments. Adjust based on business needs in production environments.
-
-
Session Lifecycle: Set the maximum duration from session creation to destruction.
-
Default: 21600 seconds (6 hours)
-
Note: After this time, the system automatically destroys the session and no longer guarantees affinity.
-
-
Session Idle Timeout: Set how long a session remains idle before automatic destruction.
-
Default: 1800 seconds (30 minutes)
-
Note: If an instance receives no requests for this duration, the session becomes idle and is automatically destroyed.
-
-
Click the Deploy button to save the configuration.
Important notes:
-
After enabling session affinity, the system automatically sets Concurrency per Instance to 200 (the system default, which cannot be changed manually).
-
If you configure session affinity during function creation, it applies to the new function. For existing functions, you must deploy the updated configuration separately.
-
The cookie name is auto-generated by the system in the format
x-fc-cookie-session-id. The cookie value is a system-generated GUID. -
The cookie’s
Max-Agedefaults to 21600 seconds (6 hours), matching the session lifecycle.
Verify Cookie affinity
Purpose: Confirm that requests with the same cookie value route to the same instance and that the system automatically scales out when needed.
-
Requests with the same cookie value route to the same instance.
-
When the session limit per instance is reached, the system automatically creates a new instance.
-
All requests return successfully (status code 200).
You can observe instance scaling changes on the function details page in the console.
Procedure:
-
Prepare the test environment
-
Ensure the function is deployed with Cookie affinity enabled (set Concurrent Sessions per Instance to 2 for easier testing).
-
Get the HTTP trigger URL for the function.
-
To simplify testing, change the HTTP trigger authentication from signed to anonymous:
-
On the function details page, click the Triggers tab.
-
Click the Edit button for the trigger.
-
Set Authentication to Anonymous Access.
-
Save the configuration.
-
-
-
First request: No cookie included—system inserts one automatically
-
Send the first request:
curl -i http://your-function-url/You can get a temporary verification URL by clicking the HTTP trigger on the function details page.
Example:
curl -i https://test-cookies-b*******v.cn-hangzhou.fcapp.run -
Expected result:
Content-Length: 13 Content-Type: text/html; charset=utf-8 Set-Cookie: x-fc-cookie-session-id=3******a-c**5-4**0-a**4-e**********4; Max-Age=21600 X-Fc-Request-Id: 1-******e8-******c9-e**********8 Date: Tue, 30 Dec 2025 11:47:53 GMT Hello, World!% -
Extract the cookie value (
x-fc-cookie-session-id) from theSet-Cookieheader. -
Record the
X-Fc-Request-Id(useful for analyzing request routing).
-
-
Second request: Use the extracted cookie value
-
Verify that the same session routes to the same instance. Replace
<first-x-fc-cookie-session-id>with theSet-Cookievalue ofx-fc-cookie-session-idfrom the previous step.curl -i -b "x-fc-cookie-session-id=<first-x-fc-cookie-session-id>" https://your-function-url/Example:
curl -i -b "x-fc-cookie-session-id=3******a-c**5-4**0-a**4-e**********4" https://test-cookies-b********v.cn-hangzhou.fcapp.run -
Expected result:
HTTP/1.1 200 OK Content-Length: 13 Content-Type: text/html; charset=utf-8 X-Fc-Request-Id: 1-******cb-******16-e**********2 Date: Tue, 30 Dec 2025 12:12:59 GMT Hello, World!% -
Affinity verification: On the function details page in the console, go to Instances > Instance Logs. You will see both requests handled by the same instance (confirmed by matching
X-Fc-Request-Idvalues).The second request includes the cookie from the first request. The log output from the same instance appears as follows.
2025-12-30 20:19:41 FC Invoke Start RequestId: 1-695xxx08d1309db 2025-12-30 20:19:41 FC Invoke End RequestId: 1-69xxx1309db 2025-12-30 20:19:42 * Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on hxxx * Running on hxxx Press CTRL+C to quit Path: Data: b'' 21.0.0.1 - - [30/Dec/2025 12:19:41] "GET / HTTP/1.1" 200 - 2025-12-30 20:22:18 FC Invoke Start RequestId: 1-695xxxa15fef9d 2025-12-30 20:22:18 FC Invoke End RequestId: 1-695xxxa15fef9d 2025-12-30 20:22:19 Path: Data: b'' xxx - - [30/Dec/2025 12:22:18] "GET / HTTP/1.1" 200 -
-
-
Third request: No cookie included—system generates a new one
-
Send a new request:
curl -i https://your-function-url/Example:
curl -i http://test-cookies-o********j.cn-hangzhou.fcapp.run/ -
Expected result: The system generates a different cookie value for the new session.
Access-Control-Expose-Headers: Date,x-fc-request-id Content-Disposition: attachment Content-Length: 13 Content-Type: text/html; charset=utf-8 Set-Cookie: x-fc-cookie-session-id=d******e-***3-4**5-a**2-3**********d; Max-Age=21600 X-Fc-Request-Id: 1-******cb-******16-1**********a Date: Tue, 30 Dec 2025 12:31:00 GMT Hello, World!% -
Affinity verification: If Concurrent Sessions per Instance is set to 2, the new session should bind to the same instance. On the function details page, go to Instances > Instance Logs to confirm (using
X-Fc-Request-Id).Log verification result (instance type: 0.35 vCPU / 512 MB memory, function: test-cookies, custom runtime: Debian10): The first two requests used the same cookie value. The third used a new one, confirming session affinity works.
2025-12-30 20:26:41 * Serving Flask app 'app' * Debug mode: off WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead. * Running on all addresses (0.0.0.0) * Running on xxx * Running on xxx Press CTRL+C to quit 2025-12-30 20:31:00 FC Invoke Start RequestId: 1-6xxx60a # Same cookie value 2025-12-30 20:31:00 FC Invoke End RequestId: 1-6xxx1760a 2025-12-30 20:31:01 Path: Data: b'' 2xxx -- [30/Dec/2025 12:31:00] "GET / HTTP/1.1" 200 – 2025-12-30 20:33:03 FC Invoke Start RequestId: 1-6xxxe69 # Same cookie value 2025-12-30 20:33:03 FC Invoke End RequestId: 1-6xxxe69 2025-12-30 20:33:04 Path: Data: b'' 2xxx -- [30/Dec/2025 12:33:03] "GET / HTTP/1.1" 200 – 2025-12-30 20:33:48 FC Invoke Start RequestId: 1-6xxx385d # New cookie value 2025-12-30 20:33:48 FC Invoke End RequestId: 1-6xxx385d 2025-12-30 20:33:49 Path: Data: b'' 2xxx -- [30/Dec/2025 12:33:48] "GET / HTTP/1.1" 200 –
-
-
Fourth request: No cookie included—verify automatic instance scaling
-
Send a request without a cookie to generate a new cookie value. When the session limit per instance is reached, the new session binds to a newly created instance:
curl -i http://your-function-url/Example:
curl -i http://test-cookies-o********j.cn-hangzhou.fcapp.run/ -
Expected result:
HTTP/1.1 200 OK Access-Control-Expose-Headers: Date,x-fc-request-id Content-Disposition: attachment Content-Length: 13 Content-Type: text/html; charset=utf-8 X-Fc-Request-Id: 1-6******c-1******2-**********5d Date: Tue, 30 Dec 2025 12:33:48 GMT Hello, World!% -
Affinity verification: On the function details page, click Instances. You will see a new instance added.
The instance list shows two instances with status running and version LATEST, confirming successful scaling for affinity validation.
-
FAQ
Why isn’t Cookie affinity working?
Possible causes:
-
The client failed to correctly parse the session ID from the Set-Cookie header in the first response or did not include it in subsequent requests.
-
The session was destroyed because it exceeded its lifecycle or idle timeout.
-
The function was not deployed with session affinity properly configured.
Troubleshooting steps:
-
Check whether the first response header contains a
Set-Cookiefield with a valid session ID. -
Verify that subsequent requests include the Cookie header.
-
Confirm that session affinity is enabled and deployed in the function configuration.