‫Let's talk about sticky sessions,
‫also called session affinity
‫for your Elastic Load Balancer.
‫So it's possible to implement what's called stickiness
‫or sticky sessions,
‫and the idea is that the client
‫doing two requests to the load balancer
‫will have the same instance
‫in the backend to respond to the request.
‫So the idea is that for example,
‫you have the ALB with two EC2 instances,
‫and you have three clients.
‫If the client one makes a request
‫and it goes to the first EC2 instance,
‫that means that when it does a second request
‫to the load balancer,
‫it will go to the same instance.
‫Which is a different behavior
‫where usually the Application Load Balancer
‫will do a spread of all the requests
‫across all the EC2 instances.
‫Now for client two, if it goes to the ALB
‫and talks to the second instance,
‫then all the requests will go there.
‫And same for client three.
‫Okay?
‫So this can be enabled
‫for the Classic Load Balancer, Application Load Balancer
‫and the Network Load Balancer
‫And how it works, well there is a cookie
‫that is sent as part of the request
‫from the client's two load balancer.
‫And it has stickiness and it has an expiration date,
‫that means that when the cookie expires
‫then the client may be redirected to another EC2 instance.
‫Use case for this is to make sure
‫that the user is connected to the same backend instance,
‫in order not to lose his session data.
‫Which can take some important information
‫such as the login of the user, for example.
‫But if you enable stickiness,
‫it may bring imbalance to the load
‫over the backend EC2 instances,
‫in case some instances I have a very, very sticky user.
‫Okay.
‫Now to go a little bit deeper,
‫how about the cookie itself?
‫Well, there are two types of cookie
‫that you can have for sticky sessions.
‫The first one is application-based cookie,
‫and the second one is duration-based cookie.
‫So for application-based cookies,
‫well it's a custom cookie
‫that is generated by the target,
‫so by your application itself.
‫And you can include any custom attributes you want
‫by required by your application.
‫The cookie name must be specified individually
‫for each target group, okay?
‫And you must not use the following name,
‫so AWSALB, AWSALBAPP, OR AWSALBTG,
‫because they are already reserved
‫for used by the ALB itself.
‫Or it could be an application cookie,
‫and this time will be generated by the load balancer itself.
‫And the cookie name used by the ALB will be AWSALBAPP.
‫Okay?
‫Now the second type of cookies is duration-based cookie,
‫and it's a cookie generated by the load balancer.
‫And the name is AWSALB for the ALB,
‫and AWSELB for the CLB.
‫Okay?
‫And the idea is this one will have an expiry
‫based on a specific duration,
‫and the duration is generated
‫by the load balancer itself.
‫Okay?
‫Whereas before, well there
‫is an application-based cookie,
‫so the duration can be specified by the application itself.
‫So that's how it works, okay?
‫You don't need to remember exactly the name of the cookies
‫or the fact that you have custom and application,
‫but you remember there is application-based cookies
‫and duration-based cookies,
‫and they have a specific name,
‫and this will be coming into account
‫when we talk about CloudFront.
‫Okay?
‫So if I look at my load balancer right now,
‫and I open it in the new tab, as you can see
‫it goes between my three instances in my load balancer.
‫So that's perfect.
‫But now I'm going to enable sticky sessions.
‫So to do so, I'm going to go at the target group level,
‫open my target group,
‫and then Action.
‫And I will can edit the attributes of my target group.
‫And in the bottom I have Stickiness or sticky sessions.
‫And as we can see,
‫we have two types of stickiness available to us.
‫Okay?
‫We have the load balancer generated cookie,
‫which is a duration type of stickiness.
‫So I can say between one second and seven days.
‫Or I can have an application-based cookie,
‫between one second to seven days again,
‫but this time I need to specify the cookie name
‫that is sent by the app to the load balancer.
‫So it could be MYCUSTOMAPPCOOKIE.
‫And then this is what the load balancer
‫would be using to perform the stickiness.
‫Okay?
‫So that's it for stickiness.
‫As we can see,
‫if we just have a load balancer generated cookie
‫and we set the stickiness duration to be equal
‫to say one day,
‫and I will save this change.
‫So now let's have a look.
‫So I'm going to go and open the debugger as well
‫so we can have a look at the network
‫and see what happens.
‫So if we have a look at the network,
‫and then I refresh this page,
‫as we can see, I refresh it multiple times.
‫Okay?
‫You get access to the same instance.
‫So 7-176 is the one
‫that's coming back, back, and back, and back.
‫And now what's going to happen is
‫that when you look at the GET request made
‫to the load balancer,
‫and I'm very, very sorry for the font size here,
‫I don't think I can really increase it.
‫But if you go to Cookies, as you can see here,
‫there is a response cookie, okay?
‫That is saying that your cookie expires tomorrow.
‫Here is the path,
‫and here is the value of the cookie.
‫And then in the request cookie,
‫when the browser makes a request to the load balancer
‫it sends again the cookie it has right here.
‫And so because of the cookie being passed and sent,
‫this is how stickiness works.
‫Okay?
‫So just a little bit of a deeper dive
‫into how stickiness works.
‫But that's it for this lecture,
‫I hope you liked it.
‫And by the way, to access the web developer tools,
‫you click on Web Developer
‫and then Web Developer Tools.
‫And I just use a short shortcut for that.
‫And it's the same on Chrome and Firefox.
‫And then you go into Network,
‫and you get access to your information around your request.
‫Okay?
‫And finally, just go back to your target group
‫and then you edit the attributes itself,
‫and you can disable stickiness
‫to come back to normal behavior.
‫We should be good to go.
‫So that's it for this lecture, I hope you liked it.
‫And I will see you in the next lecture.