How to set a global variable when I use cluster?


I'm working in app with express and

When I started to develop in this app there are not many information about cluster. I'm trying implement cluster but I have some problems with this.

I think that the most problems I can resolve with global object that I can access from any cluster.

So, the questions are two:

  1. There are way that I could create a global object that I could access to the values from any cluster?
  2. There are way that I could modify any values of global object and get this value in another cluster?

Thank you!


So there can be two approaches

  1. External storage : In cluster mode the multiple processes act like clusters and I would not recommend sharing a global object and managing the sessions to keep these objects in sync between the processes. I would recommend using REDIS or any similar database (recommend REDIS as it quick and suites this use case)
  2. Global objects : Yes, you can create/modify the objects as it will be a copy of the object in each process and you will have to keep the objects in sync. you can use process.send() for process and worker.send() for workers from master process. Another option that I see are using libraries for the same : memored npm module or StrongLoops strong store cluster

I would prefer keeping all the process independent and use a common external storage, but again you should make the decision based on your use case.

By : Mattony

This video can help you solving your question :)
By: admin