开发者

why do we need to store sessions in a database sometimes?

开发者 https://www.devze.com 2023-01-05 06:12 出处:网络
I was told that one common reason of storing sessions in a database is to make it cross-server. But isn\'t a TCP connection p开发者_开发知识库ersistent until one closes the browser? Why the next reque

I was told that one common reason of storing sessions in a database is to make it cross-server. But isn't a TCP connection p开发者_开发知识库ersistent until one closes the browser? Why the next request may switch a different server?


But isn't a TCP connection persistent until one closes the browser? Why the next request may switch a different server?

Once the web page loads, and all the images, css files and other assets are retrieved, the TCP connection will close shortly afterwards (after 5 seconds by default in Apache 2.2, for example). When the user clicks on some other internal link, a new TCP connection is opened. This may end up on another server in a multi-server, load-balanced scenario.


You store sessions in a database so that when you load balance your application, the load balancer can forward your request to any server in the farm and still have correct access to your session data.

A single request to a web page will go to a single server. The next time that a user makes a request, a new TCP connection is created and can be sent to a different server in the farm.


Page loads in a browser are normally stateless (i.e., they don't store state once the page has fully loaded).

That means that your next page load, which may be as simple as going from the shopping catalogue page to the shopping cart page, is a separate HTTP "transaction".

In fact, it's possible that the text of a page and its 12 images (and 400 porn ads) may each go to a totally separate server (even if they use the same base URL) since they may operate on different TCP sessions.

You don't want to serialise all the traffic so that you can re-use a single TCP session, it's usually faster to do them in parallel on separate sessions.

So it may end up at a totally different server, yes. In that case, you would want the state stored in a shared resource of some sort (like a database).

There's other ways to get around the problem (there always are) such as IP-based affinity where traffic from the same IP address will prefer the same server but that sort of flies in the face of all your lovely load balancing architecture :-)

0

精彩评论

暂无评论...
验证码 换一张
取 消