Chetan Chauhan
Chetan Chauhan

Reputation: 23

Nodejs Child Process on another server using server to server communication

I want to run the child process using node js from one server to another. I have a too heavy process to run that causing my main server to work slowly so I want to run my heavy processes on another server that will perform heavy tasks like data modifications and return a buffer of that data but I could not find similar like this.

For example, I have server A that is running my website and users are sharing their content using this. When the users' traffic jumps to high my server will get slow because of data like images, videos upload, and pdf report generating on the basic images, videos and serving the site content. I want to perform these tasks on server B, so that server A will only work for data serving and traffic management.

Upvotes: 0

Views: 354

Answers (1)

ojosilva
ojosilva

Reputation: 2094

Apparently at this point you probably need to split your webserver frontend routes into different worker servers.

Let's suppose you're using Nginx as a website frontend. If you're not, then your first step would be to setup an nginx webfront.

1 - If haven't done so, serve all public static content (like pdf files, videos, images, etc.) directly from nginx using different rules for static content and node server routes:

Something as basic as this:

server {
    listen 80;
    server_name example.com;

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://127.0.0.1:5000;
    }

    location /static {
        root /dir/to/my/static/files; # here you have your videos, images, etc.
    }
}

2 - Now, if you need to separate your node server onto 2 services, you can just create 2 (or more) nginx proxy rules:

server {
    listen 80;
    server_name example.com;

    location /api {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://127.0.0.2:5000;  # server 2
    }

    location / {
        proxy_set_header   X-Forwarded-For $remote_addr;
        proxy_set_header   Host $http_host;
        proxy_pass         http://127.0.0.1:5000;  " server 1
    }

    location /static {
        root /dir/to/my/static/files;
    }
}

That way example.com/api/* routes will go to your secondary Node server (on ip 127.0.0.2), example.com/static will be served directly by Nginx at blazing fast speeds, while the non-mapped routes will be served by the default main node server on 127.0.0.1.

There are many ways to setup proxies and optimize Nginx so that it can, for instance, go through a pool of node servers in round-robin fashion, and you can also compress data and use protocols like HTTP/2 to take the load off the slower node-based webserver (ie. Express).

Upvotes: 1

Related Questions