Question

My understanding is that Memcached is lockless (in its default configuration), such that no client can impede any other client's operation. If that is the case, then how does Memcached handle two or more clients attempting to update the same object at the same time?

How does Memcached handle multiple versions of an object? Simply take the one whose update finished last and evict the other versions from the cache? Is the database actively invalidating objects cached on a Memcached server?

Was it helpful?

Solution

Memcached hold only last version of object. If you get, modify and put you have no guarantee that the put applies to the same value as the get. You could use CAS operations (gets, cas) if you need to synchronize.

OTHER TIPS

Adding some details on how it works. CAS - Compare and Swap or Check and set.

Memcached 1.2.5 and higher versions provide the gets and cas commands, which can solve the above problems. If you use the gets command to query the cache of a key, it will return you the unique identifier of the item's current value. If you overwrite this item and want to write it back to Memcached, you can send the unique ID to Memcached through the cas command. If the unique identifier of the item stored in Memcached is consistent with the one you provided, your write operation will be successful. If another process also modifies the item during this period, the unique identifier of the item stored in Memcached will change, and your write operation will fail.

The key/value saved by Memcached has a unique identifier casUnique. When performing incr/decr operations, first obtain casUnique, execute incr, and check whether the return value is casUnique+1. If it is, update it, otherwise, it fails and does not update!

https://developpaper.com/question/why-does-incr-of-memcache-not-have-concurrency-problems/

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top