Collection + Concurrent Update

Reinis Rozitis roze at roze.lv
Thu Feb 21 20:18:18 UTC 2008


Hearing term 'lock' and 'locking' near memcache just doesnt sound the 
correct way to do things... Its ment to be a nonblocking cache ..

I am not sure if there is implementation in java client  (using only php 
pecl extension) but the thing you prolly want to look at is CAS (compare and 
swap) http://code.sixapart.com/svn/memcached/trunk/server/doc/protocol.txt

rr

----- Original Message ----- 
From: Rakesh Rajan
To: memcached at lists.danga.com
Sent: Thursday, February 21, 2008 8:23 PM
Subject: Collection + Concurrent Update


Hi,

I am using memcached to store a lot of collections. An example would be, for 
a given item, a collection of integers representing various attributes. Now, 
since there can be concurrent update to the collection ( addition / deletion 
/ updation ), there are times when the cached data gets stale/dirty. Example
Current Cache [ 1,2]

   Thread 1 ( Added 3 ) : [1,2,3]
   Thread 2 ( Added 4 ) : [1,2,4]
   Thread 3 ( Removed 2 ) : [1]

Now when all 3 threads tries to update the cache, cache becomes dirty. One 
way to solve this is, for every set operation, I would need to get the 
current cached collection and then perform operation ( 
addition/deletion/updation) on it before setting it back ( with the write 
operation acquiring a lock first ).

For a typical write, I do
    -> Take a lock ( with ~1-2 secs timeout)
    -> Fetch the current cache value ( if any)
       -> Perform the operation between the cached data and the newer data 
( addition/deletion/updation)
    -> Update the cache with the newer collection
    -> Release the lock ( this is purely a safety mechanism : in the finally 
block of the java code )

So every write operation, I need to make 4 memcached calls which I believe 
is not a good solution ( since it is in the same thread). One possible 
solution that I was thinking was making the cache update async process. 
Would love to hear how this case is generally solved / opinions ?  ( In my 
case collections are either Set / List )

-Rakesh 



More information about the memcached mailing list