Contents
Is the performance of the application fast or slow?
First of all, I want to share the fact that the application bottleneck is not due to choosing back-end or front-end technology, I dare say that. Even though we are the same team that is using NodeJS for the back-end, we never talked to ruby or java about how good our NodeJS performance is. Your application system is crippled at peak times such as buying a new year train, or getting a voucher to buy an iPhone 12 for example, unrelated to the performance of Nodejs if you are using it. . .
For a large application, most performance bottlenecks the culprit is none other than one of the IO operations. This includes database operations (number of connections, machine performance, etc.), cache services, network IO, file reads and writes, and others. If the IO producer’s performance bottlenecks, no matter how you distribute it and pile up the machines, no matter how great the language is, it’ll just choke.
Nodejs is multithreaded?
There are different opinions on whether Node.js is single-threaded or multi-threaded. Does adding worker_threads mean that Node.js already accepts multithreading? And still know single thread will be slower than multithread, but why is Redis single-threaded but still so fast and query performance extremely high? The following is my personal opinion, everyone has the right to understand differently. Please comment peacefully…
Today we will answer this question: “Why the newly born Nodejs is not effective when there are high connections”, what has Nodejs improved to solve this problem? To be honest, it is difficult to explain for you to understand right away in this article, so first of all, I ask you to learn about the following phrases if you do not know them:
- child_process
- cluster
- worker_threads
- Event Loop
Node.js provides “multi-threading” capabilities by providing a cluster and child_process API for creating child processes. But this way of creating a process will sacrifice shared memory and pass data that has to be passed through JSON. (There are certain limitations and performance issues)
Based on this, Node.js from V10.5.0 provides additional worker_threads, which are lighter than child_process or cluster. Unlike child_process or cluster, worker_threads can share memory bypassing ArrayBuffers or sharing SharedArrayBuffer. The above is to let you know that Nodej actually implemented a multithreading mechanism.
But this is the case Node.js multithreaded is not meant to be the same as other multithreaded programming languages (eg: java). Node.js’ worker_threads is different from java’s multithreading. If threads are added, the nature of the language itself will change, so streams cannot be added as a new set of built-in classes or functions. Understand that for now.
So the question here many of you wonder is “Well, let’s say multithreading, so how does multithreading work?” OK, I’ll answer if you ask. Ah also talk about Why if making backend why not use java but Nodeis. java does much better. Just wait, everything will be explained. I have my own opinion, everyone does.
What is multithreading?
Multithreading is about allowing multiple threads in a program to do a lot of things without blocking interface connections. For example, multithreaded download engines like Thunder are typical multithreading. When there is a download task, Thunder splits the file into 10 equal parts, then opens 10 threads to download them separately. Now the main interface is a separate thread, will not be stuck because of downloading files. And the main thread can control the sub-threads. For example, if a thread downloads slowly or even stop, the main thread may be forced to shut it down and restart another thread. That’s why programming using multithreading is fast.
If you are writing a server-side application, in fact, under the current network service model, the cost of opening a dining table is negligible, because it is common to open processes or threads by volume. CPU. cores and then open them. After that, the number was maintained. Processes and threads use communication or asynchronous communication to handle many concurrent connections, so the overhead of opening processes and opening threads can be ignored.
In modern systems, a typical CPU has multiple cores and multiple cores can run many different threads or processes at the same time. When each CPU core runs a thread, because each thread needs to share resources, these resources must be copied from one core of the CPU to another to continue the computation, which incurs additional overhead. In other words, when the CPU is multi-core, multi-threading will not be as good as multi-processing in terms of performance. That’s me telling you more about multi-core and multi-threading.
Summary
No need to summarize because the above is clear, the point of this article is to explain to you about multithreading in nodejs. But Nodejs thrives to this day, not only has multithreaded mining, but the nature of multithreading here is different from java, but also has nonblocking IO, event-loop elements. Most of the performance bottlenecks are in the IO operations. That’s why practically, sometimes 100 people gather to eat and 100 people line up to eat, but you only hear about 100 people coming to eat. But you can’t see clearly that there are two different types. Latches
The reason why Nodejs can handle high concurrency in a single thread is because of the event loop mechanism of the libuv class and the underlying thread pool implementation.