1 00:00:00,180 --> 00:00:01,770 ‫Let's talk about sticky sessions, 2 00:00:01,770 --> 00:00:03,480 ‫also called session affinity 3 00:00:03,480 --> 00:00:06,240 ‫for your Elastic Load Balancer. 4 00:00:06,240 --> 00:00:08,280 ‫So it's possible to implement what's called stickiness 5 00:00:08,280 --> 00:00:09,480 ‫or sticky sessions, 6 00:00:09,480 --> 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,560 ‫will have the same instance 9 00:00:16,560 --> 00:00:18,630 ‫in the backend to respond to the request. 10 00:00:18,630 --> 00:00:19,560 ‫So the idea is that for example, 11 00:00:19,560 --> 00:00:22,221 ‫you have the ALB with two EC2 instances, 12 00:00:22,221 --> 00:00:23,670 ‫and you have three clients. 13 00:00:23,670 --> 00:00:25,410 ‫If the client one makes a request 14 00:00:25,410 --> 00:00:27,660 ‫and it goes to the first EC2 instance, 15 00:00:27,660 --> 00:00:30,120 ‫that means that when it does a second request 16 00:00:30,120 --> 00:00:30,953 ‫to the load balancer, 17 00:00:30,953 --> 00:00:33,420 ‫it will go to the same instance. 18 00:00:33,420 --> 00:00:34,980 ‫Which is a different behavior 19 00:00:34,980 --> 00:00:37,260 ‫where usually the Application Load Balancer 20 00:00:37,260 --> 00:00:40,020 ‫will do a spread of all the requests 21 00:00:40,020 --> 00:00:42,300 ‫across all the EC2 instances. 22 00:00:42,300 --> 00:00:44,730 ‫Now for client two, if it goes to the ALB 23 00:00:44,730 --> 00:00:46,050 ‫and talks to the second instance, 24 00:00:46,050 --> 00:00:47,850 ‫then all the requests will go there. 25 00:00:47,850 --> 00:00:49,230 ‫And same for client three. 26 00:00:49,230 --> 00:00:50,063 ‫Okay? 27 00:00:50,063 --> 00:00:51,660 ‫So this can be enabled 28 00:00:51,660 --> 00:00:53,310 ‫for the Classic Load Balancer, Application Load Balancer 29 00:00:53,310 --> 00:00:55,560 ‫and the Network Load Balancer 30 00:00:55,560 --> 00:00:58,080 ‫And how it works, well there is a cookie 31 00:00:58,080 --> 00:00:59,670 ‫that is sent as part of the request 32 00:00:59,670 --> 00:01:01,950 ‫from the client's two load balancer. 33 00:01:01,950 --> 00:01:04,890 ‫And it has stickiness and it has an expiration date, 34 00:01:04,890 --> 00:01:06,510 ‫that means that when the cookie expires 35 00:01:06,510 --> 00:01:10,500 ‫then the client may be redirected to another EC2 instance. 36 00:01:10,500 --> 00:01:12,090 ‫Use case for this is to make sure 37 00:01:12,090 --> 00:01:14,940 ‫that the user is connected to the same backend instance, 38 00:01:14,940 --> 00:01:17,730 ‫in order not to lose his session data. 39 00:01:17,730 --> 00:01:19,530 ‫Which can take some important information 40 00:01:19,530 --> 00:01:22,290 ‫such as the login of the user, for example. 41 00:01:22,290 --> 00:01:23,670 ‫But if you enable stickiness, 42 00:01:23,670 --> 00:01:25,470 ‫it may bring imbalance to the load 43 00:01:25,470 --> 00:01:26,910 ‫over the backend EC2 instances, 44 00:01:26,910 --> 00:01:31,320 ‫in case some instances I have a very, very sticky user. 45 00:01:31,320 --> 00:01:32,153 ‫Okay. 46 00:01:32,153 --> 00:01:33,720 ‫Now to go a little bit deeper, 47 00:01:33,720 --> 00:01:35,370 ‫how about the cookie itself? 48 00:01:35,370 --> 00:01:37,410 ‫Well, there are two types of cookie 49 00:01:37,410 --> 00:01:39,090 ‫that you can have for sticky sessions. 50 00:01:39,090 --> 00:01:40,830 ‫The first one is application-based cookie, 51 00:01:40,830 --> 00:01:43,800 ‫and the second one is duration-based cookie. 52 00:01:43,800 --> 00:01:45,540 ‫So for application-based cookies, 53 00:01:45,540 --> 00:01:46,890 ‫well it's a custom cookie 54 00:01:46,890 --> 00:01:48,540 ‫that is generated by the target, 55 00:01:48,540 --> 00:01:50,490 ‫so by your application itself. 56 00:01:50,490 --> 00:01:52,770 ‫And you can include any custom attributes you want 57 00:01:52,770 --> 00:01:54,900 ‫by required by your application. 58 00:01:54,900 --> 00:01:57,420 ‫The cookie name must be specified individually 59 00:01:57,420 --> 00:01:59,940 ‫for each target group, okay? 60 00:01:59,940 --> 00:02:03,600 ‫And you must not use the following name, 61 00:02:03,600 --> 00:02:08,550 ‫so AWSALB, AWSALBAPP, OR AWSALBTG, 62 00:02:08,550 --> 00:02:09,990 ‫because they are already reserved 63 00:02:09,990 --> 00:02:12,210 ‫for used by the ALB itself. 64 00:02:12,210 --> 00:02:14,370 ‫Or it could be an application cookie, 65 00:02:14,370 --> 00:02:18,390 ‫and this time will be generated by the load balancer itself. 66 00:02:18,390 --> 00:02:22,950 ‫And the cookie name used by the ALB will be AWSALBAPP. 67 00:02:22,950 --> 00:02:24,000 ‫Okay? 68 00:02:24,000 --> 00:02:27,300 ‫Now the second type of cookies is duration-based cookie, 69 00:02:27,300 --> 00:02:29,730 ‫and it's a cookie generated by the load balancer. 70 00:02:29,730 --> 00:02:32,640 ‫And the name is AWSALB for the ALB, 71 00:02:32,640 --> 00:02:34,530 ‫and AWSELB for the CLB. 72 00:02:34,530 --> 00:02:35,363 ‫Okay? 73 00:02:35,363 --> 00:02:37,470 ‫And the idea is this one will have an expiry 74 00:02:37,470 --> 00:02:39,330 ‫based on a specific duration, 75 00:02:39,330 --> 00:02:40,470 ‫and the duration is generated 76 00:02:40,470 --> 00:02:41,700 ‫by the load balancer itself. 77 00:02:41,700 --> 00:02:42,533 ‫Okay? 78 00:02:42,533 --> 00:02:43,680 ‫Whereas before, well there 79 00:02:43,680 --> 00:02:44,760 ‫is an application-based cookie, 80 00:02:44,760 --> 00:02:48,120 ‫so the duration can be specified by the application itself. 81 00:02:48,120 --> 00:02:50,250 ‫So that's how it works, okay? 82 00:02:50,250 --> 00:02:53,370 ‫You don't need to remember exactly the name of the cookies 83 00:02:53,370 --> 00:02:55,800 ‫or the fact that you have custom and application, 84 00:02:55,800 --> 00:02:58,200 ‫but you remember there is application-based cookies 85 00:02:58,200 --> 00:02:59,790 ‫and duration-based cookies, 86 00:02:59,790 --> 00:03:01,140 ‫and they have a specific name, 87 00:03:01,140 --> 00:03:03,300 ‫and this will be coming into account 88 00:03:03,300 --> 00:03:05,100 ‫when we talk about CloudFront. 89 00:03:05,100 --> 00:03:05,933 ‫Okay? 90 00:03:05,933 --> 00:03:07,560 ‫So if I look at my load balancer right now, 91 00:03:07,560 --> 00:03:09,150 ‫and I open it in the new tab, as you can see 92 00:03:09,150 --> 00:03:12,480 ‫it goes between my three instances in my load balancer. 93 00:03:12,480 --> 00:03:13,313 ‫So that's perfect. 94 00:03:13,313 --> 00:03:15,690 ‫But now I'm going to enable sticky sessions. 95 00:03:15,690 --> 00:03:18,300 ‫So to do so, I'm going to go at the target group level, 96 00:03:18,300 --> 00:03:19,463 ‫open my target group, 97 00:03:19,463 --> 00:03:21,030 ‫and then Action. 98 00:03:21,030 --> 00:03:24,630 ‫And I will can edit the attributes of my target group. 99 00:03:24,630 --> 00:03:27,840 ‫And in the bottom I have Stickiness or sticky sessions. 100 00:03:27,840 --> 00:03:28,673 ‫And as we can see, 101 00:03:28,673 --> 00:03:30,990 ‫we have two types of stickiness available to us. 102 00:03:30,990 --> 00:03:31,823 ‫Okay? 103 00:03:31,823 --> 00:03:34,260 ‫We have the load balancer generated cookie, 104 00:03:34,260 --> 00:03:36,360 ‫which is a duration type of stickiness. 105 00:03:36,360 --> 00:03:39,570 ‫So I can say between one second and seven days. 106 00:03:39,570 --> 00:03:42,480 ‫Or I can have an application-based cookie, 107 00:03:42,480 --> 00:03:44,760 ‫between one second to seven days again, 108 00:03:44,760 --> 00:03:47,220 ‫but this time I need to specify the cookie name 109 00:03:47,220 --> 00:03:50,790 ‫that is sent by the app to the load balancer. 110 00:03:50,790 --> 00:03:52,563 ‫So it could be MYCUSTOMAPPCOOKIE. 111 00:03:54,300 --> 00:03:55,837 ‫And then this is what the load balancer 112 00:03:55,837 --> 00:03:58,530 ‫would be using to perform the stickiness. 113 00:03:58,530 --> 00:03:59,363 ‫Okay? 114 00:03:59,363 --> 00:04:00,870 ‫So that's it for stickiness. 115 00:04:00,870 --> 00:04:01,703 ‫As we can see, 116 00:04:01,703 --> 00:04:04,200 ‫if we just have a load balancer generated cookie 117 00:04:04,200 --> 00:04:07,140 ‫and we set the stickiness duration to be equal 118 00:04:07,140 --> 00:04:09,003 ‫to say one day, 119 00:04:09,840 --> 00:04:12,420 ‫and I will save this change. 120 00:04:12,420 --> 00:04:13,890 ‫So now let's have a look. 121 00:04:13,890 --> 00:04:16,830 ‫So I'm going to go and open the debugger as well 122 00:04:16,830 --> 00:04:18,180 ‫so we can have a look at the network 123 00:04:18,180 --> 00:04:19,440 ‫and see what happens. 124 00:04:19,440 --> 00:04:20,970 ‫So if we have a look at the network, 125 00:04:20,970 --> 00:04:22,620 ‫and then I refresh this page, 126 00:04:22,620 --> 00:04:24,750 ‫as we can see, I refresh it multiple times. 127 00:04:24,750 --> 00:04:25,767 ‫Okay? 128 00:04:25,767 --> 00:04:27,900 ‫You get access to the same instance. 129 00:04:27,900 --> 00:04:30,660 ‫So 7-176 is the one 130 00:04:30,660 --> 00:04:33,150 ‫that's coming back, back, and back, and back. 131 00:04:33,150 --> 00:04:34,710 ‫And now what's going to happen is 132 00:04:34,710 --> 00:04:37,410 ‫that when you look at the GET request made 133 00:04:37,410 --> 00:04:38,940 ‫to the load balancer, 134 00:04:38,940 --> 00:04:41,340 ‫and I'm very, very sorry for the font size here, 135 00:04:41,340 --> 00:04:43,020 ‫I don't think I can really increase it. 136 00:04:43,020 --> 00:04:45,300 ‫But if you go to Cookies, as you can see here, 137 00:04:45,300 --> 00:04:47,130 ‫there is a response cookie, okay? 138 00:04:47,130 --> 00:04:50,430 ‫That is saying that your cookie expires tomorrow. 139 00:04:50,430 --> 00:04:51,570 ‫Here is the path, 140 00:04:51,570 --> 00:04:53,790 ‫and here is the value of the cookie. 141 00:04:53,790 --> 00:04:55,170 ‫And then in the request cookie, 142 00:04:55,170 --> 00:04:57,600 ‫when the browser makes a request to the load balancer 143 00:04:57,600 --> 00:05:00,270 ‫it sends again the cookie it has right here. 144 00:05:00,270 --> 00:05:03,510 ‫And so because of the cookie being passed and sent, 145 00:05:03,510 --> 00:05:05,100 ‫this is how stickiness works. 146 00:05:05,100 --> 00:05:05,933 ‫Okay? 147 00:05:05,933 --> 00:05:08,280 ‫So just a little bit of a deeper dive 148 00:05:08,280 --> 00:05:09,780 ‫into how stickiness works. 149 00:05:09,780 --> 00:05:11,490 ‫But that's it for this lecture, 150 00:05:11,490 --> 00:05:12,360 ‫I hope you liked it. 151 00:05:12,360 --> 00:05:17,340 ‫And by the way, to access the web developer tools, 152 00:05:17,340 --> 00:05:18,750 ‫you click on Web Developer 153 00:05:18,750 --> 00:05:20,010 ‫and then Web Developer Tools. 154 00:05:20,010 --> 00:05:21,750 ‫And I just use a short shortcut for that. 155 00:05:21,750 --> 00:05:23,250 ‫And it's the same on Chrome and Firefox. 156 00:05:23,250 --> 00:05:24,450 ‫And then you go into Network, 157 00:05:24,450 --> 00:05:27,240 ‫and you get access to your information around your request. 158 00:05:27,240 --> 00:05:28,073 ‫Okay? 159 00:05:28,073 --> 00:05:29,940 ‫And finally, just go back to your target group 160 00:05:29,940 --> 00:05:32,790 ‫and then you edit the attributes itself, 161 00:05:32,790 --> 00:05:34,440 ‫and you can disable stickiness 162 00:05:34,440 --> 00:05:36,360 ‫to come back to normal behavior. 163 00:05:36,360 --> 00:05:37,470 ‫We should be good to go. 164 00:05:37,470 --> 00:05:39,090 ‫So that's it for this lecture, I hope you liked it. 165 00:05:39,090 --> 00:05:41,040 ‫And I will see you in the next lecture.