Pros and cons of NodeJS

We know that NodeJS was developed by Ryan Dahl in May 2009, which is essentially a package of the Chrome V8 engine. Node.js optimizes some special use cases and provides alternative APIs, which makes V8 run better in non-browser environments, solves the problem that JS cannot run on the server, and also provides a front-end engineer to develop a back-end interface. new program.

NodeJS is based on Event Loop (event loop) and CallBack (callback) to maximize the efficiency of single process and single thread (it will make people feel the illusion of multi-threading), so that limited resources (CPU, memory, network resources) can be utilized. maximize.

First, the advantages and disadvantages of NodeJS

Advantages: 1. High concurrency (the most important advantage)

2. Suitable for I/O intensive applications

Disadvantages: 1. Not suitable for CPU-intensive applications; the challenges brought by CPU-intensive applications to Node are mainly: due to the single thread of JavaScript, if there are long-running calculations (such as large loops), it will cause CPU time slices Cannot be released, so that subsequent I/O cannot be initiated;

Solution: Decompose large computing tasks into multiple small tasks, so that operations can be released in a timely manner without blocking the initiation of I/O calls;

2. Only supports single-core CPU and cannot make full use of CPU

3. Low reliability, once a 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 monitor the same port and use the cluster module;

4. The quality of the open source component library is uneven, the update is fast, and it is not backward compatible

5. Debug is inconvenient, and there is no stack trace for errors

2. Scenarios suitable for NodeJS

1. RESTful API

This is the most ideal application scenario of NodeJS, which can handle tens of thousands of connections. There is not much logic in itself. It only needs to request the API and organize the data for return. It essentially just looks up some values ​​from some database and composes them into a response. Since the response is a small amount of text, and the inbound request is also a small amount of text, traffic is not high, and one machine can handle the API needs of even the busiest companies.

2. Unify the UI layer of web applications

The current MVC architecture, in a sense, web development has two UI layers, one is what we finally see in the browser, and the other is on the server side, responsible for generating and splicing pages.

I won't discuss whether this architecture is good or bad, but there is another practice, service-oriented architecture, which better separates front-end and back-end dependencies. If all key business logics are encapsulated into REST calls, it means that the upper layer only needs to consider how to use these REST interfaces to build specific applications. Those back-end programmers don't care about how specific data is passed from one page to another, and they don't care whether user data updates are fetched asynchronously via Ajax or by refreshing the page.

3. Applications with a large number of Ajax requests

For example, in a personalized application, each user sees a different page, the cache is invalid, and an Ajax request needs to be initiated when the page is loaded. NodeJS can respond to a large number of concurrent requests. All in all, NodeJS is suitable for scenarios with high concurrency, I/O intensive, and a small amount of business logic.

3. Ending

In fact, NodeJS can realize almost all applications, the point we consider is whether it is suitable for use.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324668666&siteId=291194637