πͺ What are Sticky Sessions?
πͺ What are Sticky Sessions?
Sticky Sessions, also known as Session Affinity, ensure that a user is always routed to the same backend server for the duration of their session.
β This is useful when your application stores session data locally on the instance, rather than in a centralized store like Redis or a database.
π¦ How Sticky Sessions Work in AWS
AWS enables sticky sessions using a cookie:
πΉ Application Load Balancer (ALB):
- Uses the
AWSALB
cookie (default) or a custom app cookie. - Sticky sessions can be configured per target group.
πΉ Classic Load Balancer (CLB):
- Uses the
AWSELB
cookie. - Supports both duration-based and application-controlled stickiness.
β Sticky sessions are not supported in Network Load Balancer (NLB).
π§ Use Case Example
Letβs say a user logs in to a shopping site. Their cart is stored in memory on EC2 instance A. With sticky sessions enabled:
- Their first request hits instance A
- All subsequent requests are also sent to instance A, until the session expires
Without sticky sessions:
-
Load balancer may send requests to different EC2 instances, and the cart data might be lost if not stored centrally.
βοΈ Configuration (via AWS Console):
For ALB:
- Go to the Target Group settings
- Under Attributes, enable Stickiness
- Choose:
-
Duration-based
(set time in seconds) -
App-based
cookie name (optional)
-
π§ Pros and Cons
β Pros:
- Simple way to preserve session data
- Useful for legacy or stateful applications
- No need for external session storage
β Cons:
- Breaks load distribution (one server might get overloaded)
- Doesnβt work well with auto-scaling unless sessions are replicated
- Not recommended for stateless or microservice-based architectures
β Summary Table
Feature | Description |
---|---|
Purpose | Keep user connected to the same backend instance |
Cookie Used (ALB) |
AWSALB (default) or app-defined cookie |
Cookie Used (CLB) | AWSELB |
Duration | Configurable (seconds) |
Use Case | Shopping carts, login sessions, chat apps (stateful) |
Not supported in | Network Load Balancer (NLB) |