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?
Unlike databases that store data on disk or SSDs, Memcached keeps its data in memory. By eliminating the need to access disks, in-memory key-value stores such as Memcached avoid seek time delays and can access data in microseconds. Memcached is also distributed, meaning that it is easy to scale out by adding new nodes.
Memcached (pronounced variously mem-cash-dee or mem-cashed) is a general-purpose distributed memory-caching system. It is often used to speed up dynamic database-driven websites by caching data and objects in RAM to reduce the number of times an external data source (such as a database or API) must be read.
First, when memcached gets full, it will start removing items from the cache using the Least Recently Used algorithm. Second, you can have multiple instances of memcached running, adding them gets complicated because it will change the hashing algorithm used to determine which cache the data is in.
Summary. The memcached service should not be exposed to the internet or to any untrusted users. Misconfigured servers that externally expose the memcached service are vulnerable to exploitation to perform amplified Distributed Denial of Service (DDoS) attacks.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With