Sticky sessions pinned our heaviest customers to one node at 95% CPU while others idled
One of six app nodes kept running hot while the rest sat at 20 percent, and adding nodes did nothing. Cookie-based session affinity had pinned a few very heavy accounts to it. Why affinity defeats load balancing, how we moved sessions out of the nodes, and why we switched to least-outstanding-requests.
Our B2B dashboard runs on six application nodes behind HAProxy. For weeks, one node or another kept running at over 90 percent CPU during working hours while the others sat at 15 to 25 percent. Response times for some customers were terrible and for others fine. The obvious fix, adding two more nodes, changed nothing: the hot node stayed hot, and the new nodes idled.
Per-node metrics at 11:00 on a weekday:
node cpu active_sessions req/s p95 ms
app-1 93% 1,912 610 2,840
app-2 21% 2,004 140 180
app-3 18% 1,987 128 160
app-4 24% 2,051 152 190
app-5 19% 1,962 133 170
app-6 22% 2,010 147 185
app-7 6% 211 14 90 (new)
app-8 5% 198 12 85 (new)
Sessions were balanced across the original six. Requests and CPU were not. app-1 served four times the
requests of its neighbours with the same number of sessions.
The configuration
backend app
balance roundrobin
cookie SERVERID insert indirect nocache
server app-1 10.0.2.11:8080 check cookie a1
server app-2 10.0.2.12:8080 check cookie a2
...
cookie SERVERID insert is session affinity, also called sticky sessions. On a user's first request,
HAProxy picks a server by round robin and sets a SERVERID cookie naming it. Every later request with that
cookie goes to the same server, for as long as the browser keeps the cookie.
Who was on app-1
We grouped requests by account for each node. On app-1, three accounts produced 68 percent of the traffic.
They were large customers whose operations teams kept the live dashboard open on wall screens, with
auto-refresh every five seconds, plus automated exports. Each wall screen was one session making a request
every few seconds, all day. By chance, several of those sessions had landed on app-1 when they were first
created, and stayed there for weeks.
Affinity balances sessions, not load
Round robin with stickiness balances new sessions evenly. Once a session is assigned, its future load goes to that node regardless of how busy the node is. If sessions have similar weight, that works out. When weights differ by orders of magnitude, as ours did (a wall screen versus a person checking once a day), the distribution of load depends on which node the heavy sessions happened to land on.
Adding nodes cannot help. New nodes only receive new sessions. The heavy sessions already pinned to
app-1 stay there until their cookie expires or the browser is restarted, and wall screens rarely restart.
Why we had stickiness at all
The application stored user sessions in local memory on each node. Stickiness was not a performance choice; it was required for correctness, because a request routed to a different node would find no session and log the user out. It had been set up years earlier, when the app ran on two servers.
That is the usual root cause: sticky sessions exist because state lives in the application node. They also make deploys and scale-in painful: draining a node logs out its users or forces you to wait for sessions to expire.
Environment
| Item | Value |
|---|---|
| Load balancer | HAProxy 2.8, cookie-based affinity |
| App | Java (Spring Boot), in-memory HTTP sessions |
| Nodes | 6, then 8 |
| Skew | top 3 accounts produced 68% of the hot node's requests |
1. Move session state out of the nodes
We switched the application to store sessions in Redis using Spring Session:
spring:
session:
store-type: redis
timeout: 8h
data:
redis:
host: sessions.internal
port: 6379
Any node can now serve any request for any session. We kept sessions small (user ID, tenant, a few preferences) and moved cached data that had crept into the session back into a proper cache.
Before switching, we measured the extra latency: one Redis read per request, about 0.4 ms at p99 on the same network, which was negligible next to our request times.
2. Remove affinity and pick a better balancing algorithm
With stateless nodes, we removed the cookie and changed the algorithm:
backend app
balance leastconn
server app-1 10.0.2.11:8080 check maxconn 200
server app-2 10.0.2.12:8080 check maxconn 200
...
leastconn sends each new connection to the server with the fewest active connections. Our dashboard
requests vary a lot in cost (a simple widget refresh versus a large export), so a server busy with slow
requests has more open connections and receives fewer new ones. Round robin, by contrast, would keep handing
a busy server its share regardless.
maxconn per server caps concurrent requests and queues the rest at HAProxy, so a temporarily slow server
cannot be overloaded.
On cloud load balancers the equivalent is usually called "least outstanding requests" (AWS ALB) or similar; the idea is the same.
3. Deal with the heavy clients themselves
Even with perfect balancing, wall screens refreshing every five seconds were wasteful. We changed the live dashboard to receive updates over a server-sent events stream instead of polling, and asked the export users to move to the scheduled export API. Total request volume from those three accounts fell by about 80 percent.
4. Migrate without logging everyone out
Switching from in-memory to Redis sessions would normally log out every user. We deployed the change with both stores active for a day: the app read the session from Redis first, then from memory, and wrote to Redis. HAProxy stickiness stayed on during that day, so in-memory sessions were still found. After 24 hours, almost every active session existed in Redis, and we removed the cookie and the in-memory fallback.
1. Distribution. The same hour of a weekday after the change, on six nodes (we removed the two extra):
node cpu req/s p95 ms
app-1 36% 228 210
app-2 34% 221 205
app-3 35% 226 200
app-4 37% 231 215
app-5 33% 219 198
app-6 35% 224 207
2. Hot client test. In staging, we pointed 20 simulated wall screens (5 second refresh) and a heavy export
loop at the dashboard. With affinity, whichever node they landed on saturated. With leastconn and shared
sessions, their load spread across all nodes and no node exceeded 50 percent CPU.
3. Node loss. We killed a node during the test. Its users' next requests went to other nodes, still logged in, with no visible interruption beyond in-flight requests. With in-memory sessions, everyone on that node had been logged out.
4. Deploys. Rolling deploys no longer need a long connection drain to avoid logouts, which shortened them from about 25 minutes to 6.
5. Security implications. Moving sessions to Redis means session data now crosses the network and lives in a shared store. The Redis instance is reachable only from the app subnet, requires authentication and TLS, and session IDs are still random 128-bit values. Sessions are also now revocable centrally, which we use for "sign out everywhere".
Takeaways
- Sticky sessions balance new sessions, not load. A few heavy sessions can pin a node at capacity.
- Adding nodes does not help, because existing sessions stay where they are.
- Remove the need for affinity by moving session state into a shared store.
- With stateless nodes, use least-connections or least-outstanding-requests, and cap concurrency per server.
References: HAProxy configuration manual, balance and cookie, Spring Session with Redis, AWS ALB routing algorithms.
Add a label
Embed this post
<iframe src="https://hibug.me/sticky-sessions-uneven-load" width="700" height="420" frameborder="0" loading="lazy"></iframe>
I am a site reliability engineer on a platform team that runs the edge for dozens of internal services: API gateways, reverse proxies, load balancers and the resilience settings everyone copies from each other. The incidents I learn most from happen when a safety mechanism does the wrong thing. A rate limit that locked out an entire office behind one IP, a circuit breaker that never opened, a liveness probe that restarted every pod during a database blip, two nodes that both decided they were primary. Each post explains what the mechanism was meant to do, why it misfired in our setup, how we changed the configuration and how we drilled the failure afterwards to prove it. I keep the examples close to real configs so you can compare them with your own.
Comments
0No comments yet. Be the first to comment!
Sign In to post a comment.