Nodejs Series -01-begins

Source: Internet
Author: User

1. What problems to solve

1. Concurrent connections

For example, imagine a scenario where we queue up for business in a bank, and we look at the following two models.

(1) System Threading Model:

The problem with this model is obvious, the server has only one thread, the concurrent request (the user) arrives only one, the rest waits, this is the blocking, the request that is being enjoyed by the service blocks behind the request.

(2) Multithreading, thread pool model:

This model has progressed more than the previous one, it regulates the number of service threads to increase the reception and response to concurrent requests, but when the concurrency is high, the request still needs to wait, and it has a more serious problem. At the level of the code, let's look at the process of client-side requests for communication with the server:

The server and the client each establish a connection, to this connection to allocate a set of resources, mainly reflected in the system memory resources, in PHP, for example, to maintain a connection may require 20M of memory. This is why the general concurrency is large, you need to open the server more.

So how did Nodejs solve the problem? Let's look at another model and imagine the scene where we have a meal at a fast food restaurant.

(3) Asynchronous, event-driven model

We're also going to make a request to wait for the server to respond, but unlike the bank example, we get a number after the meal, get the number, we tend to wait in the position, and the request behind us continues to be processed, as well as taking a number and waiting for the receptionist to be able to handle it all the time.

Wait for the food to make the number, will shout the number, we got our own food, carries on the subsequent processing (eats). This call number action in the Nodejs called Callback (Callback), can be in the event (cooking, I/O) processing completed after the continuation of the logic (eat), which embodies the salient features of the NODEJS, the asynchronous mechanism, event-driven entire process does not block the connection of new users (ordering), There is no need to maintain a connection between the user who has ordered the meal and the chef.

Based on this mechanism, in theory, there are user requests to connect, Nodejs can respond, so nodejs can support higher concurrency than Java, PHP programs, although the maintenance event queue also cost, and because Nodejs is single-threaded, the longer the event queue, the longer the response time, Concurrency is still too much to go by.

Summarize how Nodejs solves the problem of concurrent connections: Change the way you connect to the server, each connection launch (emit) an event that runs in the Nodejs engine process, and put it in the event queue, Instead of generating a new OS thread for each connection (and allocating some companion memory for it).

2. I/O blocking

Another problem that Nodejs solves is I/O blocking, and look at the business scenario where you need to pull data from multiple data sources and then process it.

(1) Serial data acquisition, which is our general solution, take PHP as an example

If you need 1S for both profile and timeline operations, then serial acquisition requires 2S.

(2) Nodejs non-blocking I/O, transmit/listen events to control the execution process

Nodejs encountered I/O event will create a thread to execute, and then the main thread will continue to execute, therefore, take the action of profile to trigger an I/O event, will immediately execute the action of taking timeline, two actions parallel execution, if each need 1S, then the total time is 1S. After their I/O operations are completed, an event, profile, and timeline are emitted, and the event agent receives and continues to perform the subsequent logic, which is the feature of Nodejs non-blocking I/O.

2. Installation

http://nodejs.cn/download/, Direct next

  

Indicates that the installation was successful.

  

Nodejs Series -01-begins

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.