How does nginx-mysql + php solve the problem of insufficient port ing under high concurrency?

Source: Internet
Author: User
Tags unix domain socket
The current situation is that the small-scale 1mysql server 1web server data operation is very fast dozens of ms-level processing logic, but each access to the local will map a port to access 3306 so that in the case of high concurrency, the port will it will not be mapped out. For example, if the second concurrency is 50030 seconds, it will be enough to map 15000... the current situation is that small-scale 1 MySQL Server 1 web server data operations are very fast, dozens of ms-level processing logic
However, a port is mapped to the local host for each access to access 3306. In this case, the port cannot be mapped out in the case of high concurrency.
For example, if the second concurrency is 500, 30 seconds is enough to map 15000 ports, but the time_wait release in linux takes 60 seconds by default.
It seems useless to try pdo persistent connections.
With unixodbc enabled pooling, there is about 20% probability of crash. php-fpm is directly killed.
In addition, this scenario is relatively simple. The entire system will only enable one connection, and the connection will not be switched Based on the permission or scenario.

Reply content:

The current situation is that small-scale 1 MySQL Server 1 web server data operations are very fast, dozens of ms-level processing logic
However, a port is mapped to the local host for each access to access 3306. In this case, the port cannot be mapped out in the case of high concurrency.
For example, if the second concurrency is 500, 30 seconds is enough to map 15000 ports, but the time_wait release in linux takes 60 seconds by default.
It seems useless to try pdo persistent connections.
With unixodbc enabled pooling, there is about 20% probability of crash. php-fpm is directly killed.
In addition, this scenario is relatively simple. The entire system will only enable one connection, and the connection will not be switched Based on the permission or scenario.

Nginx and PHP-FPM communicate with UNIX Domain Socket (without passing through TCP ),
PHP-FPM and MySQL with persistent connections (a fpm worker maintains a persistent connection to MySQL ),
This avoids the problem of too many TIME_WAIT connections due to frequent TCP short connections.

That is, 2 PHP-FPM working process and MySQL established 2 persistent database connection, the connection is in Sleep state when no operation:


   true,    PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));print_r($dbh);// mysql -uroot -p -e "show processlist"// sudo netstat -antp|egrep "php|mysqld"|grep ESTABLISHED

Keep the php script executed as soon as possible, and put the delayed operations in the queue, so that 500 concurrent 30 seconds, release the port quickly (or the tcp connection is reuse and can be reused soon ).
If you are not normally disconnected when you connect to mysql or when your http is connected, time_wait may occur if you do not close the connection to mysql or directly close the response without sending an http request. It is very difficult for the system to reclaim time_wait, finally, the port is exhausted.
For more information about time_wait, see the four handshakes During disconnection.

If the concurrency is too large, consider distributed.

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.