Caching your rails application

  • 2,902 views
Uploaded on

Bernard from Openminds talks about caching your Rails appication.

Bernard from Openminds talks about caching your Rails appication.

More in: Technology
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Be the first to comment
No Downloads

Views

Total Views
2,902
On Slideshare
0
From Embeds
0
Number of Embeds
0

Actions

Shares
Downloads
65
Comments
0
Likes
7

Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
    No notes for slide

Transcript

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