头图

Original: Why Node.js is a single threaded language?

Applications built on node.js use a single-threaded event loop model architecture to handle multiple concurrent clients, such as JSP, Spring MVC, ASP.NET, HTML, Ajax, jQuery, etc. There are other web technologies that can be used, but these listed technologies follow a "multithreaded request-response" architecture to handle multiple concurrent clients.

Single thread: The Node JS platform does not follow the multithreaded request/response stateless model. It follows the single thread and event loop model. The Node JS Processing model is mainly inspired by the JavaScript event-based model and has a JavaScript callback mechanism. Therefore, Node.js can easily handle more concurrent client requests. The event loop is the core of the Node.js processing model, as shown in the figure below.

n = The number of requests made by the client to the Node.js Web server.
Suppose they access the web application client we built on Node.js at the same time as Client-1, Client-2. .. Client-n.
m = the number of threads in the thread pool.

The Web server receives Client-1, Client-2. .. Until Client-n requests and put them in the event queue.

The advantages of the single-threaded event loop over the multithreaded request/response stateless model:

  • Can easily handle more and more concurrent client requests.
  • Due to the event loop, there is no need to create more and more threads.

Applications built on node.js use as few threads as possible to reduce memory or resource usage.

The reasons why node.js uses the single-threaded event loop model architecture:

  • Initially, node.js was created as an experiment for asynchronous processing. In theory, when the application does not use the CPU, asynchronous processing on a single thread can provide a higher performance than a typical thread-based implementation under a typical Web load. The performance and scalability are intensive things, and can run thousands of concurrent connections than Apache or IIS or other thread-based servers.
  • The single-threaded and asynchronous nature of node.js does make things complicated, but threads are more than this in terms of the time spent designing applications, development costs, deadlocks, priority inversion, and the life cycle issues of all applications. Oops.
  • There is also a well-known and criticized problem with the model that uses a thread to respond to each request of the server. Compared with the event loop threading model, they have poor scalability in various scenarios. In short, They lack the scalability as applications that continue to grow to meet future needs and add new features.

Since Node.js follows the single-threaded event loop model, it is inspired by the JavaScript event-based model and JavaScript callback mechanism. Therefore, node.js is a single thread similar to JavaScript, but not pure JavaScript code, which means that things like network calls, file system tasks, DNS lookups and other asynchronous completions are not actually processed by the main thread.

How the single threaded non blocking IO model works in NodeJS ?

Example: If we receive requests from two users A and B. Through non-blocking IO, we can initiate a request to A, and then immediately initiate a request to B without waiting for the response to A's request. Therefore, we can say that with non-blocking IO we can eliminate the use of multithreading, because the node server can handle multiple requests at the same time.

Working of single-threaded non-blocking IO:

When the client sends a request to the server, the request is called an event. All these requests are stored in an event queue. A single thread that exists in the event loop assigns this request to one of the threads that exist in the internal thread pool.

This thread reads the client request, processes the request, performs any blocking IO operations when needed, and prepares to send the final response back to the server. The event loop sends this response back to the corresponding client.

The event loop receives requests and processes them indefinitely.

Due to the event loop, multiple threads are not required. Due to this event loop and single-threaded concept, node.js uses less resources and memory.

More original articles by Jerry, all in: "Wang Zixi":


注销
1k 声望1.6k 粉丝

invalid