Can memcached be used for locking?

2020-07-30 03:39发布

问题:

memcached can be used for a caching static data which reduces database lookup and typically does memcached.get(id) and memcached.set(id).

However is it fine to use this for locking mechanisms? Does memcache.set and memcached.get always give the data if it is present or will it just return None if the request is taking too much time?

I want to avoid concurrent access to a particular resource identified by a id and I use this logic:

def access(id):
    if memcache.get(id):
        return access
    else:
        memcache.set(id)
        return true

If any user tries to access that resource, if memcache.get(id) = username returns a value we decline the access else we do memcache.set(id) = username to stop subsequent access and allow access for the current user.

Is it fine to using memcached like this? Will set and get actually give the data if its available regardless of the time it takes or does it give best possible result in the least possible time from whatever I have found (for example: Guaranteed memcached lock) so far is of the former category and might not work for locking thought it might work 99% of the time.

Can anyone clarify and if there are alternative locking mechanisms?

回答1:

For anyone intersted in this, I have created a thread on Memcache Github at Will memcached work reliably for implementing a locking mechanism?. It explains some of the common caveats using get and set and how to avoid that using add. Some blogs also explain this problem if you can search for distributed locking using memcache on your favorite search engine.

There is also a related question Memcached, Locking and Race Conditions which might help on getting more clarity on memcache race conditions.

Here is more discussions on this at the Memcache Forum:

Thread 1 and Thread 2