Simple PHP control site concurrency

Source: Internet
Author: User

In general, a project will always have services that are difficult to process, such as complicated searches and search systems such as sphsf-ce, if the data source changes too fast (such as top, step, and browsing number), the frequency of pushing data is hard to measure, and it cannot be precisely searched or sorted. Therefore, in general, the method is to search through the DB, and in addition to breaking down the business to the program level as much as possible, a layer of cache will be added before the DB. However, doing so also has many known drawbacks:
1. In fact, many businesses cannot be processed in common PHP, such as complicated sorting and filtering. If you use common PHP, the efficiency is not as high as that of MYSQL.
2. the cache hit rate is not guaranteed, especially for some services that use keywords for query, the keyword changes too much.

In case of malicious clicks, the DB server is prone to direct disconnection. Therefore, we can add a limiter at the program level to limit the number of concurrent connections. This limiter has the following features:
1. Efficient (nonsense, otherwise, the attacker will be able to brush the limiter directly ). Now we use memcache for atomic operation count, which can be extended to other methods.
2. Precise to the action layer. You can restrict a single page.
3. Convenient. See what you need
4. low cost. Including R & D costs and hardware costs.

 

According to the stress test, the original only 100 concurrent search function, after the limit of 100, the pressure test 800 results are normal.

The Code is as follows:

 

<? Php/*** search */public function search () {// adds the counter function. If the counter function is exceeded, the system is busy. $ viewlimiter = Library: load ('viewlimiter') is returned '); // The Name Of The access limiter. Use controller + action to ensure that a single page is unique $ limitName = 'search _ search'; // The maximum number of concurrent connections is $ limit = 100; // if the number is exceeded, if (! $ Viewlimiter-> check ($ limitName, $ limit) {ajaxOutput (0, $ this-> lang-> line ('multi _ search_limit '));} /* ---- complex and abnormal business logic ----- */$ xxxModel-> search ($ params);} // end func?>

Extended topic:
1. if the service is flushed, normal users may not be able to view the content. -- This problem must be solved from the system level to operate, visible here: http://johnsteven.blog.51cto.com/2523007/818209
2. It is best to combine the limit quantity with the cache hit rate to automatically change the limit quantity within a certain range to improve the accuracy of the limit. -- This study can be conducted later and combined with the cache class
3. Dynamic Control Based on server load. -- The underlying and server permissions are involved, and the research cost is high. If you are interested, you can study them.

Note:
This article only describes the ideas. The specific code is not open for some reasons.

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.