[Node.js Record] First introduction to Node.js, introduction, advantages and disadvantages, usage areas, why learn Node?

Table of contents

Introduction to Nodejs

Why learn Node

Node features

Non-blocking asynchronous io

single thread

event driven

Single thread, disadvantages brought by

The characteristic of Node.js is single-threading, which brings benefits, but it also has disadvantages. The weaknesses of single-threading are as follows:

Disadvantages Solution

Disadvantage 1:

Disadvantage 2:

Disadvantage 3:

Cross-platform

Why Serverless NodeJS server-side development has significant technical advantages

Field of use

Summarize


Introduction to Nodejs

Node.js was released in May 2009 and developed by Ryan Dahl. It is a JavaScript running environment based on the Chrome V8 engine. It uses an event-driven, non-blocking I/O model to allow JavaScript to run on the server-side development platform. It Let JavaScript become a scripting language on par with server-side languages ​​such as PHP, Python, Perl, and Ruby.

It can be understood as a lightweight JSP or PHP environment, which is much more convenient for developing web applications.

Why learn Node

If you have a certain front-end foundation, such as HTML、CSS、JavaScript、jQuery; then, Node.js allows you to quickly transition to a full-stack engineer at the lowest cost.

  1. Low learning cost (mobile terminal, desktop application, front-end and back-end).
  2. Node's development efficiency is very high and it has the ability to build complex systems.
  3. Cross-platform and fast to deploy.
  4. Single thread, strong concurrency.
  5. Low server requirements (suitable for entrepreneurs).
  6. It can solve the IO intensive problem very well.

Node has an active community and a rich module pool, with many ready-made function implementations. In terms of frameworks, there are also mature basic frameworks such as koa and express and secondary packaging frameworks such as egg, which can be selected according to needs and are relatively easy to get started.

NodeCompared with traditional back-end languages ​​(such as PHP, JAVA, etc.), each has its own advantages and disadvantages, and each has different areas of expertise and focus. Therefore, each has its own merits and demand market. Node It gives us a convenient way to carry out back-end development, so everyone should not always say which language is the best.

Node features

Non-blocking asynchronous io

For example, when accessing the database to obtain data, it takes a while. In the traditional single-thread processing mechanism, after executing the code to access the database, the entire thread will pause and wait for the database to return the result before executing the subsequent code. In other words, I/O blocks the execution of the code and greatly reduces the execution efficiency of the program.

Due to the non-blocking mechanism used in Node.jsI/O, after the code that accesses the database is executed, the subsequent code will be executed immediately and the results will be returned to the database. The processing code is placed in the callback function, thus improving the execution efficiency of the program.

When a certainI/O is executed, the thread executing the I/O operation will be notified in the form of an event, and the thread will execute the callback function of this event. In order to handle asynchronousI/O, the thread must have an event loop, constantly checking whether there are unhandled events, and processing them in turn.

In blocking mode, one thread can only handle one task. To improve throughput, multi-threading must be used. In non-blocking mode, a thread is always performing calculation operations, and the CPU core utilization of this thread is always 100%. Therefore, this is a particularly philosophical solution: Rather than having many people idle, it is better for one person to risk his life and work to death.

single thread

In server-side languages ​​such as Java, PHP or .net, a new thread is created for each client connection. Each thread requires approximately 2MB of memory. In other words, theoretically, the maximum number of users that can be connected to a server with 8GB of memory at the same time is about 4,000. In order for a Web application to support more users, the number of servers needs to be increased, and the hardware cost of the Web application will of course increase.

Node.js does not create a new thread for each client connection, but only uses one thread. When a user connects, an internal event is triggered. Through non-blockingI/O、事件驱动机制, the Node.js program is macroscopically parallel. Using Node.js, a server with 8GB of memory can handle more than 40,000 user connections simultaneously.

In addition, the benefits brought by single threading are that the operating system no longer has the time overhead of thread creation and destruction. However, single threading also has many disadvantages, which will be explained in detail in the disadvantages of Node.js, please continue reading.

event driven

In Node.js , the client requests to establish a connection, submit data and other behaviors, which will trigger corresponding events. In Node.js , only one event callback function can be executed at a time, but in the middle of executing an event callback function, other events occur, and other events can be processed instead (for example, again A new user is connected), and then returns to the callback function that continues to execute the original event. This processing mechanism is called the "event loop" mechanism.

Node.js The bottom layer of is C++ (V8 is also written in C++). Nearly half of the underlying code is used for the construction of event queues and callback function queues. Using event-driven to complete server task scheduling is something only a genius can think of. The dance on the tip of the needle, using a thread, takes on the mission of handling a lot of tasks.

Single thread, disadvantages brought by

Not suitable for CPU-intensive applications
Only supports single-core CPU and cannot fully utilize the CPU
Low reliability. Once a certain link of the code crashes, The whole system crashes

The characteristic of Node.js is single-threading, which brings benefits, but it also has disadvantages. The weaknesses of single-threading are as follows:

  1. Unable to take advantage of multi-core CPUs

  2. The error will cause the entire application to exit and cannot continue to call asynchronously.I/O

  3. A large amount of calculations occupy the CPU, making it impossible to continue calling asynchronous calls.I/O

The above problems can be solved using some frameworks.

Disadvantages Solution

Disadvantage 1:

  • (1) Some management tools such as pm2,forever can create multiple processes to solve the problem of multi-core CPU utilization.

  • (2) Before version v0.8, multiple processes can be usedchild_process

  • (3) After version v0.8, you can use the cluster module to create multiple worker processes through the master-slave mode to solve the multi-core CPU utilization problem.

Disadvantage 2:

  • (1) Nnigx reverse proxy, load balancing, open multiple processes, bind multiple ports;

  • (2) Some management tools such aspm2,forever can realize process monitoring, automatic restart on errors, etc.

  • (3) Open multiple processes to listen to the same port and use the cluster module provided by Node;

  • Before (4) appearscluster, you can also use child_process to create multiple child threads to listen to a port.

  • (5) Let me explain here, there are the above solutions, but when writing node back-end code, exception throwingtry catch is particularly necessary.

Disadvantage 3:

  • (1) A large amount of intensive calculations can be split into multiple sub-thread calculations as above

  • But if splitting is not allowed and you want to calculate 1 million big data, Node will indeed be powerless in a single thread. This itself is a drawback of V8's memory limit.

Summary: The above disadvantages are gradually solved with the update of Node version and the emergence of new API modules.

Cross-platform

Initially, Node could only run on the Linux platform. Later, with the development of Node, Microsoft noticed its existence and invested in a team to help Node achieve compatibility with the Windows platform. When the v0.6.0 version was released, Node could already run directly on the Windows platform (Node is implemented based on libuv Cross-platform).

Why Serverless NodeJS server-side development has significant technical advantages

You can refer to this article to comment:Why is Node.js so advantageous as a web backend?

Field of use

  • The first category: user form collection systems, backend management systems, real-time interaction systems, examination systems, networking software, and high-concurrency web applications
  • The second category: multiplayer online games based on web, canvas and other
  • The third category: web-based multi-person real-time chat clients, chat rooms, graphic and text live broadcasts
  • Category 4: Single-page browser applications
  • The fifth category: operating databases and providing json-based APIs for front-end and mobile terminals

Summary: Nodejs can implement almost any application, just consider whether it is suitable to use it

Summarize

Try to learn and see if you can go further. There are more and more hybrid application developments, and it is difficult to go the native way...

Guess you like

Origin blog.csdn.net/piyangbo/article/details/125797728