mutex on memcached

339 views
Skip to first unread message

juanbackson

unread,
Nov 24, 2009, 11:11:58 AM11/24/09
to memcached
Hi,

I have two distributed clients that need to write stuff to memcached,
but each write will involve two records. These two records must be
sync. So, they must be changed together.

Is there anyway that I can lock on those two records and write them,
so there won't be issue with two clients writing at the same time?

thanks,
jb

Boris Partensky

unread,
Nov 24, 2009, 11:35:37 AM11/24/09
to memc...@googlegroups.com
Not really. If could synchronize it externally using some kind of a distributed lock server.
Or you could "wrap" those 2 keys into an object of some sort and write out the object. This way it's atomic.

--
--Boris

Matt Ingenthron

unread,
Nov 24, 2009, 11:55:41 AM11/24/09
to memc...@googlegroups.com
Hi JB,

Just a couple of quick thoughts, and others will likely jump in as well.

juanbackson wrote:
> Hi,
>
> I have two distributed clients that need to write stuff to memcached,
> but each write will involve two records. These two records must be
> sync. So, they must be changed together.
>

You may be stretching the limits of what memcached is designed for
here. By being simple, memcached is very, very fast and quite scalable,
but doesn't provide features like distributed locks.

> Is there anyway that I can lock on those two records and write them,
> so there won't be issue with two clients writing at the same time?
>

There are no locks available to clients of memcached, but with the use
case you describe it should be possible to put both records into the
same item stored in memcached. Since that item will always be
consistent, you can be sure no two clients will write at the same time.

You may also look into the CAS operations which are part of memcached.
With CAS, memcached allows one to implement some lock-free algorithms
which can ensure, for instance, that a client will know that no other
client has updated an item since that client last fetched it. It's not
the same, but locking has overhead and you can accomplish a lot of
things lock-free with CAS. It could fit your case, depending on what
the relationship between those two items is.

Beyond that, there are some other distributed lock managers which one
could use alongside memcached, but you're going further and further away
from the common memcached deployments if we start talking about DLM.

Hope that helps,

- Matt

Mark Atwood

unread,
Nov 24, 2009, 11:57:52 AM11/24/09
to memc...@googlegroups.com
If any one record is always associated with one unique other record,
that is, if A is always a pair with G, and never with any other
record, and so forth...

Then just treat each pairing as a single item, concatenate their
record ids together to make the key, and write that.


--
Mark Atwood <http://mark.atwood.name>



Reply all
Reply to author
Forward
0 new messages