Home / OCI

OCITroubleshootingIntermediate

OCI Load Balancer sticky sessions not working: application cookie vs LB cookie

Users get logged out or lose their cart because requests hop between backends. Check the backend set's persistence settings, the cookie and the listener, then fix it with the CLI.

Deepak Bhardwaj8 min read

Applies toOCI Flexible Load Balancer (HTTP/HTTPS listeners), OCI CLI 3.x

Symptom

Users behind an OCI Load Balancer are logged out at random, lose shopping carts or see "invalid session" errors. The application keeps session state in memory on each backend, so every request has to reach the same server. With one backend everything works. With two or more it breaks, or it breaks right after a patching window.

Each request is being sent to a different backend. Either session persistence isn't configured on the backend set, or it is configured in a way that doesn't match what the application and the client actually do.

How OCI Load Balancer stickiness works

A backend set supports two persistence methods. They are mutually exclusive; set only one per backend set.

Method Who creates the cookie When to use it
Application cookie stickiness The backend sends Set-Cookie with a name you configure (for example JSESSIONID), or any cookie if you use * The app already issues a session cookie
Load balancer cookie stickiness The load balancer adds its own cookie (default name X-Oracle-BMC-LBS-Route) The app has no session cookie, or you don't control it

Both need cookies to pass through the load balancer at layer 7. Persistence based on cookies only works when the load balancer operates in HTTP mode, which means an HTTP or HTTPS listener. A TCP listener passes bytes through and can't read or set cookies. The backend set's IP_HASH policy is the only affinity you get there. A Network Load Balancer has no cookie stickiness at all.

Checks

1. Read the backend set configuration

LB_ID=ocid1.loadbalancer.oc1..example
BS=app-backends

oci lb backend-set get --load-balancer-id "$LB_ID" --backend-set-name "$BS" \
  --query 'data.{policy:policy,app:"session-persistence-configuration",lb:"lb-cookie-session-persistence-configuration"}'

If both app and lb are null, no stickiness is configured. Every request goes through the policy (ROUND_ROBIN, LEAST_CONNECTIONS or IP_HASH).

2. Confirm the listener protocol

oci lb load-balancer get --load-balancer-id "$LB_ID" \
  --query 'data.listeners.* | [].{name:name,protocol:protocol,port:port,backendSet:"default-backend-set-name"}' \
  --output table

If the listener for this backend set shows TCP, cookie persistence won't take effect. If you terminate TLS on the backends and need stickiness, change the design: terminate TLS on the load balancer (HTTPS listener) and optionally re-encrypt to the backends.

3. Look at the cookies the client actually gets

curl -sk -c /tmp/jar.txt -o /dev/null -D - https://app.example.com/login | grep -i '^set-cookie'
cat /tmp/jar.txt

Compare what you see with the backend set:

  • Application cookie mode: the configured cookie name must match the backend's Set-Cookie name exactly. JSESSIONID and jsessionid don't match. Some frameworks rename the cookie (SESSION, PHPSESSID, .AspNetCore.Session).
  • LB cookie mode: you should see X-Oracle-BMC-LBS-Route (or your custom name). If it's missing, check that a CDN, WAF or reverse proxy in front of the load balancer isn't stripping cookies.

With the LB cookie, look at the attributes in the response:

  • Secure set but users reach the site over plain HTTP: the browser never sends the cookie back. OCI also refuses to associate a backend set that has a Secure LB cookie with an HTTP listener.
  • Domain set to a different domain than the one users browse: the cookie is dropped.
  • Path narrower than /: requests outside that path aren't sticky.
  • Max-Age too short: sessions break after that many seconds even though the app session is still valid.

5. Prove it with repeated requests

Return the backend hostname from a test endpoint (or read the access logs), then send several requests with and without the cookie jar:

for i in $(seq 1 6); do curl -sk -b /tmp/jar.txt https://app.example.com/whoami; echo; done
for i in $(seq 1 6); do curl -sk https://app.example.com/whoami; echo; done

With the jar, every response should name the same backend. Without it, you should see several backends.

Symptom → cause → fix

Symptom Likely cause Fix
Requests rotate between backends, no LB cookie in the response No persistence configured Configure LB cookie or application cookie persistence
App cookie present, still not sticky Cookie name in the backend set doesn't match the app's cookie Set the exact name, or * to match any cookie
Sticky over HTTPS but not after a redirect from HTTP Secure cookie dropped on the HTTP leg Redirect HTTP to HTTPS at the listener with a rule set, so users only browse over HTTPS
Users get 502 when one backend is down disableFallback is true Set fallback back on, unless the app truly can't survive a move
Persistence ignored TCP listener or Network Load Balancer Use an HTTP/HTTPS listener, or IP_HASH as a weaker substitute
Stickiness breaks only through the corporate proxy or CDN Proxy strips or caches Set-Cookie Exclude the cookie from caching and pass it through

