Force memcached to write to all servers in Pool for reliability

108 views
Skip to first unread message

Jay

unread,
May 23, 2010, 6:11:21 PM5/23/10
to memcached
Hi everyone,

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

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

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

foreach ($this->cluster() as $cluster) {
@$this->tempMemcache = new Memcache;
@$this->tempMemcache->connect($cluster['host'],
$cluster['port']);
@$this->tempMemcache->set($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?

Artur Ejsmont

unread,
May 23, 2010, 8:14:13 PM5/23/10
to memc...@googlegroups.com
i guess you could do it this way  - may be slow though as its not asyncronous so has to write to all instances.

No matter what you do using @ operator is a very bad idea. this is how undetectable bugs happen, id check for response and throw exception if error is important.

if you have many servers would probably be much more efficient to have some kind of consistent hashing function so that each key would be saved into 2 servers only. You will not have multiple failures unless you have tons of servers. Then you save each key in 2 servers not N, makes save much much faster and saves you space.

regards

art

Brian Aker

unread,
May 23, 2010, 8:39:54 PM5/23/10
to memc...@googlegroups.com
Hi!

On May 23, 2010, at 3:11 PM, Jay wrote:

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

For libmemcached turn on replica, and set it to the number of hosts you have.

Cheers,
-Brian

Jay

unread,
May 24, 2010, 3:24:06 AM5/24/10
to memcached
Hi! That's an option of course. I just had the @operators set now to
prevent those nasty errors when running Memcached in a non-reliable
LAN environment.
Would you make a new instance to do this or makes it sense to use the
standard instance to save this keys, when utilizing consistent hashing
on my normal memcache instance?

Thanks!

On 24 Maj, 02:14, Artur Ejsmont <ejsmont.ar...@gmail.com> wrote:
> i guess you could do it this way  - may be slow though as its not
> asyncronous so has to write to all instances.
>
> No matter what you do using @ operator is a very bad idea. this is how
> undetectable bugs happen, id check for response and throw exception if error
> is important.
>
> if you have many servers would probably be much more efficient to have some
> kind of consistent hashing function so that each key would be saved into 2
> servers only. You will not have multiple failures unless you have tons of
> servers. Then you save each key in 2 servers not N, makes save much much
> faster and saves you space.
>
> regards
>
> art
>

Artur Ejsmont

unread,
May 24, 2010, 4:41:46 AM5/24/10
to memc...@googlegroups.com
hi, im not sure if i understand what you mean.

I am not using this approach myself so im not sure if there is out of the box solution to your problem.

If you wish to control caching and distribution etc you would write some classes to hide the complexity from the rest of the application. Then implement which ever way you like.

I guess you would need persistent connections to speed things up. unless you have too many apache processes and cant afford to keep sockets all the time. then save data to 2 boxes (if any of saves fails rise alert). On read also hide complexity reading from any of 2 boxes in pair. If fails rise alert and try the second one. then you should have even spread of load.

art
Message has been deleted

Jay

unread,
May 25, 2010, 5:20:43 PM5/25/10
to memcached
Hi Brian,

I don't know if you have received my last message here. Written a
couple of times on here, but nothing appears (checked in after few
hours after original write to verify, but no).

I have searched under every rock I could find (read googled like
hell), but haven't found any information or documentation about
replica functions in libmemcached / PECL memcached?

Would you mind telling me more about it?
Thanks!

Jay

unread,
May 25, 2010, 5:27:00 PM5/25/10
to memcached
Hi Artur,

Google groups seems to be a bit fluctuating right now. Trying to post
here anyway.

Would you mind giving an example on how you would do it? Do you think
that a second instance for this case is a bad practice?
Persistent connections is a good idea. Definitely.

Thanks a lot for your help!

On 24 Maj, 10:41, Artur Ejsmont <ejsmont.ar...@gmail.com> wrote:
> hi, im not sure if i understand what you mean.
>
> I am not using this approach myself so im not sure if there is out of the
> box solution to your problem.
>
> If you wish to control caching and distribution etc you would write some
> classes to hide the complexity from the rest of the application. Then
> implement which ever way you like.
>
> I guess you would need persistent connections to speed things up. unless you
> have too many apache processes and cant afford to keep sockets all the time.
> then save data to 2 boxes (if any of saves fails rise alert). On read also
> hide complexity reading from any of 2 boxes in pair. If fails rise alert and
> try the second one. then you should have even spread of load.
>
> art
>

Eric Lambert

unread,
May 25, 2010, 5:34:25 PM5/25/10
to memc...@googlegroups.com
Hi Jay:

Trond Norbye, who added this feature into libmemcached, wrote a blog
entry describing how to use libmecached for replication. You can find it
at http://blogs.sun.com/trond/entry/replicate_your_keys_to_multiple

Good luck

Eric

Brian Moon

unread,
May 25, 2010, 5:39:38 PM5/25/10
to memc...@googlegroups.com, Eric Lambert
That is not super helpful however for users of pecl/memcached.

From Andrei's blog:

"I�m working on next version (2.0) of the memcached extension. It�ll
have UDP support and replication (failover)."

May want to dig into the gitub repo for it.
http://github.com/andreiz/php-memcached

Brian.
--------
http://brian.moonspot.net/

Jay

unread,
May 25, 2010, 6:17:36 PM5/25/10
to memcached
Hi!

What about doing something like this?

$m = new Memcached();
$m->addServer('localhost', 11211);

foreach ($array as $server) {
$m->setByKey('api-cache', 'block-ip:169.254.253.252',
$server['id']);
}

Would that be a better idea?
Remember, I am not going to fill up my memcached server like this :)

On 25 Maj, 23:39, Brian Moon <br...@moonspot.net> wrote:
> That is not super helpful however for users of pecl/memcached.
>
>  From Andrei's blog:
>

> "I m working on next version (2.0) of the memcached extension. It ll


> have UDP support and replication (failover)."
>
> May want to dig into the gitub repo for it.http://github.com/andreiz/php-memcached
>
> Brian.

> --------http://brian.moonspot.net/


>
> On 5/25/10 4:34 PM, Eric Lambert wrote:
>
> > Hi Jay:
>
> > Trond Norbye, who added this feature into libmemcached, wrote a blog
> > entry describing how to use libmecached for replication. You can find it

> > athttp://blogs.sun.com/trond/entry/replicate_your_keys_to_multiple

Reply all
Reply to author
Forward
0 new messages