4

The Memcached (not Memcache) PECL plugin for PHP accepts a $persistent_id as part of its constructor.

The docs say:

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.

What is the best way to use (or not use) $persistent_id on a site with multiple webservers and several shared memcached servers? In particular, if I assign the same $persistent_id always, will the "shared instance" become a bottleneck, while different php processes wait for the instance? Can additional instances with the same $persistent_id be created, by, for example, a race condition? What happens to those additional connections, if that happens? What happens to those instances between processes?

At the very least, according to my tests, more than one "connection" can have the same $persistent_id. I did a simple test where I created Memcached instances with the same $persistent_id and added different servers to them, then loaded and echoed out servers; sometimes I would get the first set, sometimes the second, always using the same $persistent_id.

Additionally, it is clear that there are issues when adding servers to one of these shared instances. Because Memcached allow you to add the same server multiple times, I've seen advice that one should check the servers with getServerList() before adding servers, and only add them if the server list is not populated. Depending on the degree to which these instances are shared, this seems like a potential race condition--is it possible that two processes could simultaneously add servers to the same instance, corrupting the server list?

Is there a way to safely share Memcached instances between processes without race conditions? Is it worth doing?

Karptonite
  • 1,490
  • 2
  • 14
  • 30
  • Hmm... My question appears to be related to this question, which received no replies. http://stackoverflow.com/questions/19451582/persistent-memcached-in-php-server-pool-growing-until-curr-connections-10?rq=1 – Karptonite Jul 21 '14 at 18:28

1 Answers1

1

Setting same persistent_id for you MemCache server is a normal case when you want a pool of servers.

Example from the doc :

/* Create a persistent instance */
$m2 = new Memcached('story_pool');
$m3 = new Memcached('story_pool');

/* now $m2 and $m3 share the same connection */

Note that even using persistent id you must re-add your servers, see http://php.net/manual/en/memcached.construct.php#93536

See also this important note : http://php.net/manual/en/memcached.construct.php#106865

Your requests are independent. PHP doesn't share its requests. So If you've 2 (or more) requests with the same persistent_id both will live into their own process.

Kevin Labécot
  • 2,005
  • 13
  • 25
  • if i remember correctly memcached run persistently (outside the php scripts execution) so i think you can save the persistent_id and come back to use it on another run of the php file – Barkermn01 Jul 29 '14 at 17:15
  • You say you must re-add your servers, but notice that the example you sent me to shows re-adding of servers only when the servers are not already set. The connection definitely persists after the request terminates. I'm trying to figure out why it seems that when you use a persistent_id, sometimes you get the same connection, and sometimes you don't, and if there is any risk of problems because of that. – Karptonite Jul 29 '14 at 21:12
  • Anyway, you must manage this case. You can't expect to get same connection permanently. If for any reason the connection has been lost your must reopen it. The comment above (106865), check for empty(getServerList()). If empty, connections are re-opened. For me, there is no risk to get the same connection when you use persistent_id. – Kevin Labécot Jul 30 '14 at 05:30