Monday, December 21, 2009

Cache Race conditions, any solution available ?

Here is the problem
"Say you cache the latest five comments for display on a sidebar in
your application. You decide that the data only needs to be refreshed
once per minute. However, you neglect to remember that this sidebar
display is renderred 50 times per second! Thus, once 60 seconds rolls
around and the cache expires, suddenly 10+ processes are running the
same SQL query to repopulate that cache. Every time the cache expires,
a sudden burst of SQL traffic will result. " (source
http://code.google.com/p/memcached/wiki/FAQ)

Any solution already made ? I was a bit disappointed cakephp didn't
offered a solution "by default".. Or I was unable to find it's already
there.

Here's one proposed by the same FAQ

"First, set the cache item expire time way out in the future. Then,
you embed the "real" timeout serialized with the value. For example
you would set the item to timeout in 24 hours, but the embedded
timeout might be five minutes in the future.

Then, when you get from the cache and examine the timeout and find it
expired, immediately edit the embedded timeout to a time in the future
and re-store the data as is. Finally, fetch from the DB and update the
cache with the latest value. This does not eliminate, but drastically
reduces the amount of time where a stampede can occur. "

If I would like to implement that in the existing Cache::write, what
would be the right thing to do ?

Thank you.

Check out the new CakePHP Questions site http://cakeqs.org and help others with their CakePHP related questions.

You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php?hl=en

No comments: