High concurrency caused by a function

Source: Internet
Author: User
There is such a demand. A user wants to register a domain name. However, all users who want to do well are registered. At this time, the program automatically generates a certain number of domain names, assuming that 100 domain names are generated. Then, the system automatically queries whether these 100 domain names have been registered. From the very beginning of generation, the domain name has been displayed to the user. A user wants to register a domain name. However, all users who want to do well are registered.
At this time, the program automatically generates a certain number of domain names, assuming that 100 domain names are generated. Then in the automatic Query
Whether these 100 domain names have been registered. The domain names have been displayed to the user since the generation, and then
Ajax is used to update the registration status of each domain name in real time.

At this time, a user will bring 100 concurrent queries, and query the domain name status, it is necessary to connect to the whois server, there is a network transmission process
That is, the 100 ajax query backend will bring 100 php requests to send whois queries, wait for the results, analyze the results, and return the results to ajax calls.

In general, several users can check at the same time. The php processing process is full. Other php implementations are waiting for the entire site ..

In this case, how can we optimize it?

The solution I want is:
1. Take the domain name status query and place it on another server.
2. This synchronous method is not used to query the domain name status. Instead, the asynchronous model implementation of node. js is used.

Reply content:

There is such a demand. A user wants to register a domain name. However, all users who want to do well are registered.
At this time, the program automatically generates a certain number of domain names, assuming that 100 domain names are generated. Then in the automatic Query
Whether these 100 domain names have been registered. The domain names have been displayed to the user since the generation, and then
Ajax is used to update the registration status of each domain name in real time.

At this time, a user will bring 100 concurrent queries, and query the domain name status, it is necessary to connect to the whois server, there is a network transmission process
That is, the 100 ajax query backend will bring 100 php requests to send whois queries, wait for the results, analyze the results, and return the results to ajax calls.

In general, several users can check at the same time. The php processing process is full. Other php implementations are waiting for the entire site ..

In this case, how can we optimize it?

The solution I want is:
1. Take the domain name status query and place it on another server.
2. This synchronous method is not used to query the domain name status. Instead, the asynchronous model implementation of node. js is used.

Nodejs is a method of brute force execution. Another idea is to create a task queue to control the total concurrency, for example, the maximum concurrency is 50, then every user queries push100 tasks into the queue. When the queue is too long, the user can be told that the system is busy.

However, no matter how you do it, the first thing you need to do is to change 100 ajax to 1 ......

You can optimize the process by merging 100 ajax requests into one ajax request and returning the status of 100 domain names.

The php backend requests data from the whois server in batches Based on the 100 domain names to be queried.
The example of php concurrent (batch) http requests is as follows:


  

This ensures that a php process can query the domain name status concurrently, rather than blocking the query one by one, which is very efficient. Setting the timeout does not cause the process to be full due to network io blocking.

The https://github.com/visionmedia/batch can be adjusted according to your actual situation.

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.