Sticky sessions (also called session affinity) is a load balancing feature where a userβs requests are always routed to the same backend server during a session.
Why Sticky Sessions Are Needed
In distributed systems, multiple backend servers handle requests. But some applications store session data locally in memory.
Problem Without Sticky Sessions
User Request-1 β Server A (login session stored)
User Request-2 β Server B (no session data)
Result β User gets logged out
This happens because Server B does not know about the session created in Server A.
With Sticky Sessions
User β Server A (all requests)
User β Server A (session maintained)
User β Server A (consistent experience)
How Sticky Sessions Work
1. User sends first request
2. Load balancer assigns a backend server
3. Load balancer stores mapping (user β server)
4. All future requests go to same server
Sticky Session Mechanisms
1. Cookie-Based Stickiness
The most common method. Load balancer inserts a cookie in the response.
Example
Set-Cookie: AWSALB=server1
Flow
Client β ALB β Server-1
Client stores cookie
Next request β Server-1 (based on cookie)
Used In
- AWS ALB
- AWS ELB
- Nginx
2. IP-Based Stickiness
Load balancer maps client IP to a server.
Flow
192.168.1.10 β Server A
192.168.1.10 β Server A (always)
Limitations
- Not reliable with NAT
- Mobile users may change IPs
3. Application-Based Stickiness
Application defines session ID and load balancer uses it.
Example
SESSIONID=abc123 β Server A
Sticky Sessions in AWS
AWS Application Load Balancer supports sticky sessions using cookies.
Architecture
Users
|
Application Load Balancer
|
---------------------------------
| | |
EC2-1 EC2-2 EC2-3
|
Sticky Cookie β Same instance
Types of AWS Stickiness
1. Duration-Based Stickiness
Session persists for a fixed time.
Example
Cookie expires in 1 hour
2. Application-Controlled Stickiness
Application sets its own session cookie.
Example
JSESSIONID (Java)
PHPSESSID (PHP)
Advantages of Sticky Sessions
- Simple session handling
- No need for external session store
- Faster access (in-memory session)
Disadvantages of Sticky Sessions
- Uneven load distribution
- Scalability issues
- Risk of server failure losing session
- Harder horizontal scaling
Sticky Sessions vs Stateless Architecture
| Feature | Sticky Sessions | Stateless Architecture |
|---|---|---|
| Session Storage | Server memory | External store (Redis, DB) |
| Scalability | Limited | High |
| Failure Impact | High risk | Low risk |
| Load Distribution | Uneven | Even |
Real-World Use Cases
- E-commerce checkout sessions
- Legacy web applications
- Session-based authentication systems
Sticky Sessions in Microservices
Modern microservices generally avoid sticky sessions because they prefer:
- Stateless design
- JWT authentication
- Redis session storage
Architecture Evolution
Old Model:
Sticky Session β Server Memory
Modern Model:
Stateless β JWT / Redis / DB
When NOT to Use Sticky Sessions
- Microservices architecture
- High-scale distributed systems
- Cloud-native applications
Best Practices
- Prefer stateless services
- Use Redis for session storage
- Use sticky sessions only when required
- Enable health checks in load balancer
Interview Answer
Sticky sessions (session affinity) is a load balancing technique where a userβs requests are routed to the same backend server for the entire session.
It is implemented using:
- Cookies
- IP-based routing
- Session IDs
While it simplifies session management, it reduces scalability and fault tolerance, so modern systems prefer stateless design.
Quick Summary Table
| Concept | Meaning |
|---|---|
| Sticky Session | Same user β same server |
| Purpose | Maintain session consistency |
| Best For | Legacy apps |
| Modern Approach | Stateless + Redis/JWT |
Final Conclusion
Sticky sessions are useful for maintaining session continuity, but they introduce limitations in scalability and fault tolerance.
Modern cloud architectures prefer stateless systems over sticky sessions for better performance and resilience.