This is the second day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
Regular study notes, including ES6, Promise, Node.js, Webpack, HTTP Principles, Vue buckets, and possibly more Typescript, Vue3, and common interview questions.
Basic concepts of Node
What is Node?
Node.js is a JavaScript runtime based on Chrome V8 engine. Node is not a language that allows JavaScript to run at the back end of the runtime. It does not include the complete set of JavaScript because DOM and BOM are not included in the server. Node also provides some new modules such as HTTP and FS modules. Node.js uses an event-driven, non-blocking I/O model that makes it lightweight and efficient, and Node.js’s package manager, NPM, is the largest open source library ecosystem in the world. At this point we have a simple idea of Node.
High concurrency of Node
Node has significant performance advantages in high concurrency, I/O intensive scenarios.
- High concurrency refers to concurrent access to the server at the same time.
- I/O intensive refers to the file operation, network operation, database, the relative CPU intensive, CPU intensive refers to the logical processing operation, compression, decompression, encryption, decryption.
The main scene of Web is to receive requests from clients to read static resources and render interfaces, so Node is very suitable for Web application development.
When it comes to high concurrency, multithreading certainly comes to mind. So what is the relationship and difference between multi-threading and high concurrency?
multithreading
First of all, what is multithreading
Back-end languages (including Java, C++, and so on) have a thread pool that allocates a thread to the server for each request sent, and so on. Because multi-threaded language is characterized by synchronous requests, there may be a case of a single thread blocking when multi-threaded requests are sent. The thread will be released and put back into the thread pool after the task processing of the current thread is completed, so as to facilitate the use of the next batch of tasks. A wait situation may occur when the current thread count exceeds the maximum number of threads that can be allocated in the thread pool.
-
Multi-threading advantage: can efficiently and quickly process multiple API requests (image compression, large amount of calculation, etc.) Is CPU-intensive.
-
Disadvantages of multi-threading: unsafe, if we have multiple threads to operate on the same database resource (for example, modify the same data), there will be data security problems [need to lock the resource].
Multithreading does not do one thing together, but by switching contexts (time-sharing), so multithreading can waste resources.
For details on the concept of multithreading, check out the related resources.
High concurrency
High concurrency is a concept of single threads.
- Advantages of high concurrency: No need to start multiple threads, saving resources.
- Disadvantages of high concurrency: Not suitable for complex operations. If complex operations need to be performed, you can start a subprocess.
Node is multithreaded, but its main thread is single-threaded. So we’ve always said that Node is really a single-threaded language.
Synchronous asynchronous and blocking non-blocking
- Synchronization is the process of executing a piece of code that cannot be executed until the code is returned, and then the other code can continue to be executed after the return value is returned.
- Asynchrony is the execution of a piece of code, the code does not immediately return the result, can continue to execute other code, the return value is retrieved by callback.
Refer to my previous article on synchronous blocking versus asynchronous non-blocking.
The Node of the EventLoop
- 1. The JS code we write will be handed over to the V8 engine for processing.
- 2. NodeApi may be called in the code, and Node is handed over to the Libuv library
- 3. Libuv implements asynchronous I/O through blocking I/O and multithreading.
- 4. In an event-driven way, put the result into the event queue and finally deliver it to our application.
This phase executes scheduling callbacks that have been setTimeout() and setInterval(). ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ ┌ ─ > │ timers │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | execution delays to the next loop iteration of I/O callback. │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ pending callbacks │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | system for internal use only. │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ idle, prepare │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | retrieve a new I/O events; Perform the I/O related callback ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ incoming: │ │ │ poll │ < ─ ─ ─ ─ ─ ┤ connections, │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ data, etc. │ │ setImmediate () callback function is executed here. └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ │ ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ │ │ check │ │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┬ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘ | some closed │ a callback function ┌ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┴ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┐ └ ─ ─ ┤ close callbacks │ └ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ─ ┘Copy the code
Here, each stage corresponds to an event queue. When the Event loop is executed to a certain stage, the queues corresponding to the current stage will be executed successively. When the queue is exhausted or the callback limit is reached, the event loop moves to the next phase.
process.nextTick()
Not technically part of the cycle of events. Priority over microtasks
Poll phase:
- Detects whether the Poll queue is empty, and if it is not empty, the task in the queue is executed until it times out or all the tasks are executed.
- Then detect whether the setImmediate queue is empty. If not, the Check phase is performed. If not, the setImmediate queue waits for the time to arrive. When the time reaches, it returns to the timer phase
- When the wait time reaches, a new callback may appear, which is also cleared in the current phase
This article was created by Mo Xiaoshang. If there are any problems or omissions in the article, welcome your correction and communication. You can also follow my personal site, blog park and nuggets, AND I will upload the article to these platforms after it is produced. Thank you for your support!