Summary: Sticky Sessions in AWS Elastic Load Balancing
Sticky sessions, also known as session affinity, allow client requests to be consistently routed to the same backend instance behind a load balancer. This ensures that a user's session data is preserved, which is particularly useful for maintaining user login states and other important information.
Key Points:
- Purpose of Sticky Sessions: To ensure a client's requests are handled by the same backend instance to maintain session data.
- Supported Load Balancers: Sticky sessions can be enabled on Classic Load Balancer (CLB), Application Load Balancer (ALB), and Network Load Balancer (NLB).
- Mechanism: Sticky sessions work by using a special cookie with an expiration date that is sent from the client to the load balancer.
- Cookie Types:
- Application-based Cookie: Custom cookie generated by the target application with specific attributes.
- Duration-based Cookie: Generated by the load balancer with a specified expiration time.
- Cookie Names:
- Avoid using reserved names such as
AWSALB, AWSALBAPP, and AWSALBTG.
- The ALB uses
AWSALBAPP for application-generated cookies and AWSALB for duration-based cookies.
- The CLB uses
AWSELB for duration-based cookies.
- Configuration:
- Sticky sessions are configured at the target group level.
- Two types of stickiness: load balancer generated (duration-based) or application-based.
- Duration can be set between one second and seven days.
Enabling Sticky Sessions:
- Go to the target group settings.
- Choose "Edit Attributes".
- Select "Stickiness" and configure the type and duration.
- Specify a custom cookie name if using application-based stickiness.
Monitoring Sticky Sessions:
- Use web developer tools to inspect network requests and observe the sticky session cookies being passed back and forth between the client and the load balancer.
Considerations:
- Load Imbalance: Sticky sessions can cause an uneven distribution of load across backend instances.
- Disabling Stickiness: You can revert to normal load balancing behavior by disabling stickiness in the target group attributes.
Debugging: