• UID625
  • Fans5
  • Follows1
  • Posts68

Use PHP long connection to improve the performance of using ApsaraDB for memcache

More Posted time:Aug 3, 2016 14:22 PM
Use PHP long connection to improve the performance of using ApsaraDB for memcache

Recently, some PHP users feed back that the performance test results of ApsaraDB for memcache cannot reach the expected performance indexes. After knowing the specific conditions, most users use PHP to connect the ApsaraDB for memcache through Apache WEB service and then connect to the ApsaraDB for memcache (short connection). The overhead of each short connection is not only the socket reconnection, but also has complex reauthentication process. The overhead is much larger than an ordinary request, thus it has great impact on the efficiency of the website. We recommend that users change short connection to long connection, but the PHP MEMCACHED extension required for the ApsaraDB for memcache is different from the memcache extension which has a pconnect interface. How to set up long connection in PHP?

The following instructions are provided for your reference.The following passage is extracted from the official website of PHP to introduce the memcached constructor:http://php.net/manual/zh/memcached.construct.php

Memcached::_construct ([string$persistent_id]) create a representative Memcached instance to the Memcached server.
By default of persistent_id, the Memcached instance will be destroyed after end of the request. An unique ID can be assigned to each instance via persistent_id during creation, and the instance can be shared among requests. All instances created via the same persistent_id value share one connection.

That is, when the constructor is called, a same persistent_id can be sent to it, and then sharing connection can be implemented. The code can be implemented as follows:

$memc=new Memcached(‘ocs’);// here the ocs is persistent_id
if(count($memc->getServerList())==0)/* judge before connection*/
echo"New connection"."

/*all options are incorporated into the judgment, because some options may lead to reconnection and change long connection to short connection!*/
/*addServer code must be incorporated into the judgment, or it is equivalent to create the connection pool "ocs", which may cause abnormality to client php program */
$memc->addServer("your_ip", 11212);
 $memc->setSaslAuthData("user", "password");
 echo "Now connections is:".count($memc->getServerList())."

$memc->set("key", "value");
echo "Get from OCS: ".$memc->get("key");
//$memc->quit();/* The quit shall be not added to the end of a code, or it will become short connection! */

Three places of the above code requiring special attention are noted. The ‘ocs‘ keyword in the constructor is equivalent to a connection pool, and the connection to be used can get connection from the pool by calling new Memcached(‘ocs’). Then see the execution results.Put the above code under the working path/var/www/html/ of Apache, name it as test.php. and input it into the browser.http://your_ip:80/test.php. The last 8 output results of the browser are as follows:

Get from ApsaraDB for memcache:value

The 8 times are new connection, and these 8 links are reused after 8 times. The packet capture results show that they are long connections after 8 times, without socket reconnection and authentication. Why there are 8 links? Because the appache starts 8 subprocesses by viewing the configuration file httpd.conf:

#StartServers:numbers of server processes to start
StartServers        8

Initialize 8 connections in the persistent_id connection pool of ‘ocs’. The requests hereafter use these 8 links. Then jump the test page and copy a php file. The persistent_id of constructor creating the connections also uses ’ocs’. The result is change from one connection to another connection (for the called Apache subprocesses are different), without authentication and socket reconnection process. The long connection setting of PHP memcached is available. We often use the PHP-FPM mode, and the FPM process will keep long connection with memcached server, thus the life cycle of the connection is the same as the Apache process.