-1

According to documentation from PM: https://pm2.io/docs/runtime/guide/load-balancing/ To make use of its load balancing feature which uses node's clustering to spawn multiple node processes, I need a stateless nodejs application.

In the context of clustering, you first need to be sure that your application has no internal state. An internal state is typically some local data stored into its processes. It can be an array of websocket connections or a local session-memory for example. Use Redis or other databases instead to share the states between processes.

How do I know if my nodejs application is stateless or not?

3
  • Sharing your research helps everyone. Tell us what you've tried and why it didn't meet your needs. This demonstrates that you've taken the time to try to help yourself, it saves us from reiterating obvious answers, and most of all it helps you get a more specific and relevant answer. Also see How to Ask – gnat Mar 29 at 15:26
  • So, I wanted to learn how to handle CPU intensive tasks in nodejs. I stumbled upon this article blog.appsignal.com/2021/02/03/…. I went through it and tested it, it seemed promising. But then I looked up pm2's documentation and it mentioned my nodejs app had to be stateless. I have used socket, in my previous nodejs application. I wanted to know how using socket or "local session-memory" made a nodejs app "stateful". – jeff Mar 29 at 15:31
  • well, did you put state in it? – user253751 Mar 29 at 17:13
2

You may want to check first what defines a stateless application. In essence, a stateless web application, as opposed to a stateful web application, doesn't keep its information between multiple requests in memory on the servers, but relies instead on the database, an external cache system, etc.

What's the problem with stateful applications? Imagine a blog engine which relies on in-memory cache in order to store the generated pages, one cache entry per article. When somebody changes an article, the cache is invalidated. It works well if you host the blog on one server. If, on the other hand, you start having multiple servers, then the cache invalidation may go to one of those servers, while the other ones would continue serving obsolete version of the page—some of the visitors will see the new article, and some other would still access the old one.

State, therefore, prevents an application from being hosted on multiple machines (or even multiple processes in some cases), and being stuck with a single process on a single machine prevents to scale the application. With the increasing number of users, you'll need more powerful server to host the app, and at some point, you won't be able to cope with the load.

How do I know if my nodejs application is stateless or not?

If you developed the application, you should know it already, now that you understand what stateless means.

If you didn't, study the code. Check how the variables are used. Are there global variables? Something kept between the requests?

A good starting point is to check the following parts:

  • Authentication.
  • Session.
  • Cache.

This is where you find state most often. If you don't see any stateful behavior here, this doesn't necessarily mean that the application is stateless. If it wasn't written with statelessness in mind, chances are, it isn't. One nice way is to actually host it on several nodes, and see what's happens. It never gives you the definitive answer as to whether it's stateless or not, but sometimes it becomes very clear very quickly that state is involved.

I wanted to know how using socket or "local session-memory" made a nodejs app "stateful"

If by local session memory, you mean actual global variables in your JavaScript code, then yes, the application is stateful.

As for the sockets, I'm not sure if you talk about the actual sockets, or if you mistyped WebSocket protocol. In the case of the sockets, it has nothing to do with state. In the case of WebSocket, it all depends on the way the message queue is implemented. You can have a fully stateless implementation which relies on a message queue service or Redis pub-sub, as you can have an ersatz of a queue in a form of a global variable, which implies that the app is stateful.

1
  • thanks a lot. This helped a lot. Have a good day sir – jeff Mar 29 at 16:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.