SlideShare a Scribd company logo
Ruby on Rails
Performance
    Tips

          Barcamp Coimbra
          2008 Pedro Sousa
Your application is
       slow
Caching in RoR
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
Setup up your
environment
   > Rails 2.1




   <= Rails 2.0
Types of
Cache Store
Setup up your
environment

  development.rb
3 Types of Caching

Page Caching

 will always use File Store

Action Caching & Fragment Caching

 Will use the cache store you defined
Page Caching
Transforms your views into pure HTML

Best for pages with common content for all
your users or very static pages
Example
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
Example
More options
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
Examples of Caching
Example No caching
Fragment Caching
Oops, I forgot about
  the controller
the fragment
Cleaning up
individual cache files
Cleaning up
(brute force)
Cleaning up
  with memcached


soon...
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
DB Cache
(stupid) example
Memcached

New in Rails 2.0

Simple to use

It’s like a Hash in memory, has a key/value
way of working
Methods
Rails.cache.read

Rails.cache.write

Rails.cache.fetch

Rails.cache.delete

Rails.cache.exist?

Rails.cache.increment

Rails.cache.decrement
example
example with data
        Model




      Controller
example with data
            View (first time)




View (second time, it’s read from the cache)
More info on caching
 http://www.railsenvy.com/2007/2/28/rails-caching-
                      tutorial
       http://railscasts.com/episodes/115


http://www.thewebfellas.com/blog/2008/6/9/rails-2-1-
         now-with-better-integrated-caching

http://www.akitaonrails.com/2008/5/25/rolling-with-
        rails-2-1-the-first-full-tutorial-part-1
Tips for
Optimization
Write Eficient
         SQL Queries
Bring only what you need
Eager Loading

Sometimes it’s best to bring all data at once

Prevents N+1 problem

Optimized Eager Loading in Rails 2.1
Create indexes on DB
It really makes a difference

Don’t exagerate
Use Transactions
Group Transactions when possible

  Minimizes errors

  Less effort on the Database
Reduce http requests
 Too many javascript and css files makes your
 page slow

 You can package all you files into only one
Solutions
Built-in mechanism in Rails 2.0 but without js
minification

Alternative plugin - Bundle-FU

  http://code.google.com/p/bundle-fu/
Bundling Example
  Before/After
Too many images?
Use CSS Sprites Technique

Less http requests

More hard to manage
References about
           CSS Sprites

http://css-tricks.com/css-sprites-what-they-are-why-
         theyre-cool-and-how-to-use-them/

    http://www.alistapart.com/articles/sprites
Tools
Firebug & Yslow

Track your page performance

Easy to install and run

Download from

  http://developer.yahoo.com/yslow/
Examples
More examples
TextMate Footnotes
Useful plugin for Rails

Download from

  http://github.com/drnic/rails-footnotes/
  tree/master
Other Small Tips
Use LibXML-Ruby for processing XML:

 http://libxml.rubyforge.org/

Log only what you need

Use a CDN (ex: Amazon CloudFront)

Patch the RUBY GC:

 http://rubyforge.org/projects/railsbench/
That’s it!
Have fun!
 Don’t be afraid to
   experiment!

 Tweak until your
    satisfied!

If all fails, invest in
    hardware. :)
The End
Thank you

              Pedro Sousa




     [blog] http://www.reinventar.com
     [work] http://www.thinkorange.pt
[linkedin] http://www.linkedin.com/psousa

More Related Content

Performance on Rails