πŸͺ 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)
Back to blog

Leave a comment