Home » Nodejs » Node.js child processes on Heroku with single Dyno

Node.js child processes on Heroku with single Dyno

Posted by: admin November 30, 2017 Leave a comment

Questions:

Pardon my ignorance, I’m currently learning how to use Node coming from a PHP background where I really had no interaction with Apache or server management. I’m using Heroku to host my Node projects, pushed straight from the Cloud9 IDE.

From what I’ve read, a Heroku dyno is a single web process, buying additional dynos will allow you to deal with more traffic coming in because by increasing dynos you increase the size of the amount of requests that you can deal with at any one time.

I know the Node is a single-threaded system that deals with requests one by one, allowing you to spawn child processes for anything that may take some time to deal with (like database requests, processing files etc).

So what happens in Heroku if I spawn a child process with a single dyno? Does this not need another dyno to work on? Surely if Node is running a single process and I only have one process available in my single dyno any extra processes are going to have to be handled by that one too?

Or do I have this all wrong?

Answers:

Your understanding of exec is roughly correct, my concern is that you are using it when you do not need to do so… In your question you only mention I/O types of oeprations, which node handles very efficiently within it’s single threaded event model of doing things. Despite being single threaded, the event model allows code to run without blocking the main event loop(unless you’re doing VERY CPU intensive operations…, of which Database requests, and file processing are not included) That being said, you should not need to launch an additional dyno to do what you want.

Think of a dyno as a single processor computer. Whatever you can do on a machine that has a single processor you can do on your dyno, with no extra charges or created dynos. Though, a dyno does have considerably less memory than a single core processor computer could take advantage of. So, any subprocesses you wish to spawn do not need another dyno to run on. Every master process you wish to run, will need its own dyno.

var http = require('http');

http.createServer(function (req, res) {

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('An amount of data that will take 1 second to send');//This will not block the event loop
}).listen(1337, '127.0.0.1');

var http = require('http');
http.createServer(function (req, res) {

    while(true) {
         break after 1 second; //this will block the event loop for 1 second
    }

    res.writeHead(200, {'Content-Type': 'text/plain'});
    res.end('Hello World\n');
}).listen(1337, '127.0.0.1');

Consider the two servers in the code above. Both servers have roughly one second of work to do. The first example can service thousands of requests per second, the second example only 1. Sending data, database requests, server requests, file IO, etc… all would behave as the first example… very few things in Node behave like the second example. If you have something that fits under the second example, you are likely better off picking a different language, than trying to force node to work for a use case it is very poorly designed for.