Session Stickiness and Load Balancing
What You'll Learn
You'll understand why stateful sessions create a challenge when load balancing traffic across multiple servers, and the two primary solutions to this problem.
The Load Balancing Problem with Stateful Sessions
When you store session data in a server's memory (as covered in Session Storage Options), you face a critical issue with load balancers: what happens when a user's second request lands on a different server than their first?
Imagine a shopping mall with multiple information desks. You ask Desk A for directions and they write notes about your visit. If you come back with a follow-up question but get routed to Desk B, they have no memory of your previous conversation—they'd treat you as a new visitor.
This is exactly what happens with in-memory sessions and round-robin load balancing. Server 1 creates a session, but Server 2 knows nothing about it. The user appears logged out.
Two Solutions
Solution 1: Session Stickiness (Sticky Sessions) Configure your load balancer to always route a specific user to the same server. The load balancer uses session cookies or IP addresses to "stick" users to their original server. This works but creates problems:
- Uneven load distribution (one server might get unlucky)
- Session loss if that server crashes
- Complicates deployments and scaling
Solution 2: Shared Session Storage Store sessions in a centralized system (Redis, database) accessible to all servers. Now any server can handle any request because session data lives externally. This is the preferred approach for horizontally scaled systems.
Key Takeaway: Stateful sessions and simple load balancing don't mix—you need either sticky sessions to maintain server affinity or shared session storage to make sessions accessible across all backend servers.