Home >Web Front-end >JS Tutorial >What scenarios can Nodejs be applied to?

What scenarios can Nodejs be applied to?

青灯夜游
青灯夜游forward
2021-05-28 10:08:362514browse

This article will introduce you to some application scenarios of Nodejs. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to everyone.

What scenarios can Nodejs be applied to?

##Apache’s multi-threaded high concurrency mode

Advantages: Supports multi-thread concurrency

Disadvantages: blocking

What is a thread

A thread is the smallest CPU unit that can run independently and can run concurrently in the same process Run and share the memory address space under the process.

When different threads need to occupy the same variable, according to the first-come-first-served principle, while the thread that arrives first is running, the subsequent thread can only wait aside, that is, it joins the blocking queue sequence. This causes thread blocking.

Similar scenario: Bank counter handling business

NodeJS’s asynchronous I/O principle

Advantages: high concurrency (most Important advantages), suitable for I/O-intensive applications

Disadvantages:

  • Not suitable for CPU-intensive applications (the main challenges that CPU-intensive applications bring to Node Yes: Due to the single-thread nature of JavaScript, if there are long-running calculations (such as large loops), the CPU time slice will not be released, making subsequent I/O unable to be initiated)

  • Only supports single-core CPU, cannot fully utilize the CPU

  • Low reliability, once a certain link of the code crashes, the entire system crashes

    Reason: single process, single thread
    Solution:
    (1) Nnigx reverse proxy, load balancing, open multiple processes, bind multiple ports;
    (2) Open multiple processes to listen to the same port, use the cluster module
    (3) Use PM2 to manage the process online, and automatically restart the project if there is a problem

  • Debug is inconvenient, and there is no stack trace for errors

NodeJS is event-based Loop, each piece of NodeJS logic is written in the callback function, and the callback function is executed asynchronously after returning. [Recommended study: "

nodejs Tutorial"]

NodeJS is not blocking, but blocking does not occur in the subsequent callback process, but occurs in the calculation and processing of logic by NodeJS itself.

NodeJS All its I/O, network communication and other time-consuming operations can be handed over to worker threads for execution and callback, so it is very fast. But for normal operation of the CPU, it can only operate by itself.

Similar scenario: Queue to order and then call your number to pick up the food.

Application scenarios of NodeJS

NodeJS has strong ability to handle concurrency, but its ability to handle calculation and logic is very weak.

Therefore, we move complex logical operations to the front end (client) to complete, and NodeJS only needs to provide asynchronous I/O, so that high-performance processing of high concurrency can be achieved.

RESTful API

This is the most ideal application scenario for NodeJS. It can handle tens of thousands of connections. It does not have much logic itself, just You only need to request the API, organize the data and return it.

It essentially just looks up some values ​​from some database and composes them into a response.

Since responses are small amounts of text and inbound requests are small amounts of text, the traffic is not high and a single machine can handle the API needs of even the busiest companies.

Applications with a large number of Ajax requests

Real-time chat, single-page APP with strong client logic, specific examples include: localized Online music applications, localized online search applications, localized online APPs, etc.

Apache Applicable Scenarios

Apache due to its multi-threaded high concurrency shared memory address space characteristics, which means that if the server is powerful enough, If the processor has high enough cores, Apache will operate very well, so it is suitable for applications with relatively few (concurrent) asynchronous processing, large amount of background calculations, and complex background business logic.

For more programming related knowledge, please visit:

Programming Video! !

The above is the detailed content of What scenarios can Nodejs be applied to?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete