(a) This is called node. js------Single-threaded, non-blocking, event-driven

Source: Internet
Author: User
Tags lua ruby on rails

node. JS First Day notes (V1)One: Where does node. js come from?

In the fall of 2008, a young man named Ryan Dahl (Roy Darnay) had been playing for several years server programming, the bottleneck of server high concurrency performance became more and more difficult to overcome. Whether you're good at Ruby on Rails or traditional lamp. and C or LUA. Each has its own flaws. Ruby's virtual machine is too bad, C although performance is relatively high, but the innate language itself is flawed, resulting in inefficient development of the web. Lua is an existing synchronous I/O problem that causes no performance benefit.

Ryan has used these languages to develop a Web server A few years later, although it is not possible to solve the problems of these technologies themselves, it is vaguely felt that to solve the above problems, we need to rely on event-driven mechanisms and asynchronous I/O to solve.

Just as Ryan was desperate, the great Google Company used the V8 engine in its browser chrome engine. The advent of this browser's JavaScript engine makes Ryan's eyes shine. What are the highlights of the V8 engine?

    • L There is no synchronous I/O and there is no case that a synchronous I/O causes a dramatic decrease in event cycle performance.
    • L V8 performance is good enough to parse the script much faster than the scripting engine in other languages such as Python,ruby.
    • The closure characteristics of the JavaScript language itself are excellent, far exceeding the callback functions in other languages such as C.

V8 was originally the engine of Google's interpretation of JavaScript in its own browser Chrome, but Ryan was acutely involved in the realization of the idea and the implementation techniques he had been thinking about for the Web server over the years. Since the browser can send requests, accept requests, and the server can send requests and accept requests, why not reverse the order of the V8 engines? So Ryan was like this. This engine is used to implement Web server technology development.

In 2009, Ryan named these technologies as node. js. Web server-side technology is written in the JavaScript language. and submitted its own first line of "Hello World" code. In May, in Berlin this year, Ryan presented the technology to the world. and spread so far.

In short, we use a sentence to evaluate the founder of node. js. In the Web server field for many years, dedicated to solving high-performance concurrency problems. Several setbacks. Finally after encountering the V8, invented the cross-era technology of node. js.

Although Ryan withdrew from the node. JS Project in 2012, the next few years had the development of node. JS growing and the community running better.

Today, more and more companies are starting to use node. js to develop their own web servers.

Two: What is node. js? Can you eat it?

node. JS is a development platform that lets JavaScript run on the server side. It extends the tentacles of JavaScript to the Web server.

But node. JS also seems different

    • L. JS is not a separate language, with php,ruby,python,jsp,asp. NET and so on "is both language, is platform" different. node. JS is developed using the JavaScript language and runs on the server-side V8 engine.
    • Unlike php,jsp and other languages, node. js skips over the Web server such as Apache,naginx,iis. The web site that it develops can not be deployed on any HTTP server. node. JS does not have a Web container.
    • node. JS's own philosophy is to spend the smallest hardware cost and pursue higher concurrency. Higher performance processing.
Three: Open the website of node. JS to see if there's anything good.

British Civil Service Network: http://www.nodejs.org

Chinese Civil Service Network: http://www.nodejs.cn

See home page: This section of the text contains the core mechanism of node. js.

Let's try to explain the core technology of node. js.

Features of node. js

Single threaded: In a server-side language such as java,php or NET, a single thread is created for each client connection. Each thread consumes about 2MB of memory alone. In other words, a Web server with 8GB memory can connect about 4,000 clients. To allow a Web server to connect more client access, you must increase the number of servers. And the cost of Web server application is greatly increased.

node. JS does not create a new thread for each client connection. Instead of just using a thread, when a user is connected, an internal event is triggered, through non-blocking I/O, event-driven mechanisms, so that the node. JS program is also parallel to the macro, using node. js, a 8GB memory bar, can theoretically support 40,000 user connections. Another advantage of the single thread is that the operating system is not created, destroying the time that the thread was destroyed.

The single thread of node. js

(Multithreaded Web Services)

Single Thread Web Service

Non-blocking: for example, when accessing database data, it takes a while to do I/O operations, in the traditional single-threaded processing mechanism, after the execution of the Access database code, the thread will first stop, waiting for the database to return data results, to execute the subsequent code, that is, I/O blocking the execution of the thread, Greatly reduces the execution efficiency of the program.

Because node. JS performs a non-blocking I/O mechanism, the following code is executed immediately after the Access database code is executed, and the processing of the database execution return results is put into the execution callback function of the code, thus improving the efficiency of the function execution.

When an I/O execution is complete, the thread that performs the I/O operation is notified as an event, and the thread executes the callback function for the event, in order to handle the asynchronous I/O, the thread must have an event loop, constantly checking for unhandled events, and then processing them sequentially.

In traditional blocking mode, a thread can only handle one task, and the throughput of the connection to be processed must be multithreaded. In the non-blocking mode of node. js, a thread is always performing a calculation operation, the CPU utilization of this thread is always 100%, so this is a kind of philosophical design, many people, but a lot of people are idle, not as much as people, to work in the dead.

Event-driven: In node, the client request to establish a connection, commit data and other behaviors, will trigger the corresponding event, in node, in a moment, can only execute a callback function, but in the middle of executing a callback function, you can instead handle other events (such as a new user connected), It then continues to return the callback function that executes the original event, which becomes the "event loop" mechanism.

At the bottom of node. JS is C + +, where nearly half of the underlying code is used for event queuing and callback function queue construction. Use event-driven to accomplish task scheduling, which is what wizards can think of.

The three major technical points above must be understood in depth, and when you understand them, you will find that these three things are actually just one thing.

Single thread: The advantage of single threading is that it reduces memory overhead and the memory of the operating system is paged out. If it is multi-threaded, then something goes into the I/O, but it is blocked and the thread is blocked.

Non-blocking I/O: does not fool the end of the I/O statement, but executes the subsequent statement. Can non-blocking solve the problem? For example, the thread is executing the red request connection, and Xiaoming's I/O operation callback is complete, this matter what to do?

Event mechanism, event loop: Whether it's a new user's request or an old user's I/O completion, node. JS will join the event loop to wait for dispatch.

Say is three characteristics: In fact, is a characteristic, who can not leave who.

node. JS wants a stingy restaurant owner to hire a waiter and die to get the waiter to work.

four: What kind of business does node. JS Fit to develop

node. JS is ideal when an application requires large concurrent I/O and the application does not require very complex processing before it is addressed to the client. node. JS is also well-suited for the development of long-connected interactive applications that are combined with websocket.

Like what:

User Form Collection

Test system

Chat room

Graphic Live

Provides the JSON API (used for angular in the foreground)

node. JS cannot challenge legacy server-side technologies like traditional jsp,php

node. JS itself is a technology enthusiast (geek) pursuit of the nature of the product, the lack of a lot of Web server robustness considerations, Arbitrary node. JS can not be used in securities, banking, telecommunications and other high-reliability business.

Most of the companies that use node. js in China are in Angels, round A, and b companies.

Kung Fu Bear: The backstage is node. JS is serving.

Realization Network: The entire station is node. JS developed.

Knowledge: Inside the station letter function

There are many forms on Baidu that are also developed by node. js

(a) This is called node. js------Single-threaded, non-blocking, event-driven

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.