1️⃣ What is a Sticky Session?
A sticky session (also called session affinity) is a load balancing technique where all requests from a particular client are routed to the same server for the duration of a session.
- Ensures that a user’s session data is consistently available on the same server.
- Common in web applications with stateful sessions.
Goal: Keep the session consistent and avoid losing user-specific data.
2️⃣ How Sticky Sessions Work
- Client makes a request → hits load balancer
- Load balancer routes request to a server (e.g., Server A)
- Load balancer remembers that client X → Server A (usually via cookie or IP)
- All subsequent requests from the same client are routed to Server A
- Session is preserved without requiring shared storage
Mechanisms to Implement Sticky Sessions
| Method | Description |
|---|---|
| Cookie-based | Load balancer sets a cookie identifying the server (e.g., SERVERID=A) |
| IP-based | Routes requests based on client IP (less reliable for dynamic IPs) |
| Session token | Token in the URL or header used to identify the server |
3️⃣ Example
- User logs in → session is created on Server 1
- User navigates multiple pages → all requests go to Server 1
- Without sticky session → requests may go to Server 2 → session lost, user logged out
4️⃣ Pros and Cons
Pros
- Simple to implement
- No need for shared session storage
- Useful for stateful applications
Cons
- Poor load balancing if some servers get more sticky sessions than others
- Server failure → session lost unless replicated
- Not ideal for stateless, scalable architectures
5️⃣ Sticky Sessions vs Stateless Architecture
| Feature | Sticky Session | Stateless |
|---|---|---|
| Session storage | Stored on specific server | Stored in DB / cache (Redis) |
| Scalability | Limited (depends on server load) | Highly scalable |
| Fault tolerance | Low (server failure loses session) | High (any server can handle request) |
6️⃣ Quick Analogy
-
Sticky session = Favorite waiter in a restaurant
- You always go to the same waiter (server) → knows your preferences (session data)
-
Stateless = Any waiter can serve you because your order (session) is in a central system