Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to keep variables that share all node processes in node cluster?

Tags:

It seems like all the node woker processes are working as if it is executing a new copy of the same application. But would like to keep some variables that are shared by all node workers (child processes) in node cluster. Is there a simple way to do this?

like image 819
lahiru madhumal Avatar asked Feb 12 '13 05:02

lahiru madhumal


People also ask

Is it possible to cluster multiple node processes?

Node. js applications can be parallelized using cluster modules in order to use the system more efficiently. Running multiple processes at the same time can be done using few lines of code and this makes the migration relatively easy, as Node.

How does Nodejs cluster work?

Node. js runs single threaded programming, which is very memory efficient, but to take advantage of computers multi-core systems, the Cluster module allows you to easily create child processes that each runs on their own single thread, to handle the load.


1 Answers

All worker processes are indeed new copies of your application. Each worker is a full featured process created with child_process.spawn. So no, they don't share variables. And it's probably best this way. If you want to share information between worker processes (typically sessions) you should look into storing these information in a database.

If you're ready to go node all the way, you could use something like dnode to have your workers ask the master process for data.

like image 81
Floby Avatar answered Nov 17 '22 01:11

Floby