race condition might happen when use memcached in such scenario

1,022 views
Skip to first unread message

biao zhang

unread,
Feb 28, 2013, 4:06:43 AM2/28/13
to memc...@googlegroups.com
 Hi, everyone

I found a question on Stack Overflow which is about race condition which is not easy to solve:
http://stackoverflow.com/questions/7802772/cache-consistency-when-using-memcached-and-a-rdbms-like-mysql

the question is:

I have taken a database class this semester and we are studying about maintaining cache consistency between the RDBMS and a cache server such as memcached. The consistency issues arise when there are race conditions. For example: 1. Suppose I do a get(key) from the cache and their is a cache miss. Because I get a cache miss, I fetch the data from the database, and then do a put(key,value) into the cache. 2. But, a race condition might happen, where some other user might delete the data I fetched from the database. This delete might happen before I do a put into the cache.

Thus, ideally the put into the cache should not happen, since the data is longer present into the database.

If the cache entry has a TTL, the entry in the cache might expire. But still, their is a window where the data in the cache is inconsistent with the database.


Does anyone know how to prevent race condition in that scenario?

Thanks in advance.

Best Regards,

Steve




Brian Moon

unread,
Mar 4, 2013, 11:18:57 AM3/4/13
to memc...@googlegroups.com, biao zhang
> I have taken a database class this semester and we are studying about
> maintaining cache consistency between the RDBMS and a cache server such
> as memcached. The consistency issues arise when there are race
> conditions. For example: 1. Suppose I do a get(key) from the cache and
> their is a cache miss. Because I get a cache miss, I fetch the data from
> the database, and then do a put(key,value) into the cache. 2. But, a
> race condition might happen, where some other user might delete the data
> I fetched from the database. This delete might happen before I do a put
> into the cache.
>
> Thus, ideally the put into the cache should not happen, since the data
> is longer present into the database.
>
> If the cache entry has a TTL, the entry in the cache might expire. But
> still, their is a window where the data in the cache is inconsistent
> with the database.
>
>
> Does anyone know how to prevent race condition in that scenario?

The code that deletes the data from the database should issue a delete
on the memcache data.

Sounds like maybe you are doing query based caching which is caching 101
and only holds up for a very short time or can last when you understand
the caveats and are willing to live with them.

Brian.

abinesh td

unread,
Mar 14, 2013, 2:56:43 AM3/14/13
to memc...@googlegroups.com

biao zhang

unread,
Apr 1, 2013, 5:10:00 AM4/1/13
to memc...@googlegroups.com
Thanks, Abinesh
 
I've read the paper you recommended, it's very helpful.
 

Best Regards,

Steve

charles merto

unread,
Mar 3, 2014, 9:56:10 AM3/3/14
to memc...@googlegroups.com
Suppose that we have a message passing system using mailboxes. When sending to a full mail box or trying to receive from an empty one, a process does not block, instead, it gets an error code back. The process responds to the error code by just trying again, over and over, until it succeeds. Does this lead to RACE CONDITION? 

can someone help me pls :( 

Ryan McElroy

unread,
Mar 3, 2014, 4:58:11 PM3/3/14
to memc...@googlegroups.com
This email does not appear to be related to memcached. If you intended to send this here, please clarify what you trying to accomplish an what questions you have.

Cheers,

~Ryan


On Mon, Mar 3, 2014 at 6:56 AM, charles merto <charle...@gmail.com> wrote:
Suppose that we have a message passing system using mailboxes. When sending to a full mail box or trying to receive from an empty one, a process does not block, instead, it gets an error code back. The process responds to the error code by just trying again, over and over, until it succeeds. Does this lead to RACE CONDITION? 

can someone help me pls :( 

--
 
---
You received this message because you are subscribed to the Google Groups "memcached" group.
To unsubscribe from this group and stop receiving emails from it, send an email to memcached+...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
Forward
0 new messages