Understanding node. JS Event-driven programming

Source: Internet
Author: User

node. JS is now very active, and the relevant eco-community has surpassed Lua (basically the more well-known features are implemented with the Nodejs module).

But why do we use node. js? What are the advantages of nodejs compared to the traditional webserver service model?

node. JS is a JavaScript-based language that constructs a non-blocking event-driven IO framework on Google V8 engine and Linux. Nodejs is single-process single-threaded, but based on V8 's strong drive and event-driven model, the performance of Nodejs is very high, and it is not difficult to achieve multicore or multi-process (there are now a large number of third-party module to implement this feature).

This is mainly not to introduce Nodejs specific application code, but to introduce event-driven programming.

In this article (1), Dan York introduces two typical event-driven instances.

The first example is about a doctor.

To see a doctor in the United States, you need to fill out a large number of forms, such as insurance, personal information, such as traditional thread-based systems (thread-based system), the receptionist called you, you need to fill in the front desk to complete these forms, you stand to fill the order, and the receptionist sat watching you fill out the list. you let the receptionist not be able to receive the next customer unless you finish your business .

Want the system to run faster, only a few more receptionists, labor costs need to increase a lot.

Event-based Systems (event-based system), when you go to the window to find that you need to fill out some extra forms instead of just hanging a number, the receptionist will give you the form and pen, telling you you can fill in a seat and then go back to him after you've finished filling it out. You go back to sit and fill in the form, and the receptionist starts receiving the next client. you're not blocking the receptionist's service .

You fill out the form, return to the team, wait for the receptionist to finish the present customer, and you pass the form to him. If you have any questions or need to fill out additional forms, he will give you a new one and repeat the process.

The system is already very efficient, and almost all of the doctors do it. If you wait too many people, you can join an additional receptionist for service, but certainly much less than based on threading patterns.

The second example is a fast food restaurant ordering food.

In a thread-based approach (thread-based way) before you go to the counter, give your order to the cashier or order the cashier directly, and wait until you have the food ready for you. The cashier can't receive the next person unless you get the food and leave. Want to receive more customers, easy! Add more Cashiers!

Of course, we know that fast food restaurants do not actually work like this. They are actually based on the event-driven approach, so the cashier is more efficient. As long as you give your order to the cashier, someone has started to prepare your food, while the cashier is collecting money, when you have paid, you stand aside and the cashiers have started to receive the next customer. In some restaurants, you'll even get a number, and if your food is ready, call your number and let you go to the counter. The key point is that you are not blocking the next customer's order request. The food you ordered is a good event that causes someone to do something (a waiter calls your order number, you hear your number being shouted to fetch food), and in the programming world we call this a callback (callback function).

What does node. js do?

Traditional Web server is based on the threading model. You start Apache or what server, it starts waiting to accept the connection. When a connection is received, the server keeps the connection connected until the page or whatever transaction request is completed. If he needs to spend a few minutes reading a disk or accessing a database, Web server blocks IO operations (also known as blocking IO). To improve the performance of such a Web server, you only have to start more server instances.

Instead, node. JS uses the event-driven model, when the Web server receives the request, shuts it down and processes it, and then goes to service the next Web request. When this request is completed, it is put back into the processing queue, and when it arrives at the beginning of the queue, the result is returned to the user. This model is very efficient and extensible because webserver always accepts requests without waiting for any read and write operations. (This is also known as non-blocking IO or event-driven IO)

Consider the following procedure:

1, you use the browser to access the "/about.html" on the Nodejs server

The 2,NODEJS server receives your request and calls a function to read the file from the disk.

3, this time, Nodejs webserver in the service of subsequent Web requests.

4, when the file is read, a callback function is inserted into the Nodejs service queue.

5,nodejs webserver running this function is actually rendering (render) The about.html page returned to your browser.

It seems to save a few microseconds, but it's important! Especially for Web servers that require a large number of users.

That's why node. js is so hot and so interesting. It also uses a very generic programming language JavaScript, and also allows developers to quickly and easily write high-scalability servers.

(1) http://code.danyork.com/2011/01/25/ node-js-doctors-offices-and-fast-food-restaurants-understanding-event-driven-programming/

Understanding node. JS Event-driven programming

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.