Re: Memcache on multiple servers is safe to use from multiple http servers ?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



On 18/03/2014 05:12, Cristian Bichis wrote:
> Hello,
> 
> I am wondering if is safe to use memcache extension with multiple
> memcache servers used from multiple http/php servers ?
> 
> I am asking this because I dont know how memcache extension is handling
> internally selecting the server where to get/set values. Because using
> multiple http servers I am not sure if for the same key all the http
> servers will query the same memcache server ?
> 
> A sample from http/php server 1:
> 
> $memcache = new Memcache;
> $memcache->addServer('memcache_host1', 11211);
> $memcache->addServer('memcache_host2', 11211);
> 
> $memcache_obj->set('var_key', 'value A');
> 
> echo $memcache_obj->get('var_key');
> 
> A sample from http/php server 2:
> 
> $memcache = new Memcache;
> $memcache->addServer('memcache_host1', 11211);
> $memcache->addServer('memcache_host2', 11211);
> 
> $memcache_obj->set('var_key', 'value B');
> 
> echo $memcache_obj->get('var_key');
> 
> 
> We can be sure both http/php servers 1 and 2 will set/get the var_key to
> the same memcache server  ? Because otherwise it could be like this:
> 
> 1. http server 1 is writing var_key to memcache_host1
> 2. http server 2 has an update and is writing updated var_key to
> memcache_host2
> 3. at a later time http server 1 is trying to get the var_key and is
> getting this from memcache_host1 => getting an outdated value ISSUE!
> 
> So how safe is to use memcache extension with multiple memcache servers
> from multiple http/php servers ?
> 
> Cristian
> 
Hi,

I use multiple memcached instances on ONE server.  Whilst setting this
up I found the following comment in the manual for the method
Memcache::addServer [http://www.php.net/manual/en/memcache.addserver.php] :

------------
rstaveley at seseit dot com 17-Jun-2011 01:06

The Memcache client library is responsible for picking the right server
to set/get data. That's why addServer is what you want to use rather
than connect, when you have more than one Memcache server. A subsequent
set/get will then connect on demand to the appropriate instance as needs
be. Disconnection to whatever servers were connected to happening when
you close or your script terminates.

Memcache instances added to your Memcache object via addServer should be
added in the same order in your application to ensure that the same
server is picked for use with the same key.

A client library may be implemented to run a CRC on the key and do a
modulus over the number of instances in the list to select an instance
from the list for the set/get. This ensures that data is spread nicely
across the nodes.

That all works nicely behind the scenes for you in your PHP code, as
long as you add your list of Memcache instances in a consistent manner
with addServer.
--------------

This is true for the one server I have tested with, to find out if it
works across multiple servers too you will have to test it yourself.

Please let us know the result!

Regards

Ian
--



-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php





[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux