Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Force memcached to write to all servers in pool

I have thought a bit on how to make sure that a particular key is distributed to ALL memcached servers in a pool.

My current, untested solution is to make another instance of memcached, something like this:

$cluster[] = array('host' => '192.168.1.1', 'port' => '11211', 'weight' => 50);

$this->tempMemcached = new Memcached;
$this->tempMemcached->addServers($cluster);

foreach ($this->cluster() as $cluster) {    

    $this->tempMemcached->setByKey($cluster, $key, $value, $this->compress, $expireTime);

}

$this->tempMemcache->close();

What is common sense to do in this case, when certain keys need to be stored on ALL servers for reliability?

like image 334
Industrial Avatar asked Dec 22 '22 02:12

Industrial


2 Answers

I think you are not using memcached the way it was designed for. Have a look at the FAQ: you should only store a single copy of your item, which, according to the hashing algorithm used, would put your item on a certain node.

Now if you want to have an item available in all of the nodes, the only way to do this is by iterating, exactly as you are doing at the moment.

I hope your code does handle the cases when this item isn't found in the cache.

like image 194
mindas Avatar answered Jan 08 '23 23:01

mindas


I think you are missing the point of Memcached. It's not for reliable data storage. It's for VERY fast access to cached data. If you want redundancy, try a NOSQL database like MongoDB...

Besides, creating several connections is going to be bad for performance and redundancy (the more connections it needs to make, the greater chance something will go wrong, and the more it needs to do for each request).

Simplify, don't complicate...

like image 31
ircmaxell Avatar answered Jan 08 '23 23:01

ircmaxell