Scaling Rails Sites by default

2,573 views
2,481 views

Published on

0 Comments
6 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
2,573
On SlideShare
0
From Embeds
0
Number of Embeds
721
Actions
Shares
0
Downloads
42
Comments
0
Likes
6
Embeds 0
No embeds

No notes for slide

Scaling Rails Sites by default

  1. 1. Scaling Rails Site by default
  2. 2. General Scaling Scaling Rails Site : Reading Material #1- #5 http://blog.xdite.net/?cat=91
  3. 3. •Client-side Performance • Database Performance • Rails Performance
  4. 4. Client-side Performance
  5. 5. 150ms > 5ms
  6. 6. YSlow!
  7. 7. Cookie Free Domain
  8. 8. main site http://www.example.org static site http://asset.example.org
  9. 9. main site http://example.org static site http://example-static.org
  10. 10. config.action_controller.asset_host = “asset.example.org”
  11. 11. CDN
  12. 12. image_tag http://asset.example.org/photos/small.jpg?1269316198
  13. 13. Parallel Download
  14. 14. config.action_controller.asset_host = “asset%d.example.org”
  15. 15. Minimal HTTP Request
  16. 16. <%= 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
  17. 17. Cache-Control
  18. 18. def index do_somthing expires_in 10.minutes end header[“Cache-Control”] = “max-age=600”
  19. 19. ETags
  20. 20. 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
  21. 21. Last Modified
  22. 22. [‘Last-Modfield’] [‘If-Modified-Since’] 304
  23. 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 end end 304 Not Modified
  24. 24. Database Performance
  25. 25. ADD INDEX EXPLAIN every query, avoid table scan
  26. 26. SELECT ONLY NEED use “scrooge” plugin replace SELECT *
  27. 27. Avoid N+1 Queries use :include => [ “comment”]
  28. 28. Use Counter Cache size, count , length
  29. 29. Use CONSTANT CONSTANT will cache in memory
  30. 30. Use Transaction BEGIN COMMIT is expensive
  31. 31. Ruby / Rails Performance
  32. 32. Writing Efficiently Ruby Code http://ihower.tw/blog/archives/1691
  33. 33. Avoiding creating unnecessary object
  34. 34. Avoiding writing stupid code
  35. 35. str + other_str => new_str str = “a” + “b” + “c” ==> str = “#{a}#{b}#{c}”
  36. 36. Array#join tag_list = [“a”, “b”, “c”] # rendering tags tags = “” tag_list.each do |t| tags +=”t” tags += “,” end ===> tags = tag_list.join(“,”)
  37. 37. 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|
  38. 38. Date.parse(“1992-02-13”) very expensive, should use regexp
  39. 39. Knowing Rails API
  40. 40. render :partial is slow Use Fragment Caching
  41. 41. Rails action is expensive Use Rails Metal
  42. 42. Ruby API is slow use C extension
  43. 43. Conculsion
  44. 44. • Cache Everything • Knowing API • Drop in other language / system command • Avoid hit DB • Avoid hit Application
  45. 45. Thanks for listening

×