Skip to content

gottsohn/multi-core-node-js-example

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

69 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Running Node JS (with Express) on Multiple Threads

Code Climate Test Coverage Circle CI Build Status

This is a simple example showing how to make NodeJS utilize your processor threads.

The example makes use of an simple Express instance for HTTP serving.

The express application can be found in ./server/app.js, the NodeJS app useing cluster to run the web server on multiple threads is the index.js script.

Have a look at both scripts, the comments are sure to help.

Here's a blog post containing a stress test experiment with single and multi-threaded NodeJS instances.

NOTE For Heroku Apps you'll want to use a ratio of the threads you are provided with else you run the risk of exceeding the Memory Limit designated to your application. See below a quick example of how you could manage this with Heroku.

In line 8 of index.js you should have

let numCPUs = require('os').cpus().length / (parseInt(process.env.CLUSTER_DIVIDER, 10) || 1);

Where CLUSTER_DIVIDER is a config variable that determines the divider to be used. For Heroku most packages (Free and Standard 1X), you get 8 threads on a Intel(R) Xeon(R) CPU E5-2670 v2 @ 2.50GHz, so you'll want to use a divider of 2 (meaning you use only 4 threads) to avoid consuming all 512 MB of RAM. If your plan is up to 1 GB RAM then 8 threads will be fine.

You might want to checkout ./tests/main.spec.js and see how we test the multi-threading.

About

A demonstration of Node.js cluster module that help running an instance of your Node.js application on multiple processes (Processor cores).

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published