@antoninadert, you should not make your success dependent on the whims of a departed ‘guru’. It promotes learned helplessness and stifles your own initiative and creativity which is unhealthy :-).
In your situation, I would use Nginx load balancing with Meteor. Basically, you get Nginx to distribute requests across multiple Meteor (Node) processes.
And how do you create the multiple Meteor processes?
There are a few ways.
The approach I find most efficient involves using Node’s inbuilt cluster module to spawn multiple Meteor worker processes and have each one listen for requests on a separate UNIX socket file (UNIX socket support was added to Meteor last year).
Answers to other expected questions:
- How do you use Node cluster? There are lots of tutorials online, but here is an example of how to spawn three worker processes:
import cluster from 'cluster';
var workerCnt=3;
if (cluster.isMaster) {
for (var i = 0 ; i < workerCnt ; i++) {
var worker = cluster.fork();
worker.on('exit', function() {
clusterLog("Worker id " + worker.id + " exited.");
});
}
}
if (cluster.isWorker) {
console.log("Hello. I am worker number " + cluster.worker.id);
}
Code like this could be included in a Meteor.startup() function.
Note: Node cluster’s fork() method works differently to the Linux/POSIX fork() that many people may have used when programming in other languages - each worker process restarts execution from the very beginning of the Node.js code, and not from the line after the cluster.fork() call.
- Why listen on UNIX socket files instead of TCP ports?
UNIX sockets have less kernel overhead than TCP connections. They also avoid the risk of ephemeral port exhaustion when using Nginx as the proxy, which impedes scalability (one of the common causes of the C10k problem)
Meteor will listen on a socket file when you define the environment variable UNIX_SOCKET_PATH
, e.g. UNIX_SOCKET_PATH=/tmp/meteor.sock
- How do you set a unique value for
UNIX_SOCKET_PATH
in each worker process?
In November 2017, I contributed a patch that gets Node cluster Meteor worker processes to automatically set their own unique UNIX_SOCKET_PATH by appending their worker id to the UNIX_SOCKET_PATH setting.
e.g. the first worker process listens on /tmp/meteor.sock.1.sock
, the second worker process listens on /tmp/meteor.sock.2.sock
etc.
My company is using this Node cluster-based approach in production.
P.S.You can leave a thumbs up for my patch to be incorporated into Meteor or any other feedback here: