Summary
- stickiness locks a user session to a single back end instance
- it creates a cookie named AWSALB which the client saves and provides on each subsequent request
- sessions will be lost on cookie expiry or instance termination
- only use if application does not support external session (eg. in a MongoDB accessible to all instances)
Target Group Listener Rules
Each target group is used to route requests to one or more registered targets. When you create each listener rule, you specify a target group and conditions. When a rule condition is met, traffic is forwarded to the corresponding target group. You can create different target groups for different types of requests.
Target Group Health Checks
You define health check settings for your load balancer on a per target group basis. Each target group uses the default health check settings, unless you override them when you create the target group or modify them later on. After you specify a target group in a rule for a listener, the load balancer continually monitors the health of all targets registered with the target group that are in an Availability Zone enabled for the load balancer. The load balancer routes requests to the registered targets that are healthy.
Sticky Sessions Requirements
Sticky sessions are a mechanism to route requests to the same target in a target group. This is useful for servers that maintain state information in order to provide a continuous experience to clients. To use sticky sessions, the clients must support cookies.
Sticky Session Cookie AWSALB
When a load balancer first receives a request from a client, it routes the request to a target, generates a cookie named AWSALB that encodes information about the selected target, encrypts the cookie, and includes the cookie in the response to the client. The client should include the cookie that it receives in subsequent requests to the load balancer. When the load balancer receives a request from a client that contains the cookie, if sticky sessions are enabled for the target group and the request goes to the same target group, the load balancer detects the cookie and routes the request to the same target. If the cookie is present but cannot be decoded, or if it refers to a target that was deregistered or is unhealthy, the load balancer selects a new target and updates the cookie with information about the new target.
CORS Considerations
- With CORS (cross-origin resource sharing) requests, some browsers require
SameSite=None; Secure
to enable stickiness. In this case, Elastic Load Balancing generates a second stickiness cookie, AWSALBCORS, which includes the same information as the original stickiness cookie plus thisSameSite
attribute. Clients receive both cookies.
- If you are using multiple layers of Application Load Balancers, you can enable sticky sessions on one layer only, because the load balancers would use the same cookie name.
- WebSockets connections are inherently sticky. If the client requests a connection upgrade to WebSockets, the target that returns an HTTP 101 status code to accept the connection upgrade is the target used in the WebSockets connection. After the WebSockets upgrade is complete, cookie-based stickiness is not used.
- Application Load Balancers use the Expires attribute in the cookie header instead of the Max-Age header.
- Application Load Balancers do not support cookie values that are URL encoded.
Enabling Sticky Sessions Using the Console
Select Load Balancers > Target Groups > Attributes > Stickiness and set the Stickiness duration between 1 second and 7 days
Enabling Sticky Sessions Using the CLI
Use the modify-target-group-attributes command with the stickiness.enabled
and stickiness.lb_cookie.duration_seconds
attributes.