All Products
Search
Document Center

ApsaraDB for Memcache:Use PHP persistent connections to improve performance

Last Updated:Aug 24, 2026

Problem description

Some PHP users report that their performance tests against ApsaraDB for Memcache fall short of the expected performance metrics. In most of these cases, the application reaches ApsaraDB for Memcache through the Apache web service and uses short-lived connections. Each short-lived connection costs more than a socket reconnection, because it also runs a complex re-authentication procedure. This cost is much higher than the cost of an ordinary request, so it significantly reduces the efficiency of your website.

Solution

We therefore recommend that you replace short-lived connections with persistent connections. However, ApsaraDB for Memcache requires the PHP Memcached extension, which, unlike the memcache extension, provides no pconnect interface. The following steps show you how to set up a persistent connection in PHP.

From the PHP Memcached documentation:

Description
Memcached::__construct ([ string $persistent_id ] ) Creates a Memcached instance representing the connection to the memcache servers.

Parameters
persistent_id By default the Memcached instances are destroyed at the end of the request. To create an instance that persists between requests, use persistent_id to specify a unique ID for the instance. All instances created with the same persistent_id will share the same connection.

That is, you share a connection by passing the same persistent_id to the constructor. The code is as follows:

<?php
 $memc = new Memcached('ocs');// The 'ocs' here is the persistent_id
if (count($memc->getServerList()) == 0) /* Check before you establish a connection */
{
  echo "New connection"."<br>";

/* Keep every option inside the condition, because some options cause a reconnection and turn a persistent connection into a short-lived one. */
  $memc->setOption(Memcached::OPT_COMPRESSION, false);
  $memc->setOption(Memcached::OPT_BINARY_PROTOCOL, true);
  $memc->setOption(Memcached::OPT_TCP_NODELAY, true); //Important. PHP Memcached has a bug that adds a fixed 40 ms delay when the value you get does not exist. Enable this option to avoid the bug.

  /* The addServer code must also stay inside the condition. Otherwise you build the 'ocs' connection pool again, which can cause exceptions in the PHP client 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();/* Never add quit at the end of the code. Otherwise the connection becomes short-lived. */
?>

The three points that need your attention in the preceding code are all marked with comments. The ocs keyword in the constructor works as a connection pool: any code that needs a connection calls new Memcached('ocs') and takes one from the pool.

Result

Place the preceding code in the Apache working directory /var/www/html/, name it test.php, and then enter http://your_ip:80/test.php in the browser. For the first 8 requests, the browser prints the following output:

New connection
Get from OCS: value

That is, each of these 8 requests creates a new connection, and every request after them reuses one of the 8 connections. A packet capture also shows that all traffic after the 8th request runs over persistent connections, with no socket reconnection and no authentication.

Why are there 8 connections? The httpd.conf configuration file shows that Apache starts 8 child processes:

#StartServers: numbers of server processes to start
 StartServers        8

The connection pool of the ocs persistent_id therefore initializes exactly 8 connections, and all later requests use these 8 connections.

Next, test page navigation. Copy the PHP file and keep ocs as the persistent_id of the constructor that establishes the connection. The request moves from one connection to another, because a different Apache child process handles it, but no authentication and no socket reconnection occur. This proves that the PHP Memcached persistent connection setting works. PHP-FPM is the mode that we normally use: the FPM process keeps a persistent connection to the Memcached server, so the lifecycle of this connection is the same as the lifecycle of the Apache process.