Caching your rails application

3,426 views

Published on

Bernard from Openminds talks about caching your Rails appication.

Published in: Technology
0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
3,426
On SlideShare
0
From Embeds
0
Number of Embeds
15
Actions
Shares
0
Downloads
68
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Caching your rails application

  1. 1. Rails Need short bursts of speed?
  2. 2. I’m a thief • http://railslab.newrelic.com/
  3. 3. Perception
  4. 4. Communication: Two parts • Server speed • Browser speed
  5. 5. Browser speed • Firebug / Safari developer • YSlow • Different parts of a site
  6. 6. Webpage parts • Page itself (<html> ... ) • Additional files (CSS, JavaScript) • Images • ... generating dynamic content
  7. 7. Siteparts • 10 requests:10 times slower as one request • stylesheet_tag :defaults, ‘screen’, :cache => ‘all’
  8. 8. images • asset hosts • Content Delivery Network
  9. 9. Page Caching
  10. 10. Why? • Code parsing: 20 to 50 req/second • Webserver: easily > 100 req/sec • Webserver stresses less, less database queries • Why not, it is easy (with rails)...
  11. 11. implementation
  12. 12. result
  13. 13. expire_page
  14. 14. expires-result
  15. 15. conclusion: page cache • very simple • limited scope to apply • cache invalidation! Important!
  16. 16. Cache Expiry
  17. 17. Why not expire_page in the action? • Not DRY (update, create, destroy,...) • after_filter possible, but other controllers might need to expire the cache
  18. 18. after_filter • PostsController (clear_posts_cache) • CommentsController (clear_posts_cache)
  19. 19. sweeper • observer on controller and model • if saved, call clear_posts_cache • if deleted, call clear_posts_cache
  20. 20. how
  21. 21. Page caching - part 2 • Ajax Callbacks for dynamic data
  22. 22. login/logout • pain!
  23. 23. Action Caching
  24. 24. Action cache • Cached output stored • Filters are processed before the cached version is returned to the browser • Example: authenticatie
  25. 25. caches_action
  26. 26. action cache • Filter is executed before sending the reply • Output is the same for everyone!
  27. 27. Welcome <username> • content is cached • layout is rendered
  28. 28. Warning! Attention! • fetch needed data with a before_filter • action is not executed!
  29. 29. Conditional AC • caching only on certain conditions
  30. 30. Action Caching • when? • execute code on each hit (authentication) • result remains the same • very easy to implement
  31. 31. Fragment Caching
  32. 32. Fragment caching • Cache part of a page • Logical blocks are candidates
  33. 33. Example
  34. 34. Fragment Caching
  35. 35. No useless actions
  36. 36. Fragment Cache expiry • expire_fragment
  37. 37. PC/AC Storage
  38. 38. Page cache storage • on disk
  39. 39. Storage: action / fragment cache • memory (default) or syncronised memory • file on disk • drb • memcache (normal or compressed) • custom_store (not hard to impelement!)
  40. 40. DYI
  41. 41. DYI caching • Caching hard parts in the code • Only spend cycles when really needed
  42. 42. Client-side caching • The cloud is there, use it
  43. 43. Three tags • max-age • etag • last_modified
  44. 44. max-age
  45. 45. etag • rails has this embedded • MD5(body) => etag • 304 Not Modified
  46. 46. etag • Beware, the complete parsing is done! • Less bandwidth / traffic • Client load time faster
  47. 47. etag
  48. 48. last_modified
  49. 49. Scary technology? • memcached • reverse proxy’s
  50. 50. Memcached • Daemon • Is a big hash in memory • Rails has default routines for this • Hoster / setup should support this
  51. 51. Reverse Proxy’s • a proxy, but on the server side • controle over expiry: max_age + etag • expiry remains a pain
  52. 52. Rack::Cache • gem install rack-cache • rails 2.3 needed!
  53. 53. Need for speed? • Database (n+1, joins / includes) • Disk IO • Implementing caching • Browser caching • External components (CDN, memcached, ...)

×