PHP No database connection pool how to break? High load solution using Nginx ngx_http_limit_req_module module in PHP environment

Source: Internet
Author: User
Tags fpm manage connection

Online running a set of auxiliary system is the use of open source PHP change, and did not play before the PHP, did not expect this thing still have a lot of pits. Suddenly one day a user to do online campaign promotion, and then a short time into the tens of thousands of requests, and then the database connection exhausted, a short period of time almost brought down the entire system. There are many reasons for the system to crash, today mainly for PHP does not have database connection pool reasons to analyze.

In PHP, the database connection is established when the request arrives and is released at the end of the request. If at the same time thousands of requests arrive, it will establish thousands of database connections at the same time, very scary. and PHP Wood has a good database connection pool drive scheme, so we have to find another way.

There are three ways to solve this problem:
1. Use MySQL proxy middleware. Mysql Proxy provides the ability to manage connection pools. But we didn't use this method because it was urgent and nobody knew it.
2. Use PHP-FPM. PHP-FPM is a fastcgi process manager for PHP. Configuration allows you to control the number of processes that process PHP requests concurrently.
For details, refer to: http://www.linuxde.net/2013/06/14638.html
But we also don't use this scenario because the installation configuration process is cumbersome.
3. Use Nginx Ngx_http_limit_req_module to control the request.
This module can limit the frequency of requests through custom key values. The limiting method is like a funnel, fixed processing requests per second, then deferred exceeding the request, and finally exceeding the maximum value of the direct 503 return reject.
We used this scenario because of the simple configuration and the flexibility to control the scenario where the request was restricted. For example, for a static resource request we do not restrict the request to PHP. You can also extract the variable information from the URL address as a key to achieve a finer request limit.
The following section of our configuration for you to explain the next.

HTTP {... limit_req_zone$limit _keyZone=limit_one: -M rate= -r/s;#定义limit_key为Key的变量名, used for subsequent assignments, each key has its own counter. Limit_one is the name of the zone. Rate indicates a maximum of 30 simultaneous requests per second. server {...if($request _uri~*. *php.*) {Set $mp _limit_key $binary _remote_addr;#对于全部PHP首先有个默认的Key, use the client's IP as key. The equivalent of each client IP will be within the zone limit. }if($query _string~*. *id/(\d+) \.php.*) {Set $mp _limit_key  $;#提取id后面的值作为Key. }if($query _string~*. *APPID/WX (. *) \.html.*) {Set $mp _limit_key  $;#提取appid作为Key. } limit_req Zone=limit_one burst= $;#限制limit_one在此server内的漏斗容量为200. Assuming that a key corresponds to a request number of 200, then the first second within the processing of 30 requests, the remaining 170 requests are waiting to be queued. Assuming a key corresponds to a request number of 300, then the portion that exceeds 200 will return 503 directly. .......    }}

Reference: http://www.ttlsa.com/nginx/nginx-limiting-the-number-of-requests-ngx_http_limit_req_module-module/
Official Document: Http://nginx.org/en/docs/http/ngx_http_limit_req_module.html

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

PHP No database connection pool how to break? High load solution using Nginx ngx_http_limit_req_module module in PHP environment

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.