Nodejs is the server-side JS platform for fire in recent years, which is due to its high concurrency at the back end, and the rise of powerful Code and project management applications on the Nodejs platform, such as NPM, Grunt, and express, almost redefining the way the front end works and processes.
Nodejs's success is a sign of its strength, but not all cases are appropriate to apply Nodejs as a server-side platform?
The answer is, of course, negative, and there are divergent opinions on the Internet. So let's start with the principle and learn about the applicability of Nodejs.
Before we talk about Nodejs, we're not going to look at the traditional (Apache-represented) server-side processing platform to handle concurrency.
1. Apache Multi-threaded high concurrency mode
Apache is the world's number one Web server software, it is supported by multi-threaded concurrency and is welcomed by the broad selection of servers technology. But later on, Apache has gradually exposed its drawbacks in some large Web applications: blocking.
That some classmates will be strange, Apache is not multithreaded processing concurrency, why still appear blocking?
To understand this, we first need to understand the concept of threading
1.1 What is a thread?
We cite the official explanation: the smallest CPU unit that a thread can run independently can run concurrently in the same process, sharing the memory address space under that process (note this feature).
We can see that the thread under the same process is sharing the same file and memory (memory address space), so you can imagine that when different threads need to occupy the same variable, on a first-come-first-served basis, the first-to-last thread is working, and later threads can only wait next, that is, to join the blocking queue sequence. So this is the cause of thread blocking.
Therefore, although a process can support multiple threads,
they appear to execute simultaneously but are not synchronized with each other。 Multiple threads in a process share the same memory address space, which means they can access the same variables and objects, and they allocate objects from the same heap. While this makes it easier to share information between threads, programmers must be careful to ensure that they do not interfere with other threads in the same process. Understanding the shortcomings of multithreading parallelism, we can better understand the nodejs of the strong place. Because Nodejs is asynchronous single-threaded!
2. Nodejs's asynchronous I/O principleLet's take a look at the Apache Request database code: The code executes to the first line when the thread blocks, waits for the query to return the result, and then continues processing. Due to database queries, disk reads and writes, network traffic, and so on (so-called I/O), blocking time can be very large (relative to CPU always frequency). For high-concurrency access, on the one hand, long-term blocking waits on the one hand, and the addition of new threads in order to cope with the new situation, wastes a lot of system resources, and the increase in threads also consumes a lot of CPU time to handle memory context switching. See how node. js handles it. See, No,
just four words: Async callback。 The second argument to query is a callback function that executes to db.query without waiting for the result to return, but proceeds directly to the following statement until it enters the event loop. When the database execution results are returned, the event is sent to the event queue, and the previous callback function is not invoked until the thread enters the event loop. A more professional argument is asynchronous I/O. Just a single thread is available. Then why Nodejs do single-threaded, but can realize asynchronous? Here we go first on a picture, the event queue in a direct poke graph see no, Nodejs's working principle is actually
Event Loops。 It can be said that each Nodejs logic is written in the callback function, and the callback function is returned after the asynchronous execution! See here, you can not help but Marvel, Nodejs if all the processing is asynchronous, do not know to fly? Wrong wrong wrong! Of course not, don't forget that the basis for NODEJS to achieve these is
Single Thread。 Yes, single thread! One thread to carry all the actions!You can imagine, nodejs in the cold wind in the face of 100,000 concurrent armies, OK, no problem, come up to the enemy one threw into the city, come up and throw it into the city. The city is way and can digest these enemies well. But if come up a similar to Zhang Fei Zhao Yun such character, old node heart a miserable, and Zhang Fei War 300 rounds, put him mutilated, and then thrown into the city. The 100,000 armies behind will have to wait for these 300 rounds ... So what does that mean? Note that Nodejs is not without blocking, but rather blocking the process that does not occur in subsequent callbacks, and that occurs in the Nodejs itself for the calculation and processing of the logic. We already know that the Nodejs is incredibly powerful in its ability to circulate events for asynchronous callbacks. But if a complex logical operation is encountered in the event of a loop, how can a thin single thread support millions of logic +What about concurrency? Nodejs All of its I/O, network communications and other time-consuming operations can be handed to the worker threads to perform the callback again, so soon. But the normal operation of the CPU, it can only self-resistance. Speaking of which, you probably have a spectral estimate of Nodejs's characteristics. Therefore, the application of the scene is basically a sight!
3. Nodejs's application ScenarioSince Nodejs has a strong ability to handle concurrency, the ability to process compute and logic is weak, so if we move complex logical operations to the front end (client), and Nodejs only needs to provide asynchronous I/O, high-concurrency performance can be achieved. There are a lot of things, such as: RESTFUL APIs, live chat, a powerful single-page app for client logic, such as: Localized online music applications, localized online search applications, localized online apps, etc. By the way, Apache has suppressed so much and given a chocolate. Apache because of its multi-threaded high concurrency shared memory address space, it means that if the server is strong enough, the processor is high enough core, Apache operation will be very good, so it is suitable for (concurrent) asynchronous processing of relatively small, background computation is large, background business logic complex applications.
Understanding the application of Nodejs in principle