Balancing in meteor

Is it possible to use clusters for load balancing in meteor 1.6 ?

something like that:
A single instance of Node.js runs in a single thread. To take advantage of multi-core systems, the user will sometimes want to launch a cluster of Node.js processes to handle the load.

The cluster module allows easy creation of child processes that all share server ports.

https://nodejs.org/api/cluster.html

Can you enter a bit more details? What kind of cluster? Where? Because the short answer is yes, but it depends

My best advice is to use nginx to set up load balancing and routing for multiple app instances across any number of local or remote servers. It’s highly configurable, and pretty great. Check out the docs on load balancing it’s very simple.

Generally speaking since Node is single-threaded, best practice is to run one app instance per CPU/core locally, so you don’t overload your machine.

Ah what the hell, I’ll just upload my nginx.conf template for you guys. Also, disclaimer, this is a bit out of date, can’t know if future Nginx releases won’t break compatibility. This is running on Ubuntu 14.04, btw, and the SSL stuff is completely optional, but I recommend it if you’re running your app on a public IP address.

# one worker per CPU
worker_processes 4;

events {
    worker_connections 1024;
}

http {
    # websockets upgrade mapping (required to make Meteor work properly)
    map $http_upgrade $connection_upgrade {
        default upgrade;
        '' close;
    }

    # load balance 4 instances of node running our app bundle (output of `meteor build`)
    # theoretically, in this section you could load balance any number of servers, remote and/or local
    # but I wouldn't recommend load balancing too much on a server that you're also running apps on locally
    upstream myapp {
        server 127.0.0.1:3000;
        server 127.0.0.1:3001;
        server 127.0.0.1:3002;
        server 127.0.0.1:3003;
    }

    # server endpoint configuration
    server {
        listen                  80;
        server_name             myapp.com;
        # redirect http to https (optional)
        return                  301 https://myapp.com$request_uri;
    }

    server {
        # All the following SSL stuff is optional
        listen                  443 ssl;
        keepalive_timeout       5m;
        server_name             myapp.com;
        ssl_certificate         /path/to/crt/server.crt;
        ssl_certificate_key     /path/to/key/server.key;
        ssl_protocols           TLSv1 TLSv1.1 TLSv1.2;
        ssl_ciphers             AES128-SHA:AES256-SHA:RC4-SHA:DES-CBC3-SHA:RC4-MD5;
        ssl_session_cache       shared:SSL:10m;
        ssl_session_timeout     10m;

        # websockets configuration, using our mapping (required as well, for Meteor)
        location / {
            proxy_pass          http://myapp;
            proxy_http_version  1.1;
            proxy_set_header    Upgrade $http_upgrade;
            proxy_set_header    Connection $connection_upgrade;
        }
    }

    # I do this because I bundle my own fonts
    types {
        application/font-woff2  woff2;
    }
}

2 Likes