Skip to content Skip to sidebar Skip to footer

A Version Of Node.js That Is Multi-threaded?

It's great that node.js is single threaded and follows an event model, but besides that it doesn't help me much. I have alot of cpu-bound tasks and I want each server request to be

Solution 1:

The common solution is to spawn several node.js threads (up to the number of CPU cores) using build-in cluster module or some third-party library.

Handling each request in a separate thread goes against node.js philosophy and nullifies all node.js advantages. There is no point in using node.js for such tasks, because there is a lot of other web servers which will do it much better.

On the other side, node.js offers you a more efficient way of handling web requests. So, instead of spawning a new thread for each request, node.js handles all requests in a single thread (or in a small pool of threads) using 100% of a single CPU core.

You should also keep in mind that node.js was designed for handling I/O bound tasks and can beat any other web server in it. But node.js is not so good at handling CPU bound tasks (e.g. calculations). So, if handling each request requires a lot of calculations then you picked the wrong tool.

Solution 2:

consider using IISNode. From hanselman's blog:

Scalability on multi-core servers. Since node.exe is a single threaded process, it only scales to one CPU core. The iisnode module allows creation of multiple node.exe processes per application and load balances the HTTP traffic between them, therefore enabling full utilization of a server’s CPU capacity without requiring additional infrastructure code from an application developer.

Solution 3:

Probably yes, look e.g. for set of libraries

and this article (March 3, 2014)

Threads and fibers were the question asked since the first Node.js days (at least what Google says seems to indicate that at least to me).

ES6 presumes that many concurrency requirements will be solved by generators and promises so from the future perspective designing your server side around concept of threads may become a problem in the long run.

Google's Dart (next gen web language) models concurrency also as separate entities (isolate) communicating through some simple message passing. See https://www.dartlang.org/docs/spec/latest/dart-language-specification.html chapter Concurrency.

On the contrary the usual model of threads expects some kind of shared memory. Which will not be available in the cluster mode and will not be available (at least soon) in the Dart as well.

So the request for threads may become a fatal architecture design problem from several points of view.

(I'm also used to "thinking threads", just noting that it may be wrong approach in the Node.js universe)

Solution 4:

As xmojmr have mentioned, JXcore can do this.

Did you see this article?: How to turn your existing application into a multithreaded one with a few lines of code!

It runs an http server multi-threaded allowing each request to be handled by any of the threads.

Just run this code:

var http = require('http');
var cnt = 0;

http.createServer(function (req, res) {
    res.writeHead(200, {'Content-Type': 'text/html'});
    res.write("Hello world! " + cnt++);
    res.end(0);
}).listen(2000, '127.0.0.1');

With the following command:

$ jx mt-keep:3 hello.js

And that's it. It runs 3 threads/instances inside of one process. JXcore chooses one of those 3 threads to process each request.

Post a Comment for "A Version Of Node.js That Is Multi-threaded?"