You scale your application from one node to four behind a round-robin load balancer. Within an hour, users complain that they’re being logged out at random, shopping carts are emptying mid-checkout, and a WebSocket-based feature throws disconnect errors. Someone proposes enabling sticky sessions on the LB. The error rate drops to zero. Everyone celebrates. Two months later you discover that one of the four backends serves 70% of the traffic, autoscaling can’t actually scale because new nodes get no work, and a routine deploy takes one tenant’s users completely offline for fifteen minutes because the LB kept sending them to a draining backend. Sticky sessions fixed the symptom and created three new problems.
What’s happening
Sticky sessions (a.k.a. session affinity) tell the load balancer to send the same client back to the same backend, usually by hashing the client IP, planting a cookie, or honoring a JSESSIONID. There are two legitimate reasons to use it:
- The application stores session state in process memory and there is no shared session store (Redis, database, or signed JWT). Without affinity, every request that lands on a different backend has no session.
- A long-lived connection is in flight (WebSocket, gRPC stream, HTTP/2 server-push) and breaking it on every reconnect would be expensive. Affinity at connect time is fine; mid-stream you don’t have a choice anyway.
There is one illegitimate but extremely common reason: the cluster has hidden state and nobody noticed. A queue lives in process memory. A “cache” is actually authoritative. A background worker on one node holds a lock the others don’t know about. Affinity makes the bugs invisible because each user always hits the same instance and the divergence between instances stops mattering — until you deploy, autoscale, or a node fails. Then the bug surfaces violently.
Affinity also breaks load balancing arithmetic. With IP-hash affinity and a tenant whose office NATs to one egress IP, every user from that tenant lands on one backend. A 1000-user tenant on a 4-node cluster gives you 250 users / node in the best case and 1000 users / 1 node + 0 / 3 in the actual case.
How to verify
Three quick checks for whether affinity is masking a bug or solving one:
# 1. Disable affinity in staging and run a synthetic load test
ab -n 10000 -c 50 -C "session=test-cookie" https://staging.example.com/api/me
# Watch the error rate. If it's > 0 with affinity off, the app has in-process state.
# 2. Backend distribution under sticky sessions in production
# (Add the backend hostname to a response header at the LB)
curl -sI https://app.example.com/ -H "Cookie: session=alice" | grep -i x-backend
curl -sI https://app.example.com/ -H "Cookie: session=bob" | grep -i x-backend
# Send 1000 distinct cookies and tally. Even distribution = working. 70/15/10/5 = NAT skew.
# 3. What does a deploy actually do to in-flight users?
# Drain a node, then immediately check how many users get a fresh login prompt:
curl -s https://app.example.com/api/session-count?node=web03
If error rates spike when you remove affinity, the application has implicit state that needs to be externalized. If backends are skewed under affinity, your affinity key is the problem. If a drain logs users out, the session store is local and the affinity is hiding it.
The fix
The order matters. Don’t disable affinity until you’ve fixed the underlying state:
- Externalize session storage. Move sessions to Redis (or a signed JWT for stateless auth, or a database table for low-volume cases). With sessions in Redis, any backend can serve any request and a node going away is invisible to users.
- Find the implicit state. Audit the codebase for module-level mutable state, in-memory queues, in-memory rate limiters, in-memory feature-flag caches. Move them to a shared store or document them as deliberately per-instance with a TTL.
- If you still need affinity (WebSocket-heavy app, gRPC streaming), use cookie-based affinity, not IP-hash. A signed cookie planted at first connect spreads load evenly even when a tenant NATs through one IP, and it survives client IP changes (mobile networks, VPN reconnects).
- Set the affinity TTL to the session length, not “forever”. A 30-day affinity cookie pins a user to a backend across deploys, restarts, and autoscale events for a month. A 4-hour cookie expires within a business day.
- Drain on deploy. Sticky sessions are only safe across deploys if the LB respects connection draining and the affinity cookie respects backend health. Test it: a healthy node going into “drain” should stop accepting new affinity bindings but keep serving existing ones until they expire or disconnect.
The honest summary: sticky sessions are a routing pattern, not a fix for missing state externalization. If your only knob to keep the cluster working is the affinity setting, your cluster is not horizontally scalable — it’s four single-node deployments wearing a trench coat.
Stack Harbor operates clustered web tiers under environment management, including session externalization and LB tuning for B2B SaaS workloads. If your cluster doesn’t survive a node drain, book a quick architecture review.