简体   繁体   English

在应用程序服务器或数据库中维护状态?

[英]Maintaining state in the application server or in the database?

REST advocates web applications without client state on the server. REST提倡在服务器上没有客户端状态的Web应用程序。 The famous shopping cart example is translated to a resource which typically resides in a database. 著名的购物车示例被转换为通常位于数据库中的资源。

I wonder if it is a good practise to use a database for that kind of data, since the database is already the bottleneck in many applications. 我想知道对这种数据使用数据库是否是一个好习惯,因为数据库已经成为许多应用程序的瓶颈。 Wouldn't it be better to use a stateful enterprise java bean instead? 改用有状态的企业Java Bean会更好吗? Application servers are designed with clustring in mind. 应用程序服务器在设计时就考虑了字符串。

What are the advantages and disadvantages of the two approaches? 两种方法的优缺点是什么?

Storing sessions on the application server will only work if: 仅在以下情况下,才能在应用程序服务器上存储会话:

  • Your clients always connect to the same application server (aka "session affinity") 您的客户端始终连接到同一应用程序服务器(也称为“会话关联性”)
  • Your application cluster nodes all use a common mount point (nfs, etc.) to spool sessions 您的应用程序集群节点都使用公共安装点(nfs等)来假脱机会话

Storing sessions in a central database and/or EJB will work if your clients are not guaranteed to always connect to the same application node in your cluster. 如果不能保证客户端始终连接到群集中的同一应用程序节点,则可以在中央数据库和/或EJB中存储会话。

Another approach to consider is using a service such as memcached . 要考虑的另一种方法是使用诸如memcached之类的服务。 This will work in either case. 无论哪种情况,这都行得通。

In general: 一般来说:

Database 数据库

  • more reliable and will survive an app/server restart 更可靠,并且可以在应用程序/服务器重启后继续运行
  • can be shared across load-balanced servers without having to deal with "sticky" sessions 可以在负载平衡的服务器之间共享,而不必处理“粘性”会话
  • slower to access 存取较慢

In-Memory (non-distributed stateful bean) 内存中(非分布式有状态Bean)

  • Fast storage and retrieval 快速存储和检索
  • Less code 更少的代码
  • Will be lost if the app/server restarts 如果应用/服务器重启,将会丢失

Your choice will be totally dependent on your application requirements and environment. 您的选择将完全取决于您的应用程序要求和环境。 All things being equal, I favor the database solution because of the load balancing and reliability benefits, but this can easily be overkill in many scenarios. 在所有条件都相同的情况下,由于负载平衡和可靠性方面的优势,我偏爱数据库解决方案,但是在许多情况下,这很容易过大。

What happens when your app server dies. 应用服务器死机后会发生什么。 Is the session state still important? 会话状态仍然重要吗? Go for a database. 去一个数据库。

Do you have more session state, then your server can handle for all the concurrent user? 您是否拥有更多的会话状态,然后您的服务器可以为所有并发用户处理? Moving data into a database and pulling only out only what you really need at the moment, might be a solution. 将数据移动到数据库中并仅仅提取当前真正需要的内容可能是一种解决方案。

Is your app clustered? 您的应用程序集群了吗? If so, the central database makes sure the sessiondata is always available. 如果是这样,则中央数据库确保会话数据始终可用。

Otherwise: Just store it in the session. 否则:只需将其存储在会话中即可。

Do you have extrem scaling requirements (like stackoverflow or sites with even more traffic)? 您是否有极端扩展要求(例如stackoverflow或流量更大的站点)? Find a way not to use the database. 找到一种不使用数据库的方法。

It is true, that the database is often the bottleneck. 的确,数据库通常是瓶颈。 But a properly setup database should handle a couple bytes of session data just fine. 但是正确设置的数据库应该可以处理几个字节的会话数据。 More complex data and queries are what costs performance. 更复杂的数据和查询是性能的代价。

All of the other items are good suggestions. 所有其他项目都是好的建议。 One more is this: Don't use Session state if you don't absolutely need it. 另一个是:如果您绝对不需要会话状态,请不要使用它。

Storing session in a database is (generally) a bad idea for a couple of simple reasons: 将会话存储在数据库中(通常)是一个坏主意,原因有两个:

  1. Typical use of session is to keep from having to load common data from the database server multiple times. 会话的典型用法是避免多次从数据库服务器加载公共数据。 If session is stored in the db server then, well, you really haven't accomplished much. 如果会话存储在数据库服务器中,那么您确实还没有完成很多工作。

  2. Session has to be serialized and deserialized for every single page execution. 对于每个单页执行,必须对会话进行序列化和反序列化。 This means that the session data would have to be retrieved from the server, and written back to the server every single page load regardless of whether you use it or not. 这意味着必须从服务器中检索会话数据,并且无论是否使用它,每个单页加载都将其写回到服务器。

In my experience you are much better served to simply pull the data from your database server when you actually need it. 以我的经验,最好在实际需要时从数据库服务器中提取数据。 In most cases people put all sorts of short lived data in session simply because they think they are solving a performance problem, when in reality they are making it worse. 在大多数情况下,人们将各种短期数据放入会话中只是因为他们认为自己正在解决性能问题,而实际上却使情况变得更糟。

Further, if you limit the amount of data down (say to a user id, name, or something like that), then you can store that in an encrypted cookie client side and not have to worry about it at all. 此外,如果您减少数据量(例如限制为用户ID,名称或类似名称),则可以将其存储在加密的Cookie客户端中,而不必担心。

MemCache is one option; MemCache是​​一种选择。 but again I'd seriously look at your database usage and see if you can performance tune the queries and schemas first. 但我会再次认真考虑您的数据库使用情况,看看是否可以先对查询和模式进行性能调整。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM