All Products
Search
Document Center

ApsaraDB for Memcache:Improve ApsaraDB for Memcache performance through PHP persistent connections

Last Updated:Aug 08, 2023

Problem description

Some PHP users experienced that the performance of ApsaraDB for Memcache fails to meet the expected indexes through tests. We followed up on the situation and found that most users go through the Apache web service to connect to ApsaraDB for Memcache through PHP, using short connections. The overhead of every short connection not only includes socket reconnection, but also complicated re-authentication procedures, being much larger than that of a general request. This impacts the website efficiency greatly.

Solution

We recommend users change short connections to persistent connections. But ApsaraDB for Memcache requires the use of PHP Memcached extensions which do not possess the pconnect interface as memcache extensions do. Following is a tutorial on how to establish persistent connections in PHP, for your reference.

On the PHP official website, the introduction of memcached constructor includes the following:

Note:
Memcached::__construct ([ string $persistent_id ] ): Create a Memcached instance representing the connection to the Memcached service end.
Parameter
Persistent_id: By default, the Memcached instance is destroyed after the request is over. But you can specify a unique ID for every instance through the persistent_id at the instance creation to share the instance between requests. All the instances created using the same persistent_id share the same connection.

That is, you must pass an identical persistent_id to the constructor called to achieve shared connections. The code implementation is as follows:

<?php
 $memc = new Memcached(‘ocs’);//ocs refers to persistent_id
if (count($memc->getServerList()) == 0) /*Before establishing the connection, first judge*/
{
  echo "New connection"."<br>";

/*All options must be included in the evaluation, because some options may lead to reconnection, or cause a persistent connection to become a short connection.*/
  $memc->setOption(Memcached::OPT_COMPRESSION, false);
  $memc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
  $memc->setOption(Memcached::OPT_TCP_NODELAY, true); //Important: The php memcached has a bug that when the get value does not exist, there is a fixed 40 ms of latency. Enabling this parameter can avoid this bug.

  /* The addServer code must be included in the evaluation, otherwise it is equal to repeating the establishment of the ‘ocs’ connection pool, which may lead to exceptions in the client PHP program.*/
  $memc->addServer("your_ip", 11212);
  $memc->setSaslAuthData("user", "password");
}
else
{
  echo "Now connections is:".count($memc->getServerList())."<br>";
}
$memc->set("key", "value");
echo "Get from OCS: ".$memc->get("key");
//$memc->quit();/*Do not add ‘quit’ at the end of the code, otherwise persistent connections is changed to short connections. */
?>

Annotations are provided at the three points worth special attention in the preceding code. The keyword of ocs in the constructor is equivalent to a connection pool. Call new Memcached (ocs) and you can obtain the connection from the pool for use.

Execution results

Place the preceding code under the Apache working path /var/www/html/, and name it test.php. Enter in the browser: Http://your_ip:80/test.php.All the output results of the first eight attempts in the browser are:

New connection
Get from OCS: value

That is, all these eight attempts create new connections, while the subsequent attempts reuse these eight connections. The packet capture results also show that after the first eight attempts, connections are persistent, without socket reconnection or authentication.

After checking the httpd.conf file, it is clear that Apache started eight subprocesses and that is the reason for eight connections.

#StartServers: numbers of server processes to start
 StartServers        8

As a result, eight connections are initialized in the connection pool with the ocs persistent_id and later requests can use the eight connections.

Page navigation

Copy a php file, and establish the persistent_id of the constructor. We still use ocs. The result is that the connection is switched (because the called Apache sub processes are different), but with no authentication or socket reconnection. That is, the PHP memcached persistent connection settings are effective. Usually we use the PHP-FPM mode. The FPM process keeps a persistent connection with the memcached server. As a result, the lifecycle of this connection is the same with that of the Apache process.