Rails performance:                Controlled expiration on                Fragment CachingCopyright Dimelo SA             ...
Rails performance:                Controlled expiration on                Fragment Caching                              Re...
Rails performance:                Controlled expiration on                Fragment CachingCopyright Dimelo SA             ...
Given a slow partial on a websiteCopyright Dimelo SA                                 www.dimelo.com
Load is skyrocketing when traffic increaseCopyright Dimelo SA                              www.dimelo.com
A known solution: fragment caching         Easy to implement:     <% cache("mycachekey") do %>       All the topics in the...
Sweeping is hard         « There are only two hard things in         Computer Science: cache invalidation         and nami...
Sweeping is hard     • Memcache is the prefered way to do       fragment caching     • Memcache implement key expirations ...
But ... here is the load profile when traffic                keep on increasingCopyright Dimelo SA                         ...
Fragment cache is no silver bullet     • It synchronizes cached part accross       process     • The slower the calculatio...
Solution: backgrounded sweeping     •   hard     •   messy     •   buggy     •   cron/observer debugging is painful     • ...
My solution: Atomic fragment caching     • Don’t use memcached built-in       expiration     • Use software expiration    ...
Upcoming SlideShare
Loading in …5
×

Rails performance: Controlled expiration on Fragment Caching

2,093 views

Published on

Published in: Technology
  • Be the first to comment

Rails performance: Controlled expiration on Fragment Caching

  1. 1. Rails performance: Controlled expiration on Fragment CachingCopyright Dimelo SA www.dimelo.com
  2. 2. Rails performance: Controlled expiration on Fragment Caching Renaud MORVANCopyright Dimelo SA www.dimelo.com
  3. 3. Rails performance: Controlled expiration on Fragment CachingCopyright Dimelo SA www.dimelo.com
  4. 4. Given a slow partial on a websiteCopyright Dimelo SA www.dimelo.com
  5. 5. Load is skyrocketing when traffic increaseCopyright Dimelo SA www.dimelo.com
  6. 6. A known solution: fragment caching Easy to implement: <% cache("mycachekey") do %> All the topics in the system: <%= render :partial => "topic", :collection => Topic.find(:all) %> <% end %> But you have to sweep it !!!Copyright Dimelo SA www.dimelo.com
  7. 7. Sweeping is hard « There are only two hard things in Computer Science: cache invalidation and naming things. »Copyright Dimelo SA www.dimelo.com
  8. 8. Sweeping is hard • Memcache is the prefered way to do fragment caching • Memcache implement key expirations <% cache("mycachekey", :expires_in => 3.minutes) do %> <% end %> • Great solution for medium term, no sweeper to manageCopyright Dimelo SA www.dimelo.com
  9. 9. But ... here is the load profile when traffic keep on increasingCopyright Dimelo SA www.dimelo.com
  10. 10. Fragment cache is no silver bullet • It synchronizes cached part accross process • The slower the calculation the worse the load • Multiple process try to recalculate key def cache key, options = {}, &block unless Rails.cache.read key, options yield.tap do |result| Rails.cache.write key, result, options end end endCopyright Dimelo SA www.dimelo.com
  11. 11. Solution: backgrounded sweeping • hard • messy • buggy • cron/observer debugging is painful • code is split between multiple filesCopyright Dimelo SA www.dimelo.com
  12. 12. My solution: Atomic fragment caching • Don’t use memcached built-in expiration • Use software expiration • When expiration time is reached, increase expiration and trigger cache recalculation • Short expired period => few concurrent recalculationCopyright Dimelo SA www.dimelo.com

×