Session Affinity
This page describes how to enable session affinity for CloudRun services.
By enabling session affinity, CloudRun strives to route requests carrying specific request headers to the same CloudRun instance. Session affinity requires collaboration between the client and server to implement.
Configuring Session Affinity
. By enabling session affinity, CloudRun strives to route requests carrying specific request headers to the same CloudRun instance. Session affinity requires collaboration between the client and server to implement.
General Client
For general clients, after the first call to the CloudRun service, the client needs to save the X-Cloudbase-Session-Id response header returned by CloudRun. In subsequent requests, the client must carry the X-Cloudbase-Session-Id request header with the same value to route to the same CloudRun instance. By default, the X-Cloudbase-Session-Id remains valid for 4 hours. Active X-Cloudbase-Session-Id headers will stay valid and will not be automatically cleared.
The following diagram illustrates the invocation:

MCP Client
CloudRun session affinity has built-in support for MCP Session as declared by the Model Context Protocol. For compliant MCP clients, CloudRun automatically handles session affinity maintenance for Mcp-Session-Id without requiring additional request headers.
The following diagram illustrates the invocation:
