scaling rails sites by default

Post on 19-Jan-2015

2.204 Views

Category:

Documents

5 Downloads

Preview:

Click to see full reader

DESCRIPTION

 

TRANSCRIPT

Scaling Rails Site by default

2010年3月23日星期二

Scaling Rails Site : Reading Material #1- #5

http://blog.xdite.net/?cat=91

General Scaling

2010年3月23日星期二

•Client-side Performance

•Database Performance

•Rails Performance

2010年3月23日星期二

Client-side Performance

2010年3月23日星期二

150ms > 5ms

2010年3月23日星期二

YSlow!

2010年3月23日星期二

Cookie Free Domain

2010年3月23日星期二

main site http://www.example.org

static site http://asset.example.org

2010年3月23日星期二

main site http://example.org

static site http://example-static.org

2010年3月23日星期二

config.action_controller.asset_host =

“asset.example.org”

2010年3月23日星期二

CDN

2010年3月23日星期二

image_taghttp://asset.example.org/photos/small.jpg?1269316198

2010年3月23日星期二

Parallel Download

2010年3月23日星期二

config.action_controller.asset_host =“asset%d.example.org”

2010年3月23日星期二

Minimal HTTP Request

2010年3月23日星期二

<%= 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

2010年3月23日星期二

Cache-Control

2010年3月23日星期二

def index

do_somthing expires_in 10.minutes

end

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

2010年3月23日星期二

ETags

2010年3月23日星期二

2010年3月23日星期二

def show

@user = User.find(params[:id]) if stale?(:etag => @user) @content = @user.very_expensive_call respond_to do |format| formant.html.erb end endend

304 Not Modified

2010年3月23日星期二

Last Modified

2010年3月23日星期二

[‘Last-Modfield’] [‘If-Modified-Since’] 304

2010年3月23日星期二

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 endend

304 Not Modified

2010年3月23日星期二

Database Performance

2010年3月23日星期二

ADD INDEXEXPLAIN every query, avoid table scan

2010年3月23日星期二

SELECT ONLY NEEDuse “scrooge” plugin replace SELECT *

2010年3月23日星期二

Avoid N+1 Queries use :include => [ “comment”]

2010年3月23日星期二

Use Counter Cachesize, count , length

2010年3月23日星期二

Use CONSTANTCONSTANT will cache in memory

2010年3月23日星期二

Use TransactionBEGIN COMMIT is expensive

2010年3月23日星期二

Ruby / Rails Performance

2010年3月23日星期二

Writing Efficiently Ruby Codehttp://ihower.tw/blog/archives/1691

2010年3月23日星期二

Avoiding creating unnecessary object

2010年3月23日星期二

Avoiding writing stupid code

2010年3月23日星期二

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

==>

str = “#{a}#{b}#{c}”

str + other_str => new_str

2010年3月23日星期二

tag_list = [“a”, “b”, “c”]

# rendering tags

tags = “”

tag_list.each do |t| tags +=”t” tags += “,”end

===>

tags = tag_list.join(“,”)

Array#join

2010年3月23日星期二

tag_list = [“a”, “b”, “c”]

# rendering tags

tags = “”counter = 1

tag_list.each do |t| tags +=”counter” tags +=”t” tags += “,” counter +=1end

===>tag_list.each_with_index do |t , i|

Array#each_with_index

2010年3月23日星期二

Date.parse(“1992-02-13”)very expensive, should use regexp

2010年3月23日星期二

Knowing Rails API

2010年3月23日星期二

render :partial is slowUse Fragment Caching

2010年3月23日星期二

Rails action is expensiveUse Rails Metal

2010年3月23日星期二

Ruby API is slowuse C extension

2010年3月23日星期二

Conculsion

2010年3月23日星期二

• Cache Everything

• Knowing API

• Drop in other language / system command

• Avoid hit DB

• Avoid hit Application

2010年3月23日星期二

Thanks for listening

2010年3月23日星期二

top related