Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In node.js, how to declare a shared variable that can be initialized by master process and accessed by worker processes?

I want the following

  • During startup, the master process loads a large table from file and saves it into a shared variable. The table has 9 columns and 12 million rows, 432MB in size.
  • The worker processes run HTTP server, accepting real-time queries against the large table.

Here is my code, which obviously does not achieve my goal.

var my_shared_var; var cluster = require('cluster'); var numCPUs = require('os').cpus().length;  if (cluster.isMaster) {   // Load a large table from file and save it into my_shared_var,   // hoping the worker processes can access to this shared variable,   // so that the worker processes do not need to reload the table from file.   // The loading typically takes 15 seconds.   my_shared_var = load('path_to_my_large_table');    // Fork worker processes   for (var i = 0; i < numCPUs; i++) {     cluster.fork();   } } else {   // The following line of code actually outputs "undefined".   // It seems each process has its own copy of my_shared_var.   console.log(my_shared_var);    // Then perform query against my_shared_var.   // The query should be performed by worker processes,   // otherwise the master process will become bottleneck   var result = query(my_shared_var); } 

I have tried saving the large table into MongoDB so that each process can easily access to the data. But the table size is so huge that it takes MongoDB about 10 seconds to complete my query even with an index. This is too slow and not acceptable for my real-time application. I have also tried Redis, which holds data in memory. But Redis is a key-value store and my data is a table. I also wrote a C++ program to load the data into memory, and the query took less than 1 second, so I want to emulate this in node.js.

like image 713
Jacky Lee Avatar asked Jun 09 '12 23:06

Jacky Lee


People also ask

How do you declare a variable in node JS?

To define a global variable in NodeJS we need to use the global namespace object, global . It's important to be aware that if you do not declare a variable using one of the keywords var , let or const in your codebase then the variable is given a global scope.

How do I declare a global variable in node JS?

To set up a global variable, we need to create it on the global object. The global object is what gives us the scope of the entire project, rather than just the file (module) the variable was created in. In the code block below, we create a global variable called globalString and we give it a value.

Can you explain globals in node JS?

Node. js global objects are global in nature and they are available in all modules. We do not need to include these objects in our application, rather we can use them directly. These objects are modules, functions, strings and object itself as explained below.


1 Answers

If I translate your question in a few words, you need to share data of MASTER entity with WORKER entity. It can be done very easily using events:

From Master to worker:

worker.send({json data});    // In Master part  process.on('message', yourCallbackFunc(jsonData));    // In Worker part 

From Worker to Master:

process.send({json data});   // In Worker part  worker.on('message', yourCallbackFunc(jsonData));    // In Master part 

I hope this way you can send and receive data bidirectionally. Please mark it as answer if you find it useful so that other users can also find the answer. Thanks

like image 180
Shivam Avatar answered Sep 20 '22 21:09

Shivam