PHP does not have a database connection pool. how can this problem be solved? Using the high-load solution of the Nginxngx_http_limit_req_module module in the PHP environment _ PHP Tutorial

Source: Internet
Author: User
PHP does not have a database connection pool. how can this problem be solved? Use the high load solution of the Nginxngx_http_limit_req_module module in the PHP environment. PHP does not have a database connection pool. how can this problem be solved? In the PHP environment, a high-load solution using the Nginxngx_http_limit_req_module module runs a set of auxiliary systems online. it uses open-source PHP to change PHP without database connection pool. how can this problem be solved? High load solution using Nginx ngx_http_limit_req_module in the PHP environment

A set of auxiliary systems running online are modified using open-source PHP, and PHP has never been used before. I didn't expect many pitfalls. Suddenly one day, a user launched an online promotion activity, and then received tens of thousands of requests in a short time. then, the database connection was exhausted, almost dragging the entire system down in a short time. There are many causes of system crash. Today, we mainly analyze the reasons why PHP does not have a database connection pool.

In PHP, the database connection is established when the request arrives and released when the request ends. If thousands of requests arrive at the same time, thousands of database connections will be established at the same time, which is terrible. In addition, PHP wood has a better database connection pool driver solution, so we have to find another way.

There are three solutions to this problem:
1. use Mysql Proxy middleware. Mysql Proxy provides the connection pool management function. But we didn't use this method because it was urgent and nobody was familiar with it.
2. use PHP-FPM. PHP-FPM is a FastCGI Process Manager for PHP. You can control the number of processes simultaneously processing PHP requests through configuration.
Specific can refer to: http://www.linuxde.net/2013/06/14638.html
However, this solution is useless because the installation and configuration process is troublesome.
3. Use Nginx's ngx_http_limit_req_module to control requests.
This module can use custom key values to limit the request frequency. The method of restriction is like a funnel. the number of requests processed per second is fixed, the request that exceeds the limit is postponed, and the 503 request that exceeds the maximum value is rejected directly.
We use this scheme because we only need simple configuration and can flexibly control the scenarios where requests are restricted. For example, we do not limit requests for static resources, but limit requests for PHP. You can also extract the variable information from the URL as the key to achieve finer request restrictions.
The following is an example of our configuration.

Http {...... limit_req_zone $ limit_key zone = limit_one: 50 m rate = 30r/s; # defines the variable name with limit_key as the Key, which is used for subsequent values. each Key has its own counter. Limit_one is the name of the zone. Rate indicates that up to 30 simultaneous requests are accepted per second. Server {... if ($ request_uri ~ *. * Php. *) {set $ mp_limit_key $ binary_remote_addr; # First, a default Key is available for all PHP programs. the client IP address is used as the Key. It is equivalent that each client IP address is within the zone limit. } If ($ query_string ~ *. * Id/(d +). php. *) {set $ mp_limit_key $1; # extract the value following the id as the Key. } If ($ query_string ~ *. * Appid/wx (. * pai.html. *) {set $ mp_limit_key $1; # extract appid as the Key. } Limit_req zone = limit_one burst = 200; # limit the capacity of limit_one in this server to 200. Assume that the number of requests corresponding to a Key is 200, 30 requests are processed in the first second, and the remaining 170 requests are waiting in queue. If the number of requests corresponding to a Key is 300, 200 is returned for the part exceeding 503. .......}}

Why? In the PHP environment, the Nginx ngx_http_limit_req_module high-load solution is used to run a set of auxiliary systems online, which is modified by using open-source PHP...

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.