How to make NodeJs use More CPU Core or Multithreading Instead Single-thread

11,205

Solution 1

Your question is perfectly valid.

A typical nodejs web server will use only 1 core of a CPU at any given moment.

Take a look at the cluster module

Solution 2

What you need to remember here is that multi-threaded code is not easy to write correctly, and handling concurrency in an application is something very few languages do well. The NodeJS team has chosen not to include a threading implementation probably because JavaScript itself is missing language features that would make this easy, and making NodeJS use a non-standard JavaScript is against the intentions of that project. It's possible that the emerging web workers standard will solve some of these problems.

That being said, from an operating system perspective the distinction between threads and processes is pretty minor. Each process is comprised of one or more threads, each of which compete with other threads on the system for resources according to their priority level.

If you build an application that's highly multi-threaded, or you build one that's multi-process, you will have the same concerns when it comes to running flat-out with any of your threads going to 100% utilization of a CPU core.

The solution in NodeJS is to break down your task into a series of smaller tasks that can be distributed amongst workers either in the same process, as you might with the stream interface, or between processes using some form of inter-process communication.

If you're worried about your server being swamped and grinding to a halt, what you need to do is some up with some sort of rate limiter that will regulate how much work is being done by your Node processes. A worker queue like RabbitMQ is one way to do this.

All this is talking about native NodeJS. If you're willing to mix in some modules, something that's pretty much essential with any serious work, you do have options like the webworker threads package from NPM that might do what you need.

Solution 3

Mike Gleason is correct with his answer on using the cluster module.

An easier way to "use" that is to use the PM2 app/module to manage your Node processes which manages the clustering for you and allows for graceful restarts etc.

Share:
11,205
azl
Author by

azl

Updated on June 17, 2022

Comments

  • azl
    azl almost 2 years

    Internet said that

    Node is single-threaded and doesn’t automatically make use of more than a single core in your potentially multi-core machine. This means unless you design it differently, your application won’t take full advantage of the available capacity the server hosting it has to offer.from this

    Clear Question ? (updated)

    the simple question, no comparing or scalle trick
    - can NodeJs to use Multi CPU usage - What happen when node use single-threads.?
    - If we fire node multiple process(node1,node2,node3)worker same machine , Making use all of 4 server core can it make server slow ?
    - What happen when node processes, use same cores fighting for CPU Resources?

    I Read this Then Inspired to ask you guys.. :)

    • David Schwartz
      David Schwartz over 9 years
      Not to be rude, but to be honest, it sounds like you don't understand the issues well enough to ask a sensible question.
    • azl
      azl over 9 years
      can you explain me.. what is single thread mean.?
    • David Schwartz
      David Schwartz over 9 years
      Punch something like "single thread" performance into your favorite search engine. "Explain these concepts to me" is a terrible question because it's not specific, and there are tons of web pages that explain concepts and you can pick the one that best focuses on what you need to know and that you find most understandable.
    • Mike 'Pomax' Kamermans
      Mike 'Pomax' Kamermans over 9 years
      Why do you need node.js to do more? Are you somehow hitting its performance cap? If not, this is not a question you should be asking.
    • azl
      azl over 9 years
      thanks..david Schartz but, the issue not comparing single and Mutli threaded performance, just simple question, how node functional programing use child-processing.. in nodejs documentation cluster
    • azl
      azl over 9 years
      to mike thanks to comments.. just curious this docs..http://nodejs.org/api/cluster.html, I thinks we miss preceptions guys..
  • azl
    azl over 9 years
    thank you.. :) you perfectly answer my questions.. 1. nodeJs can do that 2. making It 100%.. 3. according to priority level I Would Learn more technical approach to doing this.. start by your suggestion's: - creating web worker in nodeJS - learning Stream Interface - worker queue - webworker threads package .. sounds this is hard work..