By default, a Classic Load Balancer routes each request independently to the registered instance with the smallest load. However, you can use the sticky session feature (also known as session affinity), which enables the load balancer to bind a user’s session to a specific instance. This ensures that all requests from the user during the session are sent to the same instance.
Stickiness can be setup thru console or CLI
Elastic Load Balancing creates a cookie, named AWSELB, that is used to map the session to the instance.
Duration based session stickiness:
On the Edit stickiness page, select Enable load balancer generated cookie stickiness.
(Optional) For Expiration Period type the cookie expiration period, in seconds. If you do not specify an expiration period, the sticky session lasts for the duration of the browser session.
If there is no cookie (first request), the load balancer chooses an instance based on the existing load balancing algorithm (least loaded EC2). A cookie is inserted into the response for binding subsequent requests from the same user to that instance. After a cookie expires (after duration from above step), the session is no longer sticky.
Application-Controlled session stickiness:
The load balancer uses a special cookie to associate the session with the instance that handled the initial request, but follows the lifetime of the application cookie specified in the policy configuration.
The load balancer only inserts a new stickiness cookie if the application response includes a new application cookie.
The load balancer stickiness cookie does not update with each request.
If the application cookie is explicitly removed or expires, the session stops being sticky until a new application cookie is issued.
If an instance fails or becomes unhealthy, the load balancer stops routing requests to that instance, and chooses a new healthy instance based on the existing load balancing algorithm.
The load balancer treats the session as now “stuck” to the new healthy instance, and continues routing requests to that instance even if the failed instance comes back.
On the Edit stickiness page, select Enable application generated cookie stickiness.
For Cookie Name, type the name of your application cookie.
ELB can distribute load across EC2 instances running across AZs within a region only.