Nodejs Series One: the Magical Nodejs

Source: Internet
Author: User
Tags lua

    1. What is Nodejs?
    2. What problems can nodejs solve?
    3. Non-blocking I/O and event loop mechanism
    4. When to use Nodejs
    • What is Nodejs?

node. JS is a platform (or frame) that lets JavaScript out of the browser and runs on the server, not the language; JavaScript compatibility issues that run outside of the browser without a headache are single-threaded, Asynchronous IO and event-driven design to achieve high concurrency (asynchronous events are also somewhat more difficult to develop and debug); node. JS has an HTTP server built in, so it's good news for web development;

In fact, at the beginning of the implementation of node. js, the author Ryan Dahl did not choose JavaScript, and he tried C, Lua, because of its lack of some high-level language features, such as closures, functional programming, resulting in complicated procedures, difficult to maintain. JavaScript is a language that supports the functional programming paradigm, which fits well with the node. JS Event-driven programming model. With the V8 engine provided by Google, the JavaScript language is executed much more quickly. The final presentation in front of us becomes node. js, not the implementation of Node.c,node.lua or other languages.

Nodejs is not a JavaScript application, but a JavaScript runtime environment written in the C + + language, and node. JS uses the Google Chrome browser V8 engine, which performs well and provides many system-level APIs. such as file operations, network programming, and so on. Browser-side JavaScript code is subject to a variety of security restrictions at runtime, with limited operation to the client system. In contrast, node. JS is a comprehensive background runtime that provides JavaScript with many features that other languages can implement.

node. JS is also more innovative in its design, which runs in single-process, single-threaded mode (which is consistent with how JavaScript works), and the event-driven mechanism is implemented by node. JS through an internal single thread that efficiently maintains the event loop queue, without multi-threading resource occupancy and context switching, which means facing large-scale H The TTP request, node. JS takes care of everything with event-driven, Web service developers accustomed to traditional languages may be familiar with multithreading concurrency and collaboration, but in the face of node. js, we need to accept and understand its characteristics.

    • What problems can nodejs solve?

The primary goal of node. JS is to provide a simple development tool for creating high-performance servers, and to run a variety of applications on that server CAs.

Now let's look at what's going on in the popular server-side language. In Java, PHP, ASP. NET, a new thread is created for a client connection, and each thread consumes approximately 2MB of memory, which means that theoretically, a server with 8G of memory can support up to 4,000 connections at a time. For a Web application to support more users, you need to increase the number of servers accordingly, which directly leads to a rise in the cost of hardware for Web applications.
node. js addresses the above problems by changing the client-to-server connection method. node. JS does not create a new thread for each client connection, but instead triggers an event for each client connection that is handled inside node. JS (which is why node. JS is called event-driven). Therefore, node. JS can use this feature to handle client connections for up to tens of thousands of users at a time.

    • Non-blocking I/O and event loop mechanism (also called event polling)

The first thing to figure out is what blocking I/O and non-blocking I/O can continue our conversation:

Blocking I/O

Many I/O operations are necessary during program execution, such as reading and writing files, input and output, request response and so on. I/O operation is the most time-consuming, at least relative to code, in the traditional programming model, for example, you want to read a file, the entire thread is paused, waiting for the file to continue to execute after reading. In other words, I/O operations block the execution of the Code, greatly reducing the efficiency of the program.

Non-blocking I/O

Understanding blocking I/O, non-blocking I/O is a good idea. Non-blocking I/O is during program execution, I/O operations do not block execution of the program, that is, while I/O operations continue executing other code (thanks to node's event loop mechanism). This I/O model (non-blocking I/O) improves the performance of a program in an era where I/O devices are far less efficient than CPU efficiency.

We can understand that in node, in addition to the code, everything is parallel!

Next, let's look at what the event loop mechanism is called, or event polling.

Event Loop is a very important concept, which refers to a running mechanism of a computer system.

To understand the event Loop, you should start with the program's operating mode. A program that runs later is called a process, and typically a process can perform only one task at a time.

If there are many tasks that need to be performed, there are three ways to solve them.

(1), queued. Because a process can only perform one task at a time, you have to wait for the previous task to finish and then perform the subsequent tasks.

(2), create a new process. Use the fork command to create a new process for each task.

(3), create a new thread. Because processes are too resource-intensive, today's programs often allow a process to contain multiple threads, which are performed by threads.

In JavaScript, for example, it is a single-threaded language, and all tasks are done on one thread, using the first method above. Once a large number of tasks are encountered or a time-consuming task is encountered, the webpage will appear "suspended animation", because JavaScript can not stop, will not be able to respond to user behavior.

You might ask, why is JavaScript a single thread, can't it be multithreaded?

This is related to history:

One of the main features of the JavaScript language is single-threaded, meaning that only one thing can be done at the same time. So why can't javascript have multiple threads? This can improve the efficiency AH.

The single thread of JavaScript is related to its purpose. As a browser scripting language, JavaScript's primary purpose is to interact with the user and manipulate the DOM. This determines that it can only be single-threaded, otherwise it can lead to complex synchronization problems.

For example, assuming that JavaScript has two threads at the same time, one thread adds content to one of the DOM nodes, and the other thread deletes the node, which thread should the browser take precedence over?

So, to avoid complexity, JavaScript is a single thread from birth, which has become a core feature of the language and will not change in the future.

To take advantage of the computational power of multicore CPUs, HTML5 proposes a web worker standard that allows JavaScript scripts to create multiple threads, but the child threads are completely controlled by the main thread and must not manipulate the DOM.

So, this new standard does not change the nature of JavaScript single threading.

Back to EventLoop:

A single thread means that all tasks need to be queued, and the previous task is completed before the latter one is executed. If the previous task takes a long time, the latter task has to wait.

If the queue is because of large computational capacity, the CPU is not busy, but also forget, but many times the CPU is idle, because the IO device (input) is very slow (such as the Ajax operation from the network to read data), have to wait for the results, and then down to execute.

The designer of the JavaScript language realizes that at this point the main thread can completely ignore the IO device, suspend the waiting task, and first run the task that is in the queue. Wait until the IO device returns the result, and then go back and put the suspended task on hold.

Thus, all tasks can be divided into two types, one synchronization task (synchronous) and one asynchronous task (asynchronous). A synchronization task is a task that is queued for execution on the main thread, and only the previous task has finished executing it.

To perform the latter task, the asynchronous task refers to a task that goes into the task queue without entering the main thread, and only the task queue notifies the main thread that an asynchronous task can execute and the task goes into the main thread execution.

As long as the main line Cheng, will read "task queue", this is the operation mechanism of JavaScript. This process is repeated.

  

  

    • When to use Nodejs

After a look at node. js, what kind of application is node. js suitable for users to develop?

The answer is: when the application needs to handle a lot of concurrent input/output (I/O), and the server side does not need to do very complex processing before responding to the client, we should consider using node. js to develop our application.

For example:
Chat server
Integrated Services Web site
E-commerce sites, etc.

Nodejs Series One: the Magical Nodejs

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.