I was looking at the php docs on memcache and noticed that instead of doing
$mem->connect(\'localhost\', 11211)
I can do instead
$mem->addServer
Memcache is a distributed cache by design. When you use a pool of servers, the objects are stored on all the servers by a key distribution mechanism that selects a server based on the weight of server and the hash of the key itself.
Now, in a setup on 2 memcache servers, one would go like this:
$memcache = new Memcache;
$memcache->addServer('memcache_host1', 11211);
$memcache->addServer('memcache_host2', 11211);
post these calls, the php process will see the server pool with two servers and distribute the objects evenly to them as the default weights are selected in Memcache::addServer calls. So, a call to Memcache::get or Memcache::set would save an object or retrieve a key value from the right host from the server pool depending on the key.
Memcache::connect, however re-initializes the host pool and assumes that there is only a single host available, and all the objects are stored on that host. This also means, that if you do this:
$memcache = new Memcache;
$memcache->addServer('memcache_host1', 11211);
$memcache->addServer('memcache_host2', 11211);
$memcache->connect('memcache_host1', 11211);
The last call would clear the server pool and all the keys after the the Memcache::connect call will be saved at memcache_host1. So Memcache::connect is ideally used on a single host setup and never with a pool unless the intention is to talk to a specific host in a pool for getting stats, maintenance operations or special caching schemes. More discussion here.