1 00:00:00,200 --> 00:00:02,009 Let's talk about Sticky Sessions, 2 00:00:02,009 --> 00:00:03,469 also called Session Affinity 3 00:00:03,469 --> 00:00:06,230 for your elastic load balancer. 4 00:00:06,230 --> 00:00:08,260 So it's possible to implement what's called "stickiness" 5 00:00:08,260 --> 00:00:09,470 or "sticky sessions" 6 00:00:09,470 --> 00:00:11,040 And the idea is that the client, 7 00:00:11,040 --> 00:00:14,700 doing two requests to the load balancer, 8 00:00:14,700 --> 00:00:16,530 will have the same instance 9 00:00:16,530 --> 00:00:18,640 in the backend to respond to the request. 10 00:00:18,640 --> 00:00:19,480 So the idea is that for example, 11 00:00:19,480 --> 00:00:22,370 you have an ALB with to EC2 Instances 12 00:00:22,370 --> 00:00:23,670 and you have three clients, 13 00:00:23,670 --> 00:00:25,410 if the client 1 makes a request 14 00:00:25,410 --> 00:00:27,810 and it goes through the first EC2 Institute, 15 00:00:27,810 --> 00:00:30,120 that means that when it does a second request 16 00:00:30,120 --> 00:00:33,420 to the load balancer, it will go to the same instance, 17 00:00:33,420 --> 00:00:36,170 which is a different behavior where usually, 18 00:00:36,170 --> 00:00:39,050 the application balancer will do a spread 19 00:00:39,050 --> 00:00:42,320 of all the requests across all the EC2 instances. 20 00:00:42,320 --> 00:00:43,510 Now for Client 2, 21 00:00:43,510 --> 00:00:46,060 if it goes to the ALB and talks to the second instance 22 00:00:46,060 --> 00:00:47,840 then all the requests will go there 23 00:00:47,840 --> 00:00:49,850 and same for Client 3, okay? 24 00:00:49,850 --> 00:00:51,660 So this is the behavior that can be enabled 25 00:00:51,660 --> 00:00:53,330 for the Classic Load Balancer 26 00:00:53,330 --> 00:00:55,570 and the Application Load Balancer 27 00:00:55,570 --> 00:00:58,070 and how it works; while there is a cookie 28 00:00:58,070 --> 00:01:00,320 that is sent as part of the request from the clients 29 00:01:00,320 --> 00:01:01,950 to the Load Balancer 30 00:01:01,950 --> 00:01:04,870 and it has stickiness, and it has an expiration date, 31 00:01:04,870 --> 00:01:06,510 that means that when the cookie expires, 32 00:01:06,510 --> 00:01:08,250 then the clients may be redirected 33 00:01:08,250 --> 00:01:10,510 to another EC2 Instance. 34 00:01:10,510 --> 00:01:12,090 Use case for this, is to make sure 35 00:01:12,090 --> 00:01:14,930 that the user is connected to the same backend instance 36 00:01:14,930 --> 00:01:17,700 in order not to lose his session data. 37 00:01:17,700 --> 00:01:19,530 Which can take some important information, 38 00:01:19,530 --> 00:01:22,290 such as; the login of the user, for example. 39 00:01:22,290 --> 00:01:23,660 But if you enable stickiness, 40 00:01:23,660 --> 00:01:25,460 it may bring imbalance to the load 41 00:01:25,460 --> 00:01:26,870 over the backend EC2 Instances 42 00:01:26,870 --> 00:01:31,320 in case some instances have a very, very sticky user. 43 00:01:31,320 --> 00:01:33,700 Okay, now to go a little bit deeper. 44 00:01:33,700 --> 00:01:35,350 How about the cookie itself? 45 00:01:35,350 --> 00:01:38,200 Well, there are two types of cookie that you can have 46 00:01:38,200 --> 00:01:39,090 for Sticky Sessions. 47 00:01:39,090 --> 00:01:40,830 The first one is Application-based Cookie 48 00:01:40,830 --> 00:01:43,810 and the second one is Duration-based cookie. 49 00:01:43,810 --> 00:01:45,540 So for application based cookies, 50 00:01:45,540 --> 00:01:48,540 well, it's a custom cookie that is generated by the target, 51 00:01:48,540 --> 00:01:50,480 So by your application itself, 52 00:01:50,480 --> 00:01:53,400 and you can include any custom attributes that you want 53 00:01:53,400 --> 00:01:54,910 required by the application. 54 00:01:54,910 --> 00:01:57,450 The cookie name must be specified individually 55 00:01:57,450 --> 00:01:59,960 for each target group, okay. 56 00:01:59,960 --> 00:02:03,660 And you must not use the following names; 57 00:02:03,660 --> 00:02:08,520 So, AWSALB, AWSALBAPP, AWSALBTG 58 00:02:08,520 --> 00:02:10,389 because they are already reserved for use 59 00:02:10,389 --> 00:02:12,220 by the ELB itself. 60 00:02:12,220 --> 00:02:14,370 Or, it could be an application cookie 61 00:02:14,370 --> 00:02:18,410 and this time will be generated by the load balancer itself. 62 00:02:18,410 --> 00:02:23,410 And the cookie name used by the ALB will be AWSALBAPP, okay? 63 00:02:23,980 --> 00:02:27,310 Now, the second type of cookies is Duration-based cookie 64 00:02:27,310 --> 00:02:29,720 and it's the cookie generated by the load balancer. 65 00:02:29,720 --> 00:02:34,530 And the name is AWSALB for the ALB and AWSELB for the CLB. 66 00:02:34,530 --> 00:02:37,450 Okay, and the idea is this one will have an expiry 67 00:02:37,450 --> 00:02:39,330 based on a specific duration 68 00:02:39,330 --> 00:02:41,690 and the duration is generated by the load balancer itself. 69 00:02:41,690 --> 00:02:43,350 Okay, whereas before, 70 00:02:43,350 --> 00:02:44,620 while there is an application based cookie, 71 00:02:44,620 --> 00:02:48,140 so the duration can be specified by the application itself. 72 00:02:48,140 --> 00:02:50,250 So that's how it works, okay. 73 00:02:50,250 --> 00:02:53,340 You don't need to remember exactly the name of the cookies 74 00:02:53,340 --> 00:02:55,790 or the fact that you have custom and application, 75 00:02:55,790 --> 00:02:58,450 but remember there's application-based cookies and 76 00:02:58,450 --> 00:03:01,150 duration based cookies and they have a specific name 77 00:03:01,150 --> 00:03:03,250 and this will be coming in to accounts 78 00:03:03,250 --> 00:03:05,090 when we talk about CloudFronts. 79 00:03:05,090 --> 00:03:07,550 Okay, so if I look at my load balancer right now 80 00:03:07,550 --> 00:03:09,290 and I open it in a new tab, as you can see, 81 00:03:09,290 --> 00:03:11,650 it goes between my three instances 82 00:03:11,650 --> 00:03:13,110 in my Load balancer so that's perfect, 83 00:03:13,110 --> 00:03:15,670 but now I'm going to enable Sticky Sessions. 84 00:03:15,670 --> 00:03:18,330 So to do so, I'm going to, at the target group level, 85 00:03:18,330 --> 00:03:21,050 open my target group, and then action 86 00:03:21,050 --> 00:03:24,640 and I will can edit the attributes of my target group. 87 00:03:24,640 --> 00:03:27,850 And in the bottom, I have a stickiness or sticky sessions. 88 00:03:27,850 --> 00:03:28,683 And as we can see, 89 00:03:28,683 --> 00:03:31,160 we have two types of stickiness available to us, okay. 90 00:03:31,160 --> 00:03:34,270 We have the load balancer generated cookie, 91 00:03:34,270 --> 00:03:36,360 which is a duration type of stickiness. 92 00:03:36,360 --> 00:03:39,590 So I can say between one second and seven days, 93 00:03:39,590 --> 00:03:42,470 or I can have an application-based cookie 94 00:03:42,470 --> 00:03:44,750 between one second to seven days again, 95 00:03:44,750 --> 00:03:47,210 but this time I need to specify the cookie name 96 00:03:47,210 --> 00:03:50,800 that is sent by the app to the load balancer. 97 00:03:50,800 --> 00:03:52,823 So it could be MYCUSTOMAPPCOOKIE. 98 00:03:54,290 --> 00:03:57,310 And then this is what the load balancer would be using 99 00:03:57,310 --> 00:03:59,120 to perform the stickiness, okay. 100 00:03:59,120 --> 00:04:00,880 So that's it for a stickiness. 101 00:04:00,880 --> 00:04:02,300 As we can see, if we just have 102 00:04:02,300 --> 00:04:04,210 a load balancer generated cookie, 103 00:04:04,210 --> 00:04:08,200 and we set the stickiness duration to be equal to say; 104 00:04:08,200 --> 00:04:12,420 one day and I will save this change. 105 00:04:12,420 --> 00:04:13,890 So now let's have a look, 106 00:04:13,890 --> 00:04:16,820 so I'm going to go and open the debugger as well 107 00:04:16,820 --> 00:04:19,440 so we can have a look at the network and see what happens. 108 00:04:19,440 --> 00:04:20,959 So if you have a look at the network 109 00:04:20,959 --> 00:04:23,610 and then I refresh this page, as we can see 110 00:04:23,610 --> 00:04:25,260 I'll refresh multiple times okay, 111 00:04:26,210 --> 00:04:27,900 you get access to the same instance. 112 00:04:27,900 --> 00:04:31,890 So, 7-176 is the one that's coming back, back 113 00:04:31,890 --> 00:04:33,150 and back and back. 114 00:04:33,150 --> 00:04:34,600 And now, what's going to happen 115 00:04:34,600 --> 00:04:36,850 is that when you look at the get requests 116 00:04:36,850 --> 00:04:39,030 made to the load balancer. 117 00:04:39,030 --> 00:04:41,370 I'm very, very sorry for the font size here. 118 00:04:41,370 --> 00:04:43,010 I don't think I can really increase it, 119 00:04:43,010 --> 00:04:45,300 but if you go to cookies, as you can see here, 120 00:04:45,300 --> 00:04:47,110 there's a response cookie, okay. 121 00:04:47,110 --> 00:04:50,420 That is saying; that your cookie expires tomorrow, 122 00:04:50,420 --> 00:04:53,800 here is the path and here's the value of the cookie. 123 00:04:53,800 --> 00:04:55,150 And then in the Request Cookie, 124 00:04:55,150 --> 00:04:57,600 when the browser makes a request to the load balancer, 125 00:04:57,600 --> 00:05:00,240 it sends again, the cookie, it has right here. 126 00:05:00,240 --> 00:05:03,500 And so, because of the cookie being past Instance, 127 00:05:03,500 --> 00:05:05,290 this is how stickiness works, okay. 128 00:05:05,290 --> 00:05:09,800 So, just a bit of a deeper dive into how stickiness works, 129 00:05:09,800 --> 00:05:11,480 but that's it for this lecture. 130 00:05:11,480 --> 00:05:12,360 I hope you liked it. 131 00:05:12,360 --> 00:05:17,350 And by the way, to access the web developer tools, 132 00:05:17,350 --> 00:05:20,010 you click on web developer and then web developer tools, 133 00:05:20,010 --> 00:05:21,750 and it just uses a short cut for that, 134 00:05:21,750 --> 00:05:23,270 and it's the same on Chrome and Firefox. 135 00:05:23,270 --> 00:05:24,450 And then you go to Network 136 00:05:24,450 --> 00:05:27,230 and you get access to your information around your request. 137 00:05:27,230 --> 00:05:29,950 Okay, and finally just go back to your target group 138 00:05:29,950 --> 00:05:32,790 and then you edit the attributes itself 139 00:05:32,790 --> 00:05:34,440 and you can disable a stickiness 140 00:05:34,440 --> 00:05:36,360 to come back to a normal behavior, 141 00:05:36,360 --> 00:05:37,460 which should be good to go. 142 00:05:37,460 --> 00:05:39,090 So that's it for this lecture, I hope you liked it. 143 00:05:39,090 --> 00:05:41,040 And I will see you in the next lecture.