Scaling Rails Site
                by default
General Scaling

Scaling Rails Site :
	

 	

 	

 	

 Reading Material #1- #5

                 http://blog.xdite.net/?cat=91
•Client-side Performance
• Database Performance
• Rails Performance
Client-side Performance
150ms > 5ms
YSlow!
Cookie Free Domain
main site     http://www.example.org
static site   http://asset.example.org
main site        http://example.org
static site   http://example-static.org
config.action_controller.asset_host =
       “asset.example.org”
CDN
image_tag
http://asset.example.org/photos/small.jpg?1269316198
Parallel Download
config.action_controller.asset_host =
         “asset%d.example.org”
Minimal HTTP Request
<%= javascript_include_tag :default, :cache => true %>

<%=stylesheet_link_tag “main”, :cache => true %>




http://asset.example.org/javascripts/all.js?1269316198

http://asset.example.org/stylesheets/all.css?1269316198
Cache-Control
def index

	

 do_somthing
	

 expires_in 10.minutes

end



header[“Cache-Control”] = “max-age=600”
ETags
def show

	

 @user = User.find(params[:id])
	

 if stale?(:etag => @user)
	

 	

 @content = @user.very_expensive_call
	

 	

 respond_to do |format|
	

 	

 	

 formant.html.erb
	

 	

 end
	

 end
end


304 Not Modified
Last Modified
[‘Last-Modfield’]   [‘If-Modified-Since’]   304
def show

	

 @user = User.find(params[:id])
	

 if stale?(:last_modified => @user.updated_at )
	

 	

 @content = @user.very_expensive_call
	

 	

 respond_to do |format|
	

 	

 	

 formant.html.erb
	

 	

 end
	

 end
end


304 Not Modified
Database Performance
ADD INDEX
 EXPLAIN every query, avoid table scan
SELECT ONLY NEED
    use “scrooge” plugin replace SELECT *
Avoid N+1 Queries
       use :include => [ “comment”]
Use Counter Cache
           size, count , length
Use CONSTANT
   CONSTANT will cache in memory
Use Transaction
      BEGIN COMMIT is expensive
Ruby / Rails Performance
Writing Efficiently Ruby Code
          http://ihower.tw/blog/archives/1691
Avoiding creating unnecessary object
Avoiding writing stupid code
str + other_str => new_str




str = “a” + “b” + “c”

==>

str = “#{a}#{b}#{c}”
Array#join
tag_list = [“a”, “b”, “c”]

# rendering tags

tags = “”

tag_list.each do |t|
	

 tags +=”t”
	

 tags += “,”
end

===>

tags = tag_list.join(“,”)
tag_list = [“a”, “b”, “c”]            Array#each_with_index

# rendering tags

tags = “”
counter = 1

tag_list.each do |t|
	

 tags +=”counter”
	

 tags +=”t”
	

 tags += “,”
	

 counter +=1
end

===>
tag_list.each_with_index do |t , i|
Date.parse(“1992-02-13”)
          very expensive, should use regexp
Knowing Rails API
render :partial is slow
             Use Fragment Caching
Rails action is expensive
                   Use Rails Metal
Ruby API is slow
            use C extension
Conculsion
• Cache Everything
• Knowing API
• Drop in other language / system command
• Avoid hit DB
• Avoid hit Application
Thanks for listening

Scaling Rails Sites by default