Collection + Concurrent Update

Dustin Sallings dustin at spy.net
Thu Feb 21 21:45:20 UTC 2008


   cas was created for this scenario.

--  
Dustin Sallings (mobile)

On Feb 21, 2008, at 13:23, "Rakesh Rajan" <rakeshxp at gmail.com> wrote:

> 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