preface
Last time, we talked about node. js memory model. This time, we shared another Node model, concurrent model.
What is the concurrency model
First of all, what is concurrency?
Concurrency refers to the ability of a program to handle multiple tasks at the same time, which is an essential capability of a Web service.
Since Nodejs appeared, JS has been involved in the back-end field. Because of its excellent concurrency model, IT is used by many enterprises to deal with high concurrency requests. For example, Taobao has been using Node to deal with middle-layer business in a large number of cases.
This article will examine the concurrency model of JS to understand node’s advantages over other languages and its most appropriate application scenarios
Tips: The difference between concurrency and parallelism
Asynchronous I/o
What is asynchronous IO?
- How is asynchronous IO implemented?
- What’s the difference between asynchronous and synchronous?
- Does asynchrony not block? What about IO blocking?
With these questions in mind, let’s take our time.
IO model
UNIX Network Programming: Volume 1. Chapter 6 — I/O multiplexing. There are five TYPES of I/O models available under UNIX:
- Blocking I/O;
- Non-blocking I/O;
- I/O multiplexing (SELECT, poll, epoll…) ;
- Signal-driven I/O (SIGIO);
- Asynchronous I/O (POSIX AIO_ series of functions);
To summarize blocking, non-blocking, synchronous, asynchronous:
- Blocking, non-blocking: whether the process/thread is ready to access the data, whether the process/thread needs to wait;
- Synchronous and asynchronous: data access methods. Synchronization requires active data reading and writing, but still blocks data reading and writing. Asynchronous only requires notification of I/O completion and does not actively read and write data. The operating system kernel reads and writes data.
People speaking
The above explanation is too complicated for me to understand what to do? We substitute the IO mentioned above into the life scene. Considering that many people in our company like to buy a little drink, we take the beverage purchase as an example and convert the four common IO models into the corresponding beverage purchase process. Here are the Settings:
- Simplify the process of buying a little glass into two steps: order it and take a little back to the office
- Company employee === thread
- Place an ORDER === Initiate an I/O request
- Take a little bit back to the company === read the data
The characteristic of asynchronous IO is that the work of THE IO is handed over to the operating system, so that the thread is not blocked by the IO and can continue to process other requests.
Node asynchronous I/O
Node.js asynchronous IO is implemented by the Libuv library. Libuv is a key component of Node.js. It provides uniform API calls for the upper layer of Node.js, regardless of the platform gap, and hides the underlying implementation.
Libuv also has a thread pool in the bottom right corner. In fact, the IO requests received by Libuv are implemented by the same thread. It seems that Node is just a single thread at the program level.
Event loop
Task queue
Take a look at the node.js structure first:
According to the diagram above, Node.js works as follows:
(1) V8 engine parses JavaScript scripts.
(2) Parsed code, call Node API.
(3) Libuv library is responsible for the execution of Node API. It assigns different tasks to different threads, forming an Event Loop that asynchronously returns the execution results of the tasks to the V8 engine (callback processing results).
(4) The V8 engine returns the result to the user.
Asynchronous operations are pushed into the task queue, and when the task queue is empty, the program exits.
conclusion
The efficient I/O processing provided by Libuv’s event loop mechanism with asynchronous I/O + thread pools is the main reason Node can handle high concurrency requests.
Refer to articles and books
- Node.js
- Unix Network Programming
- 7 Weeks 7 Concurrent Model
- The difference between concurrency and parallelism
- More on the Event Loop
- How to understand the difference between blocking non-blocking and synchronous asynchronous? – Dayao’s answer – Zhihu
- Node.js Inside: Getting to know single-threaded Node.js
- Linux IO mode and select, poll, epoll details
Node-js-concurrent model is a model that can be used to create parallel nodes. It is a model that can be used to create parallel nodes.