Your SlideShare is downloading. ×
  • Like
Performance on Rails
Upcoming SlideShare
Loading in...5

Thanks for flagging this SlideShare!

Oops! An error has occurred.


Now you can save presentations on your phone or tablet

Available for both IPhone and Android

Text the download link to your phone

Standard text messaging rates apply

Performance on Rails


Presentation about the several caching mecanisms in Rails, optimization, performance which I gave on Barcamp PT 08.

Presentation about the several caching mecanisms in Rails, optimization, performance which I gave on Barcamp PT 08.

Published 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


Total Views
On SlideShare
From Embeds
Number of Embeds



Embeds 0

No embeds

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

    No notes for slide


  • 1. Ruby on Rails Performance Tips Barcamp Coimbra 2008 Pedro Sousa
  • 2. Your application is slow
  • 3. Caching in RoR
  • 4. Before you begin You need to define where to place your cache (db, memory, file) You need to tweak you development environment to test caching
  • 5. Setup up your environment > Rails 2.1 <= Rails 2.0
  • 6. Types of Cache Store
  • 7. Setup up your environment development.rb
  • 8. 3 Types of Caching Page Caching will always use File Store Action Caching & Fragment Caching Will use the cache store you defined
  • 9. Page Caching Transforms your views into pure HTML Best for pages with common content for all your users or very static pages
  • 10. Example
  • 11. Action Caching Works the same as way as page caching ... but goes through dispatcher to run filters and callbacks For example you can use it with pages that require authentication
  • 12. Example
  • 13. More options
  • 14. Fragment Caching More flexible Great for dynamic content directed at specific users or conditions (ex: your friends latest blog posts) Transforms portions of your views into rendered HTML
  • 15. Examples of Caching
  • 16. Example No caching
  • 17. Fragment Caching
  • 18. Oops, I forgot about the controller
  • 19. the fragment
  • 20. Cleaning up individual cache files
  • 21. Cleaning up (brute force)
  • 22. Cleaning up with memcached soon...
  • 23. DB Model Cache It’s built into Rails, you don’t have to do nothing good for not making the same query twice limited... only works on the same request
  • 24. DB Cache (stupid) example
  • 25. Memcached New in Rails 2.0 Simple to use It’s like a Hash in memory, has a key/value way of working
  • 26. Methods Rails.cache.write Rails.cache.fetch Rails.cache.delete Rails.cache.exist? Rails.cache.increment Rails.cache.decrement
  • 27. example
  • 28. example with data Model Controller
  • 29. example with data View (first time) View (second time, it’s read from the cache)
  • 30. More info on caching tutorial now-with-better-integrated-caching rails-2-1-the-first-full-tutorial-part-1
  • 31. Tips for Optimization
  • 32. Write Eficient SQL Queries Bring only what you need
  • 33. Eager Loading Sometimes it’s best to bring all data at once Prevents N+1 problem Optimized Eager Loading in Rails 2.1
  • 34. Create indexes on DB It really makes a difference Don’t exagerate
  • 35. Use Transactions Group Transactions when possible Minimizes errors Less effort on the Database
  • 36. Reduce http requests Too many javascript and css files makes your page slow You can package all you files into only one
  • 37. Solutions Built-in mechanism in Rails 2.0 but without js minification Alternative plugin - Bundle-FU
  • 38. Bundling Example Before/After
  • 39. Too many images? Use CSS Sprites Technique Less http requests More hard to manage
  • 40. References about CSS Sprites theyre-cool-and-how-to-use-them/
  • 41. Tools
  • 42. Firebug & Yslow Track your page performance Easy to install and run Download from
  • 43. Examples
  • 44. More examples
  • 45. TextMate Footnotes Useful plugin for Rails Download from tree/master
  • 46. Other Small Tips Use LibXML-Ruby for processing XML: Log only what you need Use a CDN (ex: Amazon CloudFront) Patch the RUBY GC:
  • 47. That’s it! Have fun! Don’t be afraid to experiment! Tweak until your satisfied! If all fails, invest in hardware. :)
  • 48. The End
  • 49. Thank you Pedro Sousa [blog] [work] [linkedin]