How to make full use of multi-core CPU in node.js

How to make full use of multi-core CPU in node.js

Overview

Nodejs is built based on the V8 engine of the Chrome browser, which means that its model is similar to that of the browser. Our JavaScript will run on a single thread in a single process.

However, the single-process and single-thread structure of the V8 engine is not a perfect structure. Nowadays, CPUs are basically multi-core. Real servers often have several CPUs (like our online physical machine has 12 cores), so this will raise a question in the actual application of Nodejs: "How to make full use of multi-core CPU servers?"

Strictly speaking, Node is not a true single-threaded architecture, because Node itself has I/O threads (network I/O, disk I/O). These I/O threads are handled by the lower-level libuv, which is transparent to JavaScript developers. JavaScript code always runs on V8 and is single-threaded. So on the surface NodeJS is single-threaded.

How to make full use of multi-core CPU in node.js

Faced with the problem of low multi-core utilization of single-process and single-thread, according to previous experience, each process can use one CPU to realize the utilization of multi-core CPUs. Node provides the child_process module and also provides the fork() method to implement process replication (as long as the process is replicated, certain resources and time are required. Node requires no less than 10M of memory and no less than 30ms to replicate the process).

Such a solution is the most classic Master-Worker mode on *nix systems, also known as master-slave mode.

This distributed architecture of a typical parallel processing business model has good scalability (Scalability is actually discussed together with parallel algorithms and parallel computer architecture. The scalability of an algorithm on a certain machine reflects whether the algorithm can effectively utilize the increasing number of CPUs.) and stability.

The main process is not responsible for specific business processing, but is responsible for scheduling and managing the working process. The working process is responsible for specific business processing, so the stability of the working process is what developers need to pay attention to.

The process copied by fork() is an independent process, which has an independent and brand new V8 instance. Although Node provides fork() to copy the process so that each CPU core can be used, it is still important to remember that the fork() process is very expensive. Fortunately, Node can handle large concurrent requests on a single thread through event-driven.

Note: Starting multiple processes here is just to make full use of CPU resources, not to solve concurrency problems.

4 ways to create child processes in Node

1. spawn()

Create a subprocess to execute the command

2. exec()

Create a child process to execute the command. The difference from spawn() is that the method parameters are different. It can pass in a callback function to get the status of the child process.

3. execFile()

Starts a subprocess to execute the specified file. Note that the SHEBANG symbol (#!) must be declared at the top of the file to specify the process type.

4. fork()

Similar to spawn(), the difference is that it only needs to execute the JavaScript file module to create a Node child process.

Note: The following three methods are extended applications of spawn().

The above is the details of how node.js can make full use of multi-core CPUs. For more information about how node.js can make full use of multi-core CPUs, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • A brief introduction to the front-end progressive framework VUE
  • Detailed explanation of vuex progressive tutorial example code
  • Spring Boot Web Application Configuration Detailed Explanation
  • Sample code for creating a web application using Spring Boot
  • NodeJs high memory usage troubleshooting actual combat record
  • Detailed explanation of using Nodejs built-in encryption module to achieve peer-to-peer encryption and decryption
  • Implementation of built-in modules and custom modules in Node.js
  • Summary of some tips for bypassing nodejs code execution
  • How to Develop a Progressive Web App (PWA)

<<:  How to quickly build a LAMP environment on CentOS platform

>>:  Detailed explanation of Apache website service configuration based on Linux

Recommend

Practical record of vue using echarts word cloud chart

echarts word cloud is an extension of echarts htt...

Download MySQL 5.7 and detailed installation diagram for MySql on Mac

1. Enter the following address in the browser htt...

Solve the problem of not finding NULL from set operation to mysql not like

An interesting discovery: There is a table with a...

CentOS7.5 installation of MySQL8.0.19 tutorial detailed instructions

1. Introduction This article does not have screen...

JavaScript generates random graphics by clicking

This article shares the specific code of javascri...

Mysql delete duplicate data to keep the smallest id solution

Search online to delete duplicate data and keep t...

Optimization methods when Mysql occupies too high CPU (must read)

When Mysql occupies too much CPU, where should we...

Mysql 8.0 installation and password reset issues

Mysql 8.0 installation problems and password rese...

Detailed explanation of MySQL and Spring's autocommit

1 MySQL autocommit settings MySQL automatically c...

Detailed explanation of mysql deadlock checking and deadlock removal examples

1. Query process show processlist 2. Query the co...

An article teaches you how to use Vue's watch listener

Table of contents Listener watch Format Set up th...