Is the add command atomic ?

dormando dormando at rydia.net
Wed Mar 26 18:57:48 UTC 2008


What do you mean by "atomic" ?

The add operation itself is "atomic", ie:

$mc->add('user1lock', 10);

... will bail if the key already exists, or return true if it did not exist.

However:

$mc->add('user1lock', 10);

$mc->set('blah blah blah');

... does _not_ *absolutely* guarantee the lock still exists by the time 
set gets called. 99.9995% of the time it'll still be in cache, but 
there's a small chance it could get ejected, which gets wider when 
there's more time between the add call and the set call, and depending 
on other factors.

So we can't say it's atomic, it's more like a ghetto lock that usually 
works. You can use it for situations where you want to reduce the number 
of processes running something in parallel, but you can't use it if you 
need to _guarantee_ they won't run it in parallel.

-Dormando

Fausto Richetti Blanco wrote:
> Hello,
> 
>  
> 
>             I’ve found this entry on the FAQ:
> 
>  
> 
> “One should be mindful about possible issues in populating or 
> repopulating our cache. Remember that the process of checking memcached, 
> fetching SQL, and storing into memcached, is **not** atomic at all!”
> 
>  
> 
>             I know it’s talking about the entire process, but I want to 
> confirm with you whether the add function is atomic or not. I want to 
> use memcache to simulate locking and this atomicity is very important to 
> the whole process.
> 
>  
> 
> Thanks in advance,
> 
>  
> 
> **Fausto Richetti Blanco**
> **CWI Software - CMMI Nível 2**
> 
> Desenvolvimento
> //(51) 3264-3737//
> 
> http://www.cwi.com.br <http://www.cwi.com.br/>
> 
>  
> 



More information about the memcached mailing list