Oguz Bastemur: Multithread Node.JS - MoxOpus


While we were developing our new backend using Node.JS, at some point (especially when the server is under the load) we needed forking some expensive tasks to the child processes or implement it internally and access the results asynchronously. Indeed It was all about keeping the backend responsive but wasn't easy because of the limitations with the single threaded development and grooving list of things we need to implement internally .



 
 

Related Links:

Original URL

http://oguzbastemur.blogspot.com/2013/12/multithread-nodejs.html?view=classic

Content

Cached Text (at the time of saving)


While we were developing our new backend using Node.JS, at some point (especially when the server is under the load) we needed forking some expensive tasks to the child processes or implement it internally and access the results asynchronously. Indeed It was all about keeping the backend responsive but wasn't easy because of the limitations with the single threaded development and grooving list of things we need to implement internally . This or that made me investigating/implementing multithreading for Node.JS and finally I got some results to share.
Before everything else I must say that the NodeJS gatekeepers and contributors did a great job and there must be a lots of developers (including me) thankful for the solution.
Long story short, I was developing a proper way to handle the expensive tasks under other threads and recently finished the initial implementation of multithreaded node.js task execution. Below part of code I used for one of the test cases ( testJob1.js )
var t = 0 ; for ( var m= 0 ; m< 10 ; m++){ for ( var i= 0 ; i< 10000000 ; i++){ t+=i% 2 ; } } //callback to the task giver with a JS object task.callback({q:global.id,number:t});
It simply calculates the 't' under a 100 million loop and turns back the result. The 'id' under the global is a variable shared from the original thread below;
var task = require( 'tasks' ); var start = Date.now(); var cc = task.getCPUCount(); for ( var i= 0 ; i One important thing to remind is that the context used for main thread is different than the others. So, there is no 'direct' resource sharing among the threads. The idea is to provide the things needed by the task and get the result when it is done without blocking the main thread. Simply, I'd like to keep the Node.JS's lock free design intact.
Finally the both MT and ST results for the test on (OSX 10.9 - MacBook Pro - i7) (I'm pretty sure that the POSIX scheduler works better on all the other *ix distros, and yes it will work for Windows too)
0-50000000
1-50000000
5-50000000
3-50000000
6-50000000
7-50000000
2-50000000
4-50000000
MT Total 358
0-50000000
1-50000000
2-50000000
3-50000000
4-50000000
5-50000000
6-50000000
7-50000000
ST Total 1551
'Tasks' multithreading feature lets the heavy tasks completed more than 4 times faster on the above system. The time spent for assigning the tasks took aprox. 40 ms and I'm trying to reduce that now. Hopefully this feature will be one of the key features of our beta that we are going to release mid January 2014. If you are interested feel free to contact me and let me know your questions.
Soon there will be more features and developments available from Nubisa website.