PHP-CP (Php-connect-pool) is a database connection pool written in PHP extension.
We know that PHP development is fast, suitable for fast iteration, but when the traffic is large, PHP a large number of short connections to the DB layer caused unnecessary consumption, while the PHP processing request during the connection will be held in addition to the process can not share the TCP connection will cause high MySQL connection, MySQL's performance will fall sharply as the number of connections increases, so many big companies are turning to Java, much of it because PHP doesn't have a connection pool! Of course you can use open source database middleware to alleviate this problem, but this project and the existing open source products have the following differences.
It differs from other open source database middleware products on the market:
1. It does not need to deploy the middleware cluster separately, it is the agent process running on the application server, reducing the layer of external dependencies, which makes the architecture simpler, fresher and more reliable.
2. Higher performance, reduced network transmission, it communicates with the PHP process through an efficient IPC, and avoids the consumption of protocol parsing.
3. Supports both Redis and MySQL without the need to deploy 2 separate middleware systems.
Two. Simple schematic diagram:
Database connection Pooling PHP-CP Introduction
Three. Technical features
1. Support Maximum minimum connection number configuration.
2. Support Low pressure automatic recovery connection (force and frequency configurable).
3. Smooth restart is supported.
4. Support the queuing mechanism of light connection.
5. Support both MySQL and Redis.
6. Simple to use, after simple integration of the framework (modify the new method), the existing business line of code is not changed to use the connection pool.
7. The Get_disable_list function is provided to obtain a list of unavailable IP down IPs, so that load balancing can also be done on the client side (all IP and downtime IP of the configuration file are set, then randomly).
BTW: You can also use LVS, but LVS forwarding in the system architecture to introduce dependency, Dr Mode can not cross the network segment and limit the expansion, and back-end db problem can only know the VIP of LVS.
8. The connection pooling process initiates a ping process to listen to the outage list, which is reflected on the return value of the Get_disable_list function if available.
9. A lot of optimization, although the request through the connection pool process forwarding, but basically no QPS loss.
Four. Use it
1. Put the Pool.ini file in the/etc/and modify the configuration as needed.
2. Start the agent process
./pool_server Start
Support for "Start" "Stop" "Restart" "Reload" command
3. Modify the PHP script
$db = new PDO (xxxxx);
Modified to $db = new Pdo_connect_pool (xxxx);//dont Use persistent
$redis = new Redis ();
Modified to $redis = new Redis_connect_pool ();//dont use Pconnect
Tip: Call $db/$redis->release () Early to release the connection occupied by this process into the pool;
Five. API
Get_disable_list ($pdo _config,cp_default_pdo_port);
Get_disable_list ($redis _conf,cp_default_redis_port);
-the first parameter is your configuration file.
-If the configuration file changes, the unavailable list will be emptied
-Returns the failed database IP.
Six. Stress test:
1. With connection pool:
The PHP script is as follows:
$obj = new Pdo_connect_pool (' Mysql:host=192.168.20.130;dbname=test1 ', "admin", "admin");
$stmt = $obj->query ("Show Tables");
$data = $stmt->fetchall ();
Var_dump ($data);
$obj->release ();
The number of requests completed by 30s:
Database connection pool PHP-CP Introduction completed approximately 19W requests
MySQL Server CPU usage:
Database connection Pool PHP-CP describes the MySQL server consumes 52% of the CPU
2. Short connection pressure measurement, without connection pool
The PHP script is as follows:
$obj = new PDO (' Mysql:host=192.168.20.130;dbname=test1 ', "admin", "admin");
$stmt = $obj->query ("Show Tables");
$data = $stmt->fetchall ();
Var_dump ($data);
30S Completion Request:
Database connection pool PHP-CP Introduction completed approximately 12w requests
MySQL Server CPU usage:
Database connection pool PHP-CP about 122% CPU usage
The visible connection pool, although forwarded by request, reduces the time it takes to establish and release TCP, with a significant increase in the total QPS and a significant reduction in the load on the MySQL server.
The above pressure measuring machine is debian,4core machine.
Seven. Installation:
Phpize=>./configure=>make Install=>echo "extensions=xx/connect_pool.so" >php.ini
Need:
PHP 5.3 + (no zts)
Linux 2.6+
And the PDO and Redis extensions are already installed
Original address: http://blog.sina.com.cn/s/blog_9eaa0f400102v9fd.html
Database connection Pooling PHP-CP Introduction