Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Maintaining state in the application server or in the database?

REST advocates web applications without client state on the server. 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? Application servers are designed with clustring in mind.

What are the advantages and disadvantages of the two approaches?

like image 497
deamon Avatar asked Jan 26 '10 21:01

deamon


People also ask

What is an application server in database?

An application server provides access to the data for the client. It serves as an interface between the client and one or more database servers, which provides an additional level of security. It can also perform some of the query processing for the client, thus removing some of the load from the database server.

What is maintaining the state in PHP?

HTTP is a stateless protocol, which means that once a web server completes a client's request for a web page, the connection between the two goes away. In other words, there is no way for a server to recognize that a sequence of requests all originate from the same client.


2 Answers

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

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.

Another approach to consider is using a service such as memcached. This will work in either case.

like image 192
AJ. Avatar answered Nov 14 '22 23:11

AJ.


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)

  • 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.

like image 28
Dave Swersky Avatar answered Nov 14 '22 23:11

Dave Swersky