how to keep variables that share all node processes in node cluster?
I used external memcached or redis server for it.
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.
You can try to communicate between master process and child processes. For example:
script test.master.js:
var cluster = require('cluster');
var childScript = __dirname + '/test.child.js';
cluster.setupMaster({ exec: childScript });
proc = cluster.fork();
proc.on('message', function(message) {
console.log('message from child: ', message);
proc.send('Hello from master!');
});
script test.child.js:
console.log('Child initializing..');
process.on('message', function(message) {
console.log('message from master: ', message);
});
process.send('Hello from Child!');