Sticky Sessions (Session Affinity)
You'll learn how to keep a client "glued" to the same backend server using cookies.
Imagine you're shopping online and add items to your cart. If each request randomly goes to a different server, your cart might appear empty because Server A doesn't know what you told Server B. Sticky sessions solve this by routing all your requests to the same server for the duration of your session.
How Cookie-Based Session Affinity Works
When you enable sticky sessions on an Application Load Balancer, the ALB generates a special cookie (typically named AWSALB) and sends it to the client's browser with the first response. This cookie contains encoded information identifying which target (backend server) handled the request.
On subsequent requests, the client's browser automatically sends this cookie back. The ALB reads the cookie and routes the request to the same target that served the original request, ensuring session continuity.
When to Use Sticky Sessions
Use sticky sessions when:
- Your application stores session data locally on each server (not in a shared database or cache)
- Users need consistent experience during their session
- Migrating legacy applications that weren't designed for distributed architectures
Configuration Essentials
You configure sticky sessions at the target group level, not per listener or rule. You can set:
- Duration: How long the stickiness lasts (1 second to 7 days)
- Cookie name: Use the ALB-generated cookie or specify your own application cookie
The ALB respects the stickiness as long as the target remains healthy. If the target fails a health check, the ALB routes requests to a different healthy target and issues a new cookie.
Key Takeaway: Cookie-based sticky sessions use an ALB-generated cookie to route repeat requests from the same client to the same backend target, maintaining session state for applications that store data locally.