Elastic Load Balancing with Sticky Sessions
Elastic Load Balancing with Sticky Sessions — Shlomo Swidler https://shlomoswidler.com/2010/04/elastic-load-balancing-with-sticky-sessions.html
Elastic Load Balancing with Sticky Sessions
At long last, the most oft-requested feature for EC2’s Elastic Load Balancer is here: session affinity, also known as “sticky sessions”. What is session affinity? Why is this feature in such high demand? How can it be used with existing applications? Let’s take a look at these questions. But first, let’s explore what a session is – then we’ll cover why we want it to be sticky, and what ELB’s sticky session limitations are. [To skip directly to an explanation of how to use ELB sticky sessions, go toward the bottom of the article.]
What is a Session?
A session is a way to get your application involved in a long-lasting conversation with a particular client. Without a session, a conversation between your application and a client would look like something straight out of the movie Memento. It would look like this:
Life Without Sessions
Client: Hi, I’d like to see /products/awesomeDoohickey.html
Application: I don’t know who you are. Please go here to login first: /login
Client: OK, I’d like to see /login
Application: Here it is: “…”
Client: Thanks. Here’s the filled in login form.
Application: Thanks for logging in. Where do you want to go?
Client: I’d like to see /products/awesomeDoohickey.html
Application: I don’t know who you are. Please go here to login first: /login
Client: >Sigh< OK, I’d like to see /login
Application: Happily! Here it is: “…”
Client: Here’s the filled in login form.
Application: Thanks for logging in. Where do you want to go?
Client: Show me /products/awesomeDoohickey.html already!
Application: I don’t know who you are. Please go here to login first: /login
Client: *$#%& this!
The application can’t remember who the client is – it has no context to process each request as part of a conversation. The client gets so frustrated he starts thinking he’s living in an Adam Sandler movie.
On a technical level: Each HTTP request-response pair between the client and application happens (most often) on a different TCP connection. This is especially true when a load balancer sits between the client and the application. So the application can’t use the TCP connection as a way to remember the conversational context. And, HTTP itself is stateless: any request can be sent at any time, in any sequence, regardless of the preceding requests. Sure, the application may demand a particular pattern of interaction – like logging in before accessing certain resources – but that application-level state is enforced by the application, not by HTTP. So HTTP cannot be relied on to maintain conversational context between the client and the application.
There are two ways to solve this problem of forgetting the context. The first is for the client to remind the application of the context every time he requests something: “My name is Henry Whatsisface, I have these items in my shopping cart (…), I got here via this affiliate (…), yada yada yada… and I’d like to see /products/awesomeDoohickey.html”. No sane client would ever agree to interact with an application that needed to be sent the entire context at every stage of the conversation. Its burdensome for the client, it’s difficult to maintain for the application, and it’s expensive (in bandwidth) for both of them. Besides, the application usually maintains the conversational state, not the client. So it’s wrong to require the client to send the entire conversation context along with each request.
The accepted solution is to have the application remember the context by creating an associated memento. This memento is given to the client and returned to the application on subsequent requests. Upon receiving the memento the application looks for the associated context, and – voila – discovers it. Thus, the conversation is preserved.
One way of providing a memento is by putting it into the URL. It looks really ugly when you do this: http://www.example.com/products/awesomeDoohickey.html?sessionID=0123456789ABCDEFGH
More commonly, mementos are provided via cookies, which all browsers these days support. Cookies are placed within the HTTP request so they can be discovered by the application even if a load balancer intervenes.
Here’s what that conversation looks like with cookies:
Life With Sessions, Take 1
Client: Hi, I’d like to see /products/awesomeDoohickey.html
Application: I don’t know who you are. Please go here to login first: /login
Client: OK, I’d like to see /login
Application: Here it is: “…”
Client: Thanks. Here’s the filled in login form.
Application: Thanks for logging in. Here’s a cookie. Where do you want to go?
Client: I’d like to see /products/awesomeDoohickey.html and here’s my cookie.
Application: I know you – I’d recognize that cookie anywhere! Great, here’s that page: “…”
Client: I’d like to buy 5000 units. Here’s my cookie.
Much improved, yes?
A side point: most modern applications will provide a cookie earlier in the conversation. This allows the following more optimal conversation:
Life With Sessions, Take 2
Client: Hi, I’d like to see /products/awesomeDoohickey.html
Application: I don’t know who you are. Here’s a cookie. Take this login page and fill it out: “…”
Client: OK. Here’s the filled in login form. And here’s my cookie.
Application: I know you – I’d recognize that cookie anywhere! Thanks for logging in. I recall you wanted to see /products/awesomeDoohickey.html. Here it is: “…”
Client: I’d like to buy 5000 units. Here’s my cookie.
That’s about as optimized a conversation as you can have. Cookies make it possible.
What is Session Affinity (Sticky Sessions)? Why is it in High Demand?
When you only have one application server talking to your clients life is easy: all the session contexts can be stored in that application server’s memory for fast retrieval. But in the world of highly available and scalable applications there’s likely to be more than one application server fulfilling requests, behind a load balancer. The load balancer routes the first request to an application server, who stores the session context in its own memory and gives the client back a cookie. The next request from the same client will contain the cookie – and, if the same application server gets the request again, the application will rediscover the session context. But what happens if that client’s next request instead gets routed to a different application server? That application server will not have the session context in its memory – even though the request contains the cookie, the application can’t discover the context.
If you’re willing to modify your application you can overcome this problem. You can store the session context in a shared location, visible to all application servers: the database or memcached, for example. All application servers will then be able to lookup the cookie in the central, shared location and discover the context. Until now, this was the approach you needed to take in order to retain the session context behind an Elastic Load Balancer.
But not all applications can be modified in this way. And not all developers want to modify existing applications. Instead of modifying the application, you need the load balancer to route the same client to the same application server. Once the client’s request has been routed to the correct application server, that application server can lookup the session cookie in its own memory and recover the conversational context.
That’s what sticky sessions are: the load balancer routing the same client to the same application server. And that’s why they’re so important: If the load balancer supports sticky sessions then you don’t need to modify your application to remember client session context.
How to Use ELB with Sticky Sessions with Existing Applications
The key to managing ELB sticky sessions is the duration of the stickiness: how long the client should consistently be routed to the same back-end instance. Too short, and the session context will be lost, forcing the client to login again. Too long, and the load balancer will not be able to distribute requests equally across the application servers.
Controlling the ELB Stickiness Duration
ELB supports two ways of managing the stickiness’ duration: either by specifying the duration explicitly, or by indicating that the stickiness expiration should follow the expiration of the application server’s own session cookie.
If your application server has an existing session cookie already, the simplest way to get stickiness is to configure your ELB to use the existing application cookie for determining the stickiness duration. PHP applications usually have a session cookie called PHPSESSID. Java applications usually have a session cookie called JSESSIONID. The expiration of these cookies is controlled by your application, and the stickiness expiration can be set to match as follows. Assuming your load balancer is called myLoadBalancer and it has an HTTP listener on port 80:
elb-create-app-cookie-stickiness-policy myLoadBalancer --cookie-name PHPSESSID --policy-name followPHPPolicy
elb-set-lb-policies-of-listener myLoadBalancer --lb-port 80 --policy-names followPHPPolicy
The above commands create a stickiness policy that says “make the session stickiness last as long as the cookie PHPSESSID does” and sets the load balancer to use that stickiness policy. Behind the scenes, the ELB’s session cookie will have the same lifetime as the PHPSESSID cookie provided by your application.
If your application does not have its own session cookie already, set your own stickiness duration for the load balancer, as follows:
elb-create-lb-cookie-stickiness-policy myLoadBalancer --policy-name fifteenMinutesPolicy --expiration-period 900
elb-set-lb-policies-of-listener myLoadBalancer --lb-port 80 --policy-names fifteenMinutesPolicy
These commands create a stickiness policy that says “make the session stickiness last for fifteen minutes” and sets the load balancer to use that stickiness policy. Behind the scenes, the ELB’s session cookie will have a lifetime of fifteen minutes.
What Can’t ELB Sticky Session Do?
Life is not all roses with ELB’s sticky session support. Here are some things it can’t do.
Update October 2010: ELB now supports SSL termination, and it can provide sticky sessions over HTTPS as well.
HTTPS
Remember how sticky sessions are typically provided via cookies? The cookie is inserted into the HTTP request by the client’s browser, and any server or load balancer that can read the request can recover the cookie. This works great for plain old HTTP-based communications.
With HTTPS connections the entire communications stream is encrypted. Only servers that have the proper decryption credentials can decipher the stream and discover the cookies. If the load balancer has the server’s SSL certificate then it can decrypt the stream. Because it does not have your application’s SSL certificate (and there’s no way to give it your certificate) ELB does not support HTTPS communications. If you need to support sticky sessions and HTTPS in EC2 then you can’t use ELB today. You need to use HAProxy or aiCache or another product that provide load balancing with session affinity and SSL termination.
Scaling-down-proof stickiness
What happens when you add or remove an application server to/from the load balancer? Depending on the stickiness implementation the load balancer may or may not be able to route requests to the same application servers as it did before the scaling event (caused, for example, by an AutoScaling trigger).
When scaling up (adding more application servers) ELB maintains stickiness of existing sessions. Only new connections will be forwarded to the newly-added application servers.
When scaling down (removing application servers), you should expect some of your clients to lose their sessions and require logins again. This is because some of the stored sessions were on the application server that is no longer servicing requests.
If you really want your sessions to persist even through scaling-down events, you need to go back to basics: your application will need to store the sessions independently, as it did before sticky sessions were supported. In this case, sticky session support can provide an added optimization, allowing you to cache the session locally on each application server and only retrieve it from the central session store (the DB?) if it’s not in the local cache. Such cache misses would happen when application servers are removed from the load balancing pool, but otherwise would not impact performance. Note that this technique can be used equally well with ELB and with other load balancers.
With the introduction of sticky sessions for ELB, you – the application developer – can avoid modifying your application in order to retain session context behind a load balancer. The technical term for this is “a good thing”. Sticky sessions are, despite their limitations, a very welcome addition to ELB’s features.
Thanks to Jeff Barr of Amazon Web Services for providing feedback on drafts of this article.
Elastic Load Balancing with Sticky Sessions的更多相关文章
- NGINX Load Balancing - HTTP Load Balancer
This chapter describes how to use NGINX and NGINX Plus as a load balancer. Overview Load balancing a ...
- Load Balancing with NGINX 负载均衡算法
Using nginx as HTTP load balancer Using nginx as HTTP load balancer http://nginx.org/en/docs/http/lo ...
- 【架构】How To Use HAProxy to Set Up MySQL Load Balancing
How To Use HAProxy to Set Up MySQL Load Balancing Dec 2, 2013 MySQL, Scaling, Server Optimization U ...
- How Network Load Balancing Technology Works--reference
http://technet.microsoft.com/en-us/library/cc756878(v=ws.10).aspx In this section Network Load Balan ...
- Network Load Balancing Technical Overview--reference
http://technet.microsoft.com/en-us/library/bb742455.aspx Abstract Network Load Balancing, a clusteri ...
- 负载均衡(Load Balancing)学习笔记(一)
概述 在分布式系统中,负载均衡(Load Balancing)是一种将任务分派到多个服务端进程的方法.例如,将一个HTTP请求派发到实际的Web服务器中执行的过程就涉及负载均衡的实现.一个HTTP请求 ...
- How Load Balancing Policies Work
How Load Balancing Policies Work https://docs.cloud.oracle.com/en-us/iaas/Content/Balance/Reference/ ...
- CF# Educational Codeforces Round 3 C. Load Balancing
C. Load Balancing time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...
- Codeforces Educational Codeforces Round 3 C. Load Balancing 贪心
C. Load Balancing 题目连接: http://www.codeforces.com/contest/609/problem/C Description In the school co ...
随机推荐
- centos 安装部署ftp服务器
0. 安装ftp yum install vsftpd 1. 添加ftp账户 useradd -d /home/test -g ftp -s /sbin/nologin test 命令的意思: 添加t ...
- web压力测试指标
1.TPS每秒钟完成的web请求响应数量TPS=并发数/响应时间TPS是衡量系统性能的重要指标 2.并发数时间段内,系统同时处理的web请求响应数量 3.响应时间所有web请求处理完毕的时间 4.吞吐 ...
- 05、Windows Store app 的图片裁切(更新)
在 Win Phone Silverlight api 中,有一个 PhotoChooserTask 选择器,指定宽.高属性,在选择图片的时候, 可以进行裁切,代码: PhotoChooserTask ...
- Qt禁止调整窗口的大小
项目中使用的是基于对话框的程序,所以限制调整窗口大小会比较合适,下面是两种方法. 1.使用代码修改 #include "dialog.h" #include "ui_di ...
- MySQL 5.7.16 zip包配置
截止2016/10/16 最新版本Mysql为5.7.16,之前写过一篇APMW搭建的文章(传送门:http://www.cnblogs.com/airoot/p/4131906.html)里面介绍的 ...
- 使用JSTL的sql:query标签制作分页查询遇到NoSuchFieldError: deferredExpression
参考:http://hi.baidu.com/desyle/item/4fe650265792d7182a0f1c33 症状: 如题所述,代码如下 <sql:query var="re ...
- django 判断用户是否登陆
基于类的视图登陆
- cocos2dx中CCTableView乱位问题歪解
可能是引擎作者没有考虑到CCTableView里cell还会改变的需求,结果改变了 cell后其它的cell也跟着改变了.于是在网上查了一下,发现没有人遇到我的 问题,看来我总是遇到奇葩问题,不过也找 ...
- Codeforces 482C Game with Strings(dp+概率)
题目链接:Codeforces 482C Game with Strings 题目大意:给定N个字符串,如今从中选定一个字符串为答案串,你不知道答案串是哪个.可是能够通过询问来确定, 每次询问一个位置 ...
- mysql client中使用帮助命令
当前MySQL服务器的版本号 使用那个命令来参看MySQL的帮助信息 帮助主题供我们查看. 命令为: ? contents 例如查看max方法的使用方法则输入? max即可 这个在navcat中是不支 ...