AgentGateway stores MCP sessions in-memory. When running multiple replicas, requests from the same client must be routed to the same replica to maintain session state.
Sticky sessions (also called session affinity) use HTTP cookies to ensure all requests from a client are routed to the same replica.
- When a client makes their first request, Azure Container Apps routes it to any available replica
- The response includes an
ARRAffinitycookie that identifies the replica - Subsequent requests include this cookie, ensuring they go to the same replica
- If a replica becomes unavailable, the client is routed to a new replica (session will be lost)
Sticky sessions are enabled by default in the Terraform module (terraform/):
# In terraform/terraform.tfvars
environment = "prod"
resource_group_name = "agentgateway-prod-rg"
# Enable sticky sessions for MCP session affinity (default: true)
enable_sticky_sessions = trueThe module automatically runs az containerapp ingress sticky-sessions set after creating the Container App.
Enable manually with:
az containerapp ingress sticky-sessions set \
--name <container-app-name> \
--resource-group <resource-group> \
--affinity stickyVerify configuration:
az containerapp show \
--name <container-app-name> \
--resource-group <resource-group> \
--query "properties.configuration.ingress.stickySessions"- Single Revision Mode: Sticky sessions only work when the Container App is in single revision mode
- HTTP Ingress: The container app must use HTTP ingress (not TCP)
- Cookie Support: Clients must accept and send cookies
- Session Loss on Replica Failure: If a replica goes down, all sessions on that replica are lost
- Uneven Load Distribution: Some replicas may handle more traffic than others
- No Cross-Replica Session Sharing: Sessions exist only on the replica that created them
Verify sticky sessions work with multiple replicas:
# Scale to multiple replicas
az containerapp update \
--name unitone-agw-prod-app \
--resource-group mcp-gateway-prod-rg \
--min-replicas 2 \
--max-replicas 10
# Run E2E tests
source .venv/bin/activate
GATEWAY_URL="https://your-gateway.azurecontainerapps.io" \
python3 tests/e2e_mcp_sse_test.pyIf tests pass with multiple replicas, sticky sessions are working correctly.
Enable sticky sessions when:
- Running multiple replicas for high availability
- Using stateful MCP mode (the default)
- Clients need to make multiple requests within a session
Consider disabling when:
- Running a single replica
- Using stateless MCP mode
- Session persistence is not required
For production deployments requiring:
- Cross-replica session sharing
- Session persistence across restarts
- High availability without session loss
Consider implementing Redis session storage. This requires modifying the agentgateway core code (currently not supported).
-
Verify sticky sessions are enabled:
az containerapp show --name <app-name> -g <rg> \ --query "properties.configuration.ingress.stickySessions"
-
Check that clients are sending the
ARRAffinitycookie -
Verify the app is in single revision mode:
az containerapp show --name <app-name> -g <rg> \ --query "properties.configuration.activeRevisionsMode"
This indicates sticky sessions are not properly configured. Follow the configuration steps above.