Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does using stateful web servers make sense?

I am working on a web application, which historically was built on a PHP/MySQL stack.

One of they key operations of the application had to do some heavy calculations which required iterating over every row of an entire DB table. Needless to say this was a serious bottleneck. So a decision was made to rewrite the whole process in Java.

This gave us two benefits. One was that Java, as a language, was much faster than a PHP process. The second one was that we could maintain the entire data set in the Java application server memory. So now we can do the calculation-heavy operations in memory, and everything happens much faster.

This worked for a while, until we realized we need to scale, so we now need more web servers.

Problem is - by current design, they all must maintain the exact same state. They all query the DB, process the data, and maintain it in memory. But what happens when you need to change this data? How do all the servers maintain consistency?

This architecture seems flawed to me. The performance benefit from holding all the data in memory is obvious, but this seriously hampers scalability.

What are the options from here? Switch to a in-memory, key-value, data store? Should we give up holding state inside the web servers entirely?

like image 374
Yuval Adam Avatar asked Dec 30 '10 13:12

Yuval Adam


People also ask

Is stateful better than stateless?

A. In most cases, stateless is a better option when compared with stateful. However, in the end, it all comes down to your requirements. If you only require information in a transient, rapid, and temporary manner, stateless is the way to go.

Are web servers stateful?

Stored Data: If the webserver stores data in a backend manner and uses it to identify the user as an always-connected client, the service is Stateful. While in Stateless, the server does store data, but in a database to verify user/client whenever it needs to connect.

Why do we want stateless servers?

It helps in achieving a better response time with experience. Design: This is regarded as an advantage for developers as the stateless servers are easy to design and code because there is no need to dynamically maintain storage units in the backend.

Is the Internet stateful or stateless?

In formal protocol specifications, a finite state machine is an abstract desciption of how a stateful system works that describes the action that follows each possible state. The Internet (including the World Wide Web) can be thought of as a stateless system or machine.


2 Answers

now switch to Erlang :-)

yeah, that's a joke; but there's a grain of truth. the issue is: you originally had your state in an external, shared repository: the DB. now you have it (partially) precalculated in an internal non-shared repository: Java RAM objects. The obvious way is to have it still precalculated but in an external shared repository, the faster the better.

One easy answer is memcached.

Another is to build your own 'calc server', which centralizes both the calculation task and the (partial) results. The web frontend processes just access this server. In Erlang it would be the natural way to do it. In other languages, you sill can do it, just more work. Check ZeroMQ for inspiration, even if you don't use it in the end (but it's a damn good implementation).

like image 64
Javier Avatar answered Oct 05 '22 08:10

Javier


This may be cliche, but data always expands to fill the space you put it in. Your data might all fit in memory today but I guarantee you it won't at some time in the future. How far away that is is the time-frame you have to figure out a better architecture. The statefulness of your application is just a symptom of this bigger problem.

Does everyone do different calculations on the entire dataset? Is this something you can do in a batch overnight and have folks access during the day? How time-sensitive is it?

I think these are the questions you need to answer becuase at some point you won't be able to buy enough memeory to store the data you need. That might sound silly given where you are now, but you should plan on that being true. Many developers I've talked to don't think about what success looks like and what impact it has on their designs.

like image 35
n8wrl Avatar answered Oct 05 '22 07:10

n8wrl