Scaling Rails Sites by default

46
Scaling Rails Site by default 2010年3月23日星期二

description

 

Transcript of Scaling Rails Sites by default

Page 1: Scaling Rails Sites by default

Scaling Rails Site by default

2010年3月23日星期二

Page 2: Scaling Rails Sites by default

Scaling Rails Site : Reading Material #1- #5

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

General Scaling

2010年3月23日星期二

Page 3: Scaling Rails Sites by default

•Client-side Performance

•Database Performance

•Rails Performance

2010年3月23日星期二

Page 4: Scaling Rails Sites by default

Client-side Performance

2010年3月23日星期二

Page 5: Scaling Rails Sites by default

150ms > 5ms

2010年3月23日星期二

Page 6: Scaling Rails Sites by default

YSlow!

2010年3月23日星期二

Page 7: Scaling Rails Sites by default

Cookie Free Domain

2010年3月23日星期二

Page 8: Scaling Rails Sites by default

main site http://www.example.org

static site http://asset.example.org

2010年3月23日星期二

Page 9: Scaling Rails Sites by default

main site http://example.org

static site http://example-static.org

2010年3月23日星期二

Page 10: Scaling Rails Sites by default

config.action_controller.asset_host =

“asset.example.org”

2010年3月23日星期二

Page 11: Scaling Rails Sites by default

CDN

2010年3月23日星期二

Page 12: Scaling Rails Sites by default

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

2010年3月23日星期二

Page 13: Scaling Rails Sites by default

Parallel Download

2010年3月23日星期二

Page 14: Scaling Rails Sites by default

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

2010年3月23日星期二

Page 15: Scaling Rails Sites by default

Minimal HTTP Request

2010年3月23日星期二

Page 16: Scaling Rails Sites by default

<%= 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日星期二

Page 17: Scaling Rails Sites by default

Cache-Control

2010年3月23日星期二

Page 18: Scaling Rails Sites by default

def index

do_somthing expires_in 10.minutes

end

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

2010年3月23日星期二

Page 19: Scaling Rails Sites by default

ETags

2010年3月23日星期二

Page 20: Scaling Rails Sites by default

2010年3月23日星期二

Page 21: Scaling Rails Sites by default

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日星期二

Page 22: Scaling Rails Sites by default

Last Modified

2010年3月23日星期二

Page 23: Scaling Rails Sites by default

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

2010年3月23日星期二

Page 24: Scaling Rails Sites by default

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日星期二

Page 25: Scaling Rails Sites by default

Database Performance

2010年3月23日星期二

Page 26: Scaling Rails Sites by default

ADD INDEXEXPLAIN every query, avoid table scan

2010年3月23日星期二

Page 27: Scaling Rails Sites by default

SELECT ONLY NEEDuse “scrooge” plugin replace SELECT *

2010年3月23日星期二

Page 28: Scaling Rails Sites by default

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

2010年3月23日星期二

Page 29: Scaling Rails Sites by default

Use Counter Cachesize, count , length

2010年3月23日星期二

Page 30: Scaling Rails Sites by default

Use CONSTANTCONSTANT will cache in memory

2010年3月23日星期二

Page 31: Scaling Rails Sites by default

Use TransactionBEGIN COMMIT is expensive

2010年3月23日星期二

Page 32: Scaling Rails Sites by default

Ruby / Rails Performance

2010年3月23日星期二

Page 33: Scaling Rails Sites by default

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

2010年3月23日星期二

Page 34: Scaling Rails Sites by default

Avoiding creating unnecessary object

2010年3月23日星期二

Page 35: Scaling Rails Sites by default

Avoiding writing stupid code

2010年3月23日星期二

Page 36: Scaling Rails Sites by default

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

==>

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

str + other_str => new_str

2010年3月23日星期二

Page 37: Scaling Rails Sites by default

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日星期二

Page 38: Scaling Rails Sites by default

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日星期二

Page 39: Scaling Rails Sites by default

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

2010年3月23日星期二

Page 40: Scaling Rails Sites by default

Knowing Rails API

2010年3月23日星期二

Page 41: Scaling Rails Sites by default

render :partial is slowUse Fragment Caching

2010年3月23日星期二

Page 42: Scaling Rails Sites by default

Rails action is expensiveUse Rails Metal

2010年3月23日星期二

Page 43: Scaling Rails Sites by default

Ruby API is slowuse C extension

2010年3月23日星期二

Page 44: Scaling Rails Sites by default

Conculsion

2010年3月23日星期二

Page 45: Scaling Rails Sites by default

• Cache Everything

• Knowing API

• Drop in other language / system command

• Avoid hit DB

• Avoid hit Application

2010年3月23日星期二

Page 46: Scaling Rails Sites by default

Thanks for listening

2010年3月23日星期二