Avoiding pileups

0 views
Skip to first unread message

Lee Fyock

unread,
May 5, 2008, 10:21:53 AM5/5/08
to acts_as_cached
Hi--

One problem with caching DB queries is that, when the cache expires,
multiple requesters can cause a pileup on the DB, all trying to refill
the cache at the same time.

I've implemented some locking code for cache_fu that should prevent
pileups. I'd appreciate some feedback.

Overall, here's the idea:
When putting an item into the cache, also insert a separate cache item
that contains the item's "freshness" date, which is 80% of the cache
period. When getting an item from cache, if the item is past its
freshness date, obtain a lock for the item, then fill the cache. If
the item is still fresh, or you can't obtain a lock, just use the
existing cached data. The lock period is a little less than 10% of the
cache period. In theory this lets two requests have a whack at filling
the cache before the item expires.

The svn diff is at <http://pastie.caboo.se/191729>. I'll do a git
thingie if anyone thinks this change is useful.

Lee

Chris Wanstrath

unread,
May 5, 2008, 1:19:01 PM5/5/08
to acts_as...@googlegroups.com
I typically use reset_cache or set_cache in an after_save to prevent
the 'dogpile' effect. That way the cache is never empty, just
atomically overwritten.

- Chris

--
Chris Wanstrath
http://github.com/defunkt

Lee Fyock

unread,
May 5, 2008, 2:43:52 PM5/5/08
to acts_as_cached
Heh, "dogpile". :-)

How does this help collections, where the data may not be requested
often enough to keep the data fresh?

For some collections, it makes sense to have a cron job refresh the
cache every once in a while, so that no user ever takes the time hit,
but some collections are on-demand, yet expensive enough that a pileup
could load the DB. How do you handle these?

Thanks,
Lee

Lee Fyock

unread,
May 19, 2008, 2:04:15 PM5/19/08
to acts_as_cached
I've placed updated code diffs for cache_methods.rb at <http://
pastie.caboo.se/199625>.

If anyone tries it out, let me know how it works for you.

Lee

Reply all
Reply to author
Forward
0 new messages