What is node. js event-driven programming?

Source: Internet
Author: User

Node. js is very active now, and the related ecological communities have surpassed Lua (basically, the well-known functions are implemented by the Node. js module ). But why should we use Node. Js? What are the advantages of nodejs over the traditional webserver service model?

Node. Js is a non-blocking event-driven I/O framework built on google V8 engine and Linux based on the javascript language. Nodejs is a single-process, single-thread, but based on the powerful driving force of V8 and the event-driven model, nodejs has very high performance, in addition, it is not difficult to achieve multi-core or multi-process (a large number of third-party modules are already available to implement this function ).

Here we will not introduce the specific application code of nodejs, but will introduce the event-driven programming.

Dan York introduces two typical event-driven instances.

The first example is about seeing a doctor.

When you go to the United States to see a doctor, you need to fill in a large number of forms, such as insurance, personal information, and so on. The traditional thread-based system, the receptionist calls you, you need to fill out these forms at the front-end. You are standing and filling out the form, while the receptionist is sitting and watching you fill out the form. You cannot allow the receptionist to receive the next customer unless you complete your business.

To make the system run faster, only a few receptionists need to be added, and the labor cost needs to be increased a lot.

In the event-based system, when you arrive at the window, you need to fill in some additional forms, not just hanging up a number. The receptionist will give you the form and pen, you can fill in a seat and then go back to him. You go back and fill out the form, and the receptionist starts to receive the next customer. You didn't block the receptionist's service.

You fill out the form and return to the team. After the receptionist receives the current customer, you will hand the form to him. If you have any questions or need to fill in an additional form, he will give you a new one and repeat the process. This system is very efficient, and almost most doctors do this. If too many people are waiting, you can join the additional receptionist for service, but it must be much less than the thread-based mode.

The second example is a fast food restaurant ordering.

In the thread-based approach, before you arrive at the counter, give your order to the cashier or give the cashier a direct order, then wait until the food you want is ready for you. The cashier cannot receive the next person unless you get the food and leave. It's easy to receive more customers! Add more cashiers!

Of course, we know that fast food restaurants do not actually work like this. They are actually event-driven, so that the Cashier is more efficient. As long as you give the order to the cashier, someone has begun preparing your food and the cashier is collecting the money, you are standing aside and the cashier has begun to receive the next customer. In some restaurants, you may even get a number. If your food is ready, call your number to ask you to pick it up at the counter. The key point is that you have not blocked the next customer's order request. When the food you order is prepared, a person may make an action (a waiter calls your order number and you hear your number be called for food). In the programming field, we call this callback function ).

What does Node. Js do?

Traditional web servers are mostly based on thread models. When you start Apache or what server, it starts to wait for the connection to be accepted. When a connection is received, the server maintains a connection until the page or transaction request is complete. If it takes a few minutes to read the disk or access the database, the web server blocks the IO operation (which is also called blocking IO ). to improve the performance of such web server, only more server instances can be started.

On the contrary, Node. Js uses the event-driven model. When the web server receives a request, it closes the request and processes it, And then serves the next web request. When the request is completed, it is put back into the processing queue. When it reaches the beginning of the queue, this result is returned to the user. This model is highly efficient and highly scalable, because webserver keeps receiving requests without waiting for any read/write operations. (This is also called non-blocking IO or event-driven IO ).

Consider the following process:

  1. You can use a browser to access "/about.html" on the nodejs server"
  2. The nodejs server receives your request and calls a function to read the file from the disk.
  3. During this time, nodejs webserver will serve subsequent web requests.
  4. When the file is read, a callback function is inserted into the service queue of nodejs.
  5. When nodejs webserverruns this function, renderreturns the about.html page to your browser.

It seems to have saved several microseconds, but this is very important! Especially for web servers that require a large number of users.

That's why Node. Js is so popular. It also uses a very common programming language, Javascript, and allows developers to quickly and easily write highly scalable servers.

Related Article

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.