Cloud

Azure Container Apps: validate session affinity before masking local application state

A production runbook for qualifying Azure Container Apps session affinity, measuring reliance on local state, and choosing a temporary control, state externalization or rollback.

28 Oct 2026 azurecontainer-appssession-affinitysticky-sessionsingressrevisionsstateobservabilitycanaryrunbookrollbackproduction

A web application migrated to Azure Container Apps signs some users out after a scale-out. One request creates a session on a replica; the next reaches another replica that does not have that state. The team proposes enabling session affinity to restore the user journey quickly.

That setting can reduce the symptom, but it does not make state durable. Azure Container Apps enforces affinity with a cookie on HTTP ingress, and only in single revision mode. If the selected replica becomes unavailable, the client can be routed elsewhere. Uneven routing can also concentrate active sessions on one replica while others remain lightly used.

The running case is an application that keeps secondary authentication context, cart data and workflow progress in memory. This runbook drives one explicit decision: keep affinity disabled, enable it as an expiring control, externalize state before proceeding, or restore the previous ingress configuration.

Freeze the contract before changing ingress

Export the effective application configuration, revisions, replica count and ingress state. Also record the deployed image and the UTC time of the last known-good journey. Affinity is an application-scope ingress property; do not confuse its change with a new container version.

bash 01-freeze-container-app.sh
RG="rg-checkout-prod"
APP="checkout-web-prod"

az containerapp show --resource-group "$RG" --name "$APP" --output json > container-app-before.json

az containerapp show --resource-group "$RG" --name "$APP" --query '{mode:properties.configuration.activeRevisionsMode,ingress:properties.configuration.ingress,scale:properties.template.scale,containers:properties.template.containers[].{name:name,image:image}}' --output yaml

az containerapp revision list --resource-group "$RG" --name "$APP" --query '[].{name:name,active:properties.active,replicas:properties.replicas,health:properties.healthState,created:properties.createdTime}' --output table

Write down the contract that must survive: which fields form the session, how long they live, which component writes them, which component reads them, and what losing them does. Do not treat a rebuildable cache, an authentication token, a cart and an in-flight order as the same class of state.

yaml session-contract.yml
journey: checkout
state:
authentication_context:
  location: process_memory
  loss_effect: force_reauthentication
cart:
  location: process_memory
  loss_effect: visible_data_loss
order_commit:
  location: database
  idempotency_key: checkout_attempt_id
session_ttl: 30m
allowed_temporary_control: sticky_cookie
control_expiry: 2026-11-04T09:00:00Z
rollback: restore container-app-before.json ingress settings

Prove that local state is the cause

A sign-out after scale-out can also come from a different encryption key, an invalidly sealed cookie, the wrong domain, an expired TTL or an identity backend. Temporarily add a request ID, a nonreversible session hash, revision name, replica name and state-read result to logs. Never log the cookie, token or cart contents.

A useful test runs the same journey with several isolated clients:

  1. A new client sends several requests without a load change.
  2. The same client continues through a controlled scale-out.
  3. A second client starts after the new replica appears.
  4. One replica is removed only after business writes are proven idempotent.

The required evidence is more than “the session disappeared.” Show that the next request reached another replica, that the expected state existed only in the first replica’s memory, and that cookie validation did not fail independently.

kusto 02-session-path-by-replica.kql
let Window = 2h;
ContainerAppConsoleLogs_CL
| where TimeGenerated > ago(Window)
| where ContainerAppName_s == "checkout-web-prod"
| where Log_s has "session_probe"
| parse Log_s with * "trace=" TraceId " session_hash=" SessionHash " state=" StateResult " request=" RequestResult
| project TimeGenerated, RevisionName_s, ReplicaName_s, TraceId, SessionHash, StateResult, RequestResult
| order by SessionHash asc, TimeGenerated asc

Adapt the table and columns to the configured log destination mode. If a session stays on one replica and still fails, affinity does not address the observed fault.

Verify that affinity is actually eligible

Native session affinity requires HTTP ingress and single revision mode. It is not the mechanism for a multi-revision canary. If the application uses several active revisions with traffic weights, the decision must address deployment strategy and state placement rather than an ingress toggle.

Check every intermediary in front of Container Apps as well. A browser must accept and return the cookie; a synthetic client must retain its cookie jar; a proxy or cache layer must not drop Set-Cookie or Cookie headers. Do not use one load-test client to judge distribution: its cookie can intentionally pin it to one replica and hide every other replica.

json 03-sticky-session-candidate.json
{
"activeRevisionsMode": "Single",
"ingress": {
  "external": true,
  "transport": "auto",
  "stickySessions": {
    "affinity": "sticky"
  }
}
}

This fragment identifies the property under test; it is not a complete resource to deploy. Produce the change through the existing IaC path and review the ARM diff before applying it.

Canary without confusing stability with durability

Enable the candidate in a representative environment or for a bounded production window. Use multiple client cohorts, each with its own cookie jar. Record the serving replica, session loss, latency and business result for every cohort.

Test four transitions:

  • repeated requests while the selected replica remains healthy;
  • addition of a replica under load;
  • controlled removal of the replica holding a session;
  • restart or replacement of that replica.

The positive test proves that one client remains on one replica while it is available. The failure test shows what happens when it is not. If the journey loses state after replacement, affinity is behaving as designed while the architecture remains dependent on local memory.

Measure distribution by replica too. A healthy global average can hide one hot replica holding the most active clients. Compare requests, latency, CPU, memory, active sessions and errors per replica. Define the acceptable skew and stop condition before the test starts.

Prepare for multiple revisions without breaking sessions

A weighted progressive deployment requires several active revisions, while native affinity is limited to single revision mode. Before switching to multiple mode, move required state to a shared store or make it reconstructible from a durable source. Keep only a bounded opaque identifier on the client.

Then validate a candidate revision through its label and dedicated FQDN. That proves the image, probes, dependencies and session schema without immediately changing distribution on the application URL. It does not replace a cross-version shared-state test.

Replay a session created by the active version and read by the candidate, then reverse the direction when rollback can return traffic to the older version. Formats must remain compatible both ways throughout coexistence. A destructive session migration turns an application rollback into another broad sign-out event.

Decide, validate or roll back

Keep affinity disabled when the journey is already stateless, the cookie is lost upstream, or the failure remains on one replica. Repair the underlying cause instead of adding a routing constraint.

Enable it temporarily when the mode is Single, ingress is HTTP, several clients prove the benefit, and distribution remains healthy. Assign an owner, expiry and state-externalization task. Keep the temporary control visible in IaC.

Externalize before proceeding when replica loss removes business state, several revisions must coexist, or affinity creates unacceptable load skew. Validate reads, writes, TTL, encryption, concurrency and shared-store fallback behavior.

Roll back when clients do not remain stable, errors persist, a replica becomes hot, or latency crosses the bound. Restore the previous ingress configuration, verify revision mode, and rerun the positive and negative cohorts. Rollback is complete when distribution and the user journey return to baseline, not merely when affinity reads none again.

Conclusion

Azure Container Apps session affinity can stabilize a client on one replica; it cannot turn that replica’s memory into durable storage. Qualification requires proof of the cookie path, state location, replica-replacement behavior and load balance.

The runbook ends with a clear state: no affinity when it does not treat the cause, an expiring control in single mode, externalization before multiple revisions, or ingress rollback. The goal is not to make a session sticky. It is to know which state can disappear without turning a scale-out or deployment into an incident.