Lecture 183: Sticky Sessions (Session Affinity)
- 课程:[NEW] AWS Certified Advanced Networking Specialty 2026
- 视频时长:05:51
- 来源:Udemy Lecture 28555985
- Slide 数量:7
- 画面切换:00:49、01:33、03:01、03:13、04:22、05:15
扫描说明
逐条读取了 Udemy 页面中的 146 条英文 Transcript cue,并按时间轴扫描全长约 05:51 的视频。依据 Session Affinity 概念、Cookie Expiration 与 Trade-off、Cookie Type/Name、启用前分流、Target Group Stickiness 配置、Browser Cookie 验证及恢复默认行为划分为 7 个小节。截图均在 Transcript 侧栏关闭、播放器控件隐藏后复核,统一为 1364×770。
Transcript 来自 Udemy 页面自带的英文自动字幕,没有使用 ASR。下面保留页面原文,包括其中的语法或识别错误。
1. Sticky Session 把同一 Client 固定到同一 Target
时间范围:00:00–00:49
总结 / Summary
中文
Sticky Session 又称 Session Affinity,使同一 Client 的连续请求始终进入同一 Backend Instance,而不是每次都重新分散。不同 Client 仍可分别绑定不同 Instance;课程指出这种能力可用于 CLB、ALB 与 NLB。
English
Sticky sessions, also called session affinity, keep consecutive requests from the same client on the same backend instance instead of redistributing every request. Different clients can still bind to different instances. The course states that this behavior is available for CLB, ALB, and NLB.
Transcript
00:00 Let's talk about sticky sessions
00:01 or so-called session affinity
00:03 for your elastic load balancer.
00:06 So it's possible to implement
00:07 what's called stickiness or sticky sessions.
00:09 And the idea is that the client
00:11 doing two requests to the load balancer
00:14 will have the same instance in the backend
00:17 to respond to the request.
00:18 So the idea is that, for example, you have the ALB
00:20 with two EC2 instances and you have three clients.
00:23 If the client one makes a request
00:25 and it goes to the first EC2 instance,
00:27 that means that when it does a second request
00:30 to the load balancer, it will go to the same instance,
00:33 which is a different behavior
00:34 where usually the application balancer
00:37 will do a spread of all the requests
00:39 across all the EC2 instances.
00:42 Now for client two, if it goes to the ALB
00:44 and talks to the second instance,
00:46 then all the requests will go there.
00:47 And same for client three, okay?
2. Cookie Expiration、Session Data 与负载不均
时间范围:00:49–01:33
总结 / Summary
中文
ELB 通过随 Request 发送且带 Expiration 的 Cookie 维持 Stickiness;Cookie 过期后,Client 可能被重新分配到其他 Instance。典型用途是避免 Login 等 Session Data 丢失,但长期黏住高流量 User 也可能造成 Backend Load Imbalance。
English
ELB maintains stickiness with a cookie sent on requests and carrying an expiration time. After the cookie expires, the client may be assigned to another instance. The common use case is preserving session data such as login state, but a high-traffic user pinned for too long can create backend load imbalance.
Transcript
00:49 So this can be enabled for the Classic Load Balancer
00:52 Application Balancer and Network Load Balancer.
00:55 And how it works, well, there is a cookie
00:58 that is sent as part of the request
00:59 from the clients to the load balancer
01:01 and it has stickiness and it has an expiration date.
01:04 That means that when the cookie expires,
01:06 then the client may be redirected to another EC2 instance.
01:10 The use case for this is to make sure
01:12 that the user is connected to the same backend instance
01:14 in order not to lose his session data,
01:17 which can take some important information
01:19 such as the login of the user, for example.
01:22 But if you enable stickiness,
01:23 it may bring imbalance to the load
01:25 over the backend EC2 instances
01:26 in case some instances I have a very, very sticky user.
01:31 Okay, now to go a little bit deeper,
3. Application-based 与 Duration-based Cookie
时间范围:01:33–03:01
总结 / Summary
中文
Application-based Cookie 可由 Target Application 生成,自定义 Attribute,并为每个 Target Group 指定名称;不要使用 ELB 保留名 AWSALB、AWSALBAPP、AWSALBTG。ALB 生成的 Application Cookie 名为 AWSALBAPP。Duration-based Cookie 由 Load Balancer 生成并控制 Expiry,ALB 使用 AWSALB,CLB 使用 AWSELB。
English
An application-based cookie can be generated by the target application, include custom attributes, and use a name specified per target group. Do not use the ELB-reserved names AWSALB, AWSALBAPP, or AWSALBTG. An ALB-generated application cookie is named AWSALBAPP. Duration-based cookies are generated by the load balancer with load-balancer-controlled expiry: AWSALB for ALB and AWSELB for CLB.
Transcript
01:33 how about the cookie itself?
01:35 Well, there are two types of cookie
01:37 that you can have for sticky sessions.
01:39 The first one is application-based cookie
01:40 and the second one is duration-based cookie.
01:43 So for application-based cookies,
01:45 well, it's a custom cookie that is generated by the target,
01:48 so by your application itself.
01:50 And you can include any custom attributes you want
01:53 required by your application.
01:54 The cookie name must be specified individually
01:57 for each target group, okay?
01:59 And you may not, you must not use the following names,
02:03 so AWSALB, AWSALBAPPOR or AWSALBTG,
02:08 because they are already reserved for use by the ELB itself.
02:12 Or it could be an application cookie.
02:14 And this time will be generated by the load balancer itself,
02:18 and the cookie name used by the ALB will be AWSALBAPP, okay?
02:23 Now the second type of cookies is duration-based cookie
02:27 and it's a cookie generated by the load answer,
02:29 and the name is AWSALB for the ALB
02:32 and AWSELB for the CLB, okay?
02:35 And the idea is this one,
02:36 will have an expiry based on a specific duration,
02:39 and the duration is generated
02:40 by the load balancer itself, okay?
02:42 Whereas before, while there is an application-based cookie,
02:44 so the duration can be specified by the application itself.
02:48 So that's how it works, okay?
02:50 You don't need to remember exactly the name of the cookies
02:53 or the fact that you have custom and application,
02:55 but you remember there's application-based cookies
02:58 and duration based, and they have a specific name.
4. 启用前的正常 Load Balancing
时间范围:03:01–03:13
总结 / Summary
中文
演示开始时反复刷新 ALB 页面,会看到 Hostname 在三台 Instance 之间变化,说明普通 Load Balancing 正在按算法选择不同 Target。这个 Baseline 用于对比启用 Stickiness 后的固定响应。
English
Before enabling stickiness, repeatedly refreshing the ALB page returns hostnames from different instances, showing normal algorithm-based target selection. This baseline makes the later pinned behavior easy to verify.
Transcript
03:01 And this will be coming into account
03:03 when we talk about CloudFront.
03:05 Okay, so if I look at my load balancer right now
03:07 and I open it in the new tab, as you can see,
03:09 it goes between my three instances in my load answer.
03:12 So that's perfect.
5. 在 Target Group Attributes 启用 Stickiness
时间范围:03:13–04:22
总结 / Summary
中文
进入 Target Group 的 Edit Attributes,在 Target Selection Configuration 打开 Stickiness。可选择 Load Balancer Generated Cookie,Duration 可设 1 秒到 7 天;也可选 Application-based Cookie 并填写 App Cookie Name。演示使用 LB Generated Cookie 与默认 1 Day 后保存。
English
Open the target group's Edit Attributes page and enable stickiness under target selection configuration. Choose a load-balancer-generated cookie with a duration from one second to seven days, or choose an application-based cookie and provide an app cookie name. The demo saves the load-balancer-generated option with the default one-day duration.
Transcript
03:13 But now I'm going to enable sticky sessions.
03:15 So to do so, I'm going to go at the target group level,
03:18 open my target group and then Action,
03:21 and I will can edit the attributes of my target group.
03:24 So here, let's scroll down
03:27 and we have the target selection configuration,
03:30 and here we have stickiness.
03:32 So here for stickiness, we can turn it on.
03:36 And we have two types of stickiness available to us.
03:40 We have a load balancer generated cookie
03:42 or the application based cookie.
03:44 So let's look at the first one.
03:46 So here we're able to set a duration.
03:48 So between one second to seven days, you can modify it here.
03:52 Or if you wanted to have your own application cookie,
03:56 again, they are the same settings,
03:57 but you need to add an app cookie name,
03:59 for example, MYCUSTOMCOOKIEAPP.
04:04 And this is just a name that you would give your cookie
04:07 so that you know how to apply stickiness
04:10 at the low balancer level.
04:12 Now, to keep things simple,
04:13 we're going to choose Load balancer generated cookie
04:16 and have everything as the default, so one day.
04:19 And let's click on Save changes.
6. 用 Browser Network Panel 验证 Cookie 往返
时间范围:04:22–05:15
总结 / Summary
中文
启用后连续刷新始终返回同一 Instance。Browser Developer Tools 的 Network/Cookies 面板显示 ALB 在 Response 中下发带 Expiry、Path 与 Value 的 Cookie,随后 Browser 又在 Request Cookie 中回传相同值;ELB 正是根据这个往返 Cookie 维持 Affinity。
English
After enabling stickiness, repeated refreshes keep returning the same instance. The browser Network/Cookies panel shows the ALB setting a response cookie with expiry, path, and value, and the browser sending the value back in later request cookies. ELB uses this round trip to maintain affinity.
Transcript
04:22 So now let's have a look.
04:24 So I'm going to go and open the debugger as well,
04:27 so we can have a look at the network and see what happens.
04:29 So if you ever look at the network
04:31 and then I refresh this page,
04:32 as we can see, I refresh multiple times, okay?
04:36 You get access to the same instance.
04:38 So 7176 is the one that's coming back,
04:41 back and back and back.
04:43 And now what's going to happen
04:44 is that when you look at the get request
04:47 made to the load balancer.
04:49 I'm very, very sorry for the font size here.
04:51 I don't think I can really increase it.
04:53 But if you go to Cookies, as you can see here,
04:55 there is a response cookie, okay,
04:57 that is saying that your cookie expires tomorrow.
05:00 Here is the path and here is the value of the cookie.
05:04 And then in the request cookie,
05:05 when the browser makes a request to the load balancer,
05:07 it sends again the cookie it has right here.
05:10 And so because of the cookie being passed and sent,
05:13 this is how stickiness works, okay?
7. 关闭 Stickiness 恢复普通分流
时间范围:05:15–05:51
总结 / Summary
中文
验证结束后返回 Target Group Attributes,关闭 Stickiness 并保存,即可恢复普通 Load Balancing。排查 Session Affinity 时,可同时检查 Target Group 设置、Browser Request/Response Cookie 与 Cookie Expiration。
English
After validation, return to the target group attributes, disable stickiness, and save to restore normal load balancing. When troubleshooting session affinity, inspect the target-group setting, browser request and response cookies, and cookie expiration together.
Transcript
05:15 So just a little bit of a deeper dive
05:18 into how stickiness works.
05:20 But that's it for this lecture.
05:21 I hope you liked it.
05:22 And by the way, to access the web developer tools,
05:27 you click on Web Developer and then Web Developer Tools.
05:30 And I just use a short shortcut for that.
05:32 And it's the same on Chrome and Firefox.
05:33 And then you go into Network
05:34 and you get access to your information
05:36 around your request, okay?
05:38 And finally, just go back to your target group,
05:40 and then you edit the attributes itself
05:43 and you can disable stickiness
05:44 to come back to a normal behavior,
05:46 which should be good to go.
05:47 So that's it for this lecture, I hope you liked it.
05:49 And I will see you in the next lecture.
整讲总结 / Overall Summary
中文
Sticky Sessions 使用 Cookie 将同一 Client 的请求持续绑定到同一 Target,适合在 Backend 保存 Login 等 Session State,但会增加负载不均风险。需要区分 Application-based 与 Duration-based Cookie、避免保留名称,并在 Target Group Attributes 配置 Duration 或 App Cookie Name。Browser Network Panel 能直接验证 Response Cookie 下发及 Request Cookie 回传;关闭 Stickiness 后恢复普通分流。
English
Sticky sessions use a cookie to keep one client's requests bound to the same target. This helps when login or other session state is stored on the backend but can cause uneven load. Distinguish application-based from duration-based cookies, avoid reserved names, and configure duration or app cookie name in target group attributes. The browser Network panel verifies response-cookie creation and request-cookie return; disabling stickiness restores normal distribution.
返回顶部 ↑