Warning

oci lb backend-set update replaces the whole backend set definition. --backends, --policy and --health-checker-protocol are required. If you pass an empty or partial backend list, the missing backends are removed and traffic drops. Always build the update from the current configuration.

Export the current backends and health checker. CLI output uses kebab-case keys, while the input JSON uses camelCase, so convert the keys:

oci lb backend-set get --load-balancer-id "$LB_ID" --backend-set-name "$BS" > bs-current.json

jq '[.data.backends[] | {ipAddress: ."ip-address", port, weight, backup, drain, offline}]' \
  bs-current.json > backends.json

POLICY=$(jq -r '.data.policy' bs-current.json)
HC_PROTO=$(jq -r '.data."health-checker".protocol' bs-current.json)
HC_PORT=$(jq -r '.data."health-checker".port' bs-current.json)
HC_PATH=$(jq -r '.data."health-checker"."url-path" // "/"' bs-current.json)
HC_CODE=$(jq -r '.data."health-checker"."return-code" // 200' bs-current.json)

Write the LB cookie configuration:

cat > lbcookie.json <<'EOF'
{
  "cookieName": "X-Oracle-OCI-AppRoute",
  "path": "/",
  "isSecure": true,
  "isHttpOnly": true,
  "disableFallback": false
}
EOF

Leave out maxAgeInSeconds to make the cookie last for the browser session. Leave out domain unless you share sessions across subdomains. Oracle recommends an X-Oracle-OCI- prefix for custom names to avoid clashing with application cookies.

Apply it and wait for the work request:

oci lb backend-set update \
  --load-balancer-id "$LB_ID" --backend-set-name "$BS" \
  --backends file://backends.json \
  --policy "$POLICY" \
  --health-checker-protocol "$HC_PROTO" \
  --health-checker-port "$HC_PORT" \
  --health-checker-url-path "$HC_PATH" \
  --health-checker-return-code "$HC_CODE" \
  --lb-cookie-session-persistence-configuration file://lbcookie.json \
  --force --wait-for-state SUCCEEDED --wait-for-state FAILED

If the backend set uses SSL to the backends, health checker timeouts or custom retries, pass those values too. Check bs-current.json and carry over everything you see, because fields you leave out may revert to their defaults. Run oci lb backend-set update --help on your CLI version to confirm the flag list.

If the app already issues JSESSIONID, use the application cookie flags instead of the LB cookie option. Don't set both:

oci lb backend-set update \
  --load-balancer-id "$LB_ID" --backend-set-name "$BS" \
  --backends file://backends.json --policy "$POLICY" \
  --health-checker-protocol "$HC_PROTO" --health-checker-port "$HC_PORT" \
  --health-checker-url-path "$HC_PATH" --health-checker-return-code "$HC_CODE" \
  --session-persistence-cookie-name JSESSIONID \
  --session-persistence-disable-fallback false \
  --force --wait-for-state SUCCEEDED --wait-for-state FAILED

The backend ends the stickiness by sending the same cookie with an expiry date in the past (for example on logout).

Fallback, drain and patching

  • Fallback on (default): if the backend a client is pinned to becomes unavailable, the load balancer sends the request to another healthy backend. The user loses in-memory session state but gets a page.
  • Fallback off: the load balancer returns HTTP 502 until the client stops presenting the cookie.

Caution

With disableFallback true and a long Max-Age, a single failed backend can lock pinned users out for days. Keep fallback on unless losing the session is worse than an error page, and never pair fallback off with a distant expiry.

  • Drain: a backend marked drain keeps serving existing persisted sessions but gets no new ones. That makes drain the right step before patching: set drain, wait for sessions to expire or move, then take the backend offline.

Prevent it next time

  • Put the persistence settings in Terraform (lb_cookie_session_persistence_configuration or session_persistence_configuration on oci_load_balancer_backend_set) so a console edit or rebuild doesn't silently drop them.
  • Add a synthetic check that logs in, calls a /whoami endpoint several times with the same cookie jar and alerts if the backend changes.
  • Move session state out of the app servers (Redis, a database or signed tokens) so stickiness becomes a performance optimization rather than a correctness requirement.
  • Document the cookie name in the runbook for the app. Framework upgrades that rename the session cookie are a common cause.