Getting the most out of your Ruby on Rails applications: from zero to hero

761 views
675 views

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
761
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • \n
  • i hope that everyone can learn something from this talk\nand I hope to learn something from you\n
  • \n
  • \n
  • i want to get a little of the track\n
  • my whole talk is build around this opinion\n
  • well, it’s not a opinion, it’s a fact. or, a myth.\nruby is slow. ruby is slow COMPARED to.\nbut working with Rails is much effective than with any other framework\n
  • \n
  • they are not supposed to scale\nthey are only supposed to carry out the tasks that we learn them to do\n
  • programming language matters (IN GENERAL) if you run a hello world benchmark\n
  • or you run a website at a massive scale\nI mean thousands of server, where spinning down 10000 servers makes a hude difference in your budget\n
  • and basically you almost always come to a conclusion that’s not your application that’s slow.\nand if it is it really simple to fix.\nit’s your architecture that’s failing\n
  • \n
  • and I hope to learn something from you too - feel free to interrupt at any time\n
  • because every app is different\n
  • this talk may help you solve some very obvious things with your application\nthis talk will not solve your architectural issues\n
  • \n
  • \n
  • ASK: what do you want to monitor in your app?\nthere’s one thing that you want to monitor\n
  • you want to monitor everything\nboth your application for bottlenecks and other issue\nand your server to simplify debugging - ie. your code won’t be fast if you disks are slow or you’re running out of memory\n
  • some tools I can recommend\nthese tools are easy and FREE to start with\n
  • monitors application performance\nbreaks down every aspect of your application - controller, database, templates, other\nit can monitor servers too!\n
  • again - free to start and has a very nice graph set if you don’t want to roll out your own solution\nthough it gets expensive really fast so you might want to roll something on your own\n
  • you what’s a great source of information?\nRails log files!\nyou might want to analyze them and create custom reports\n
  • all these tools are free\n
  • nagios, union station, munin, scout, plenty of others\n
  • there’s one thing that you want to monitor\n
  • you want to monitor everything\nboth your application for bottlenecks and other issue\nand your server to simplify debugging - ie. your code won’t be fast if you disks are slow or you’re running out of memory\n
  • produces xdebug compatible output\n
  • gets deeper into Ruby internals, based on Google perftools\n
  • a droping for you rails application\n
  • \n
  • \n
  • by default rails executes lots of queries\n
  • that’s how orms work\nthat’s what keeps us, developers, happy & productive\nthat’s what keeps our DBA loosing precious sleep\n
  • you can use this shortcut when creating new tables\n
  • always use includes when running queries\n
  • always use includes when running queries\n
  • always use includes when running queries\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • replace distance of time in words with javascript counterpart\n
  • \n
  • \n
  • \n
  • \n
  • do you know where?\n
  • \n
  • \n
  • \n
  • saves a lot on date parsing. and date / datetime parsing is incredibly slow in ruby\n
  • \n
  • \n
  • \n
  • this can become a huge number\n
  • \n
  • \n
  • because of thread safety and other considerations\n
  • \n
  • there’s one thing that you want to monitor\n
  • you want to monitor everything\nboth your application for bottlenecks and other issue\nand your server to simplify debugging - ie. your code won’t be fast if you disks are slow or you’re running out of memory\n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • \n
  • so you can do plenty of stuff with Rails\nRails is amazing application development stack\nyou want to learn all its features - it definitely worth\n
  • \n
  • Getting the most out of your Ruby on Rails applications: from zero to hero

    1. 1. Getting the most of yourapplicationsRuby onRails
    2. 2. Getting the most of yourapplicationsWeb
    3. 3. my dad is at about.me/left right
    4. 4. I HAVE A AND I HAVE A DAY JOB NIGHT JOB
    5. 5. BUT FIRST!
    6. 6. A PERSONALOPINION.
    7. 7. Ruby DOESN’T SCALE!!!11
    8. 8. WELL! THAT’SBECAUSE
    9. 9. program ming DON’Tlanguage SCALE s .
    10. 10. unless your websiteisHELLOWORLD
    11. 11. oYOU WORK ATFACEBOOK
    12. 12. < PROGRAMMING LANGUAGEAPPLICATION ARCHITECTURE
    13. 13. SO.
    14. 14. TIPS,TRICKS &GOOD
    15. 15. THIS TALKNOGOLDENBULLETS
    16. 16. ABOUTAPPLICATIONNOTARCHIT
    17. 17. MONITORINGORMCACHINGOTHER
    18. 18. MONITORINGORMCACHINGOTHER
    19. 19. monitor
    20. 20. monitor everyth ing
    21. 21. TOOLS
    22. 22. left right
    23. 23. request-log-analyzergithub.com/wvanbergen/request-log-analyzer
    24. 24. EE
    25. 25. EEso you have no reason not to monitor your application
    26. 26. profile
    27. 27. profile everyth ing
    28. 28. ruby-profgithub.com/rdp/ruby-prof
    29. 29. perftools.rbgithub.com/tmm1/perftools.rb
    30. 30. rack-perftools_profilergithub.com/bhb/rack-perftools_profiler
    31. 31. KNOW YOURAPPLICATION!
    32. 32. MONITORINGORMCACHINGOTHER
    33. 33. queries queries queries queries queries queries queries queries queries queries queries queries queries queries queries queries queries queries queries queriesqueries queries queries queries queries queries queries queries queries
    34. 34. IT IS WHATORMS DO
    35. 35. LET RAILS DO ITS JOBt.references
    36. 36. LET RAILS DO ITS JOBincludes
    37. 37. REAL LIFE EXAMPLE
    38. 38. REAL LIFE EXAMPLE
    39. 39. class Subscription < ActiveRecord::Base  belongs_to :user  belongs_to :followerendclass User < ActiveRecord::Base  has_many :subscriptions_from  has_many :subscriptions_to  has_one :avatar  has_one :background  has_many :update  has_one :current_updateend
    40. 40. FETCH USER’SSUBSCRIPTIONS
    41. 41. USER HAS 2000FOLLOWERS
    42. 42. USER FOLLOWS 30USERS
    43. 43. A QUERY THATFETCHES IT ALLLET’S DO THE MATH@subscriptions = Subsciption.where(  "`user_id` = ? OR `follower_id` = ?", @user.id, @user.id)
    44. 44. 2030 SUBSCRIPTIONS+ 2030 * 2 USERS+ 2030 * 2 AVATARS+ 2030 * 2 BACKGROUNDS+ 2030 * CURRENT UPDATES= 16241 QUERIES
    45. 45. LET’S USEinclude@subscriptions = Subsciption.where(  "`user_id` = ? OR `follower_id` = ?", @user.id, @user.id).include({  :user => [:avatar, :background, :current_status],  :follower => [:avatar, :background, :current_status]})
    46. 46. 2030 SUBSCRIPTIONS+ 2030 * 2 USERS+ 2030 * 2 AVATARS+ 2030 * 2 BACKGROUNDS+ 2030 * CURRENT UPDATES= 5 QUERIES
    47. 47. bulletgithub.com/flyerhzm/bullet
    48. 48. LET RAILS DO ITS JOBpluck
    49. 49. MONITORINGORMCACHINGOTHER
    50. 50. RAILSHAS GREAT CACHING
    51. 51. BUT!
    52. 52. HTTP CACHINGIS ALWAYS BETTERTHAN APPLICATIONCACHING
    53. 53. NO CODE THAT YOUWRITEIS FASTERTHAN NO CODE
    54. 54.              
    55. 55.       
    56. 56. CACHING IN RAILSPAGEACTIONFRAGMENT
    57. 57. RAILS CACHE ISKEY-BASED
    58. 58. RAILS CACHE ISEASY!
    59. 59. IN GENERALCACHE EXPIRATION ISHARD
    60. 60. THERE ARE ONLYTWO HARD THINGSIN COMPUTERSCIENCE: CACHEINVALIDATION ANDNAMING THINGSyou’ve never seen this, right? :-)
    61. 61. WHAT IF YOU DIDN’THAVE TOEXPIRE CACHE?
    62. 62. <%= cache @record do %>  <div>    <%= @record.expensive_to_render %>  </div><% end %>
    63. 63. CACHE KEY:RECORD/1-123456789123
    64. 64. DON’T EXPIRE!
    65. 65. LET KEYS AUTO-EXPIREWITH LRUUSE MEMCACHED,REDIS & OTHERS
    66. 66. WOULD YOU RATHERWASTE SOMEMEMORY?
    67. 67. OR WOULD YOURATHER WASTEHOURS OF CODING?
    68. 68. AND YOU CAN EXPIRERELATED OBJECTSWITHtouch
    69. 69. WILL YOU EVER USEPAGE CACHING? ORACTION CACHING?
    70. 70. YOU MIGHT.Buy I don’t recommend.
    71. 71. USE VARNISHFETCH WITHJAVASCRIPT
    72. 72. USE VARNISHPROCESS WITHJAVASCRIPT
    73. 73. MONITORINGORMCACHINGOTHER
    74. 74. C EXTENSIONS
    75. 75. RUBYIS SLOW
    76. 76. BUT C ISVERY FAST
    77. 77. YOU ARE USING CEXTENSIONSALREADY!
    78. 78. DATABASE DRIVERS
    79. 79. IT’S EASY TO WRITE CEXTENSIONSif you know C, that is!
    80. 80. MOST C EXTENSIONSREQUIRE A LINE INGEMFILE
    81. 81. home_rungithub.com/jeremyevans/home_run
    82. 82. 07/MAY/2012:06:26:56+0200* 1 000 000
    83. 83. RUBY: 53 SECONDS
    84. 84. HOME_RUN: 18SECONDS
    85. 85. THINK SCALE
    86. 86. nokogirigithub.com/tenderlove/nokogiri
    87. 87. hiredisgithub.com/pietern/hiredis-rb
    88. 88. IT’S HARD TO WRITEC EXTENSIONS
    89. 89. BACKGROUNDPROCESSING
    90. 90. background process
    91. 91. background everyth process ing
    92. 92. delayed_jobgithub.com/collectiveidea/delayed_job
    93. 93. resquegithub.com/defunkt/resque
    94. 94. sidekiqgithub.com/mperham/sidekiq
    95. 95. COMMING SOON!
    96. 96. Rails.queueIn Ruby on Rails 4, due...
    97. 97. START SMALL ANDGROW
    98. 98. GO LIGHT
    99. 99. WHOLE STACK?
    100. 100. WHO NEEDS IT?!
    101. 101. FEATURE BLOAT?
    102. 102. the smallest rails appthesmallestrailsapp.com
    103. 103. %w(action_controller/railtie coderay).map &method(:require)run TheSmallestRailsApp ||= Class.new(Rails::Application) {  config.secret_token = routes.append { root to: hello#world }.inspect  initialize!}class HelloController < ActionController::Base  def world    render inline: " <!DOCTYPE html> <title>The Smallest Rails App</title> <h3>I am the smallest rails app!</h3> <p>Here is my source code:</p> #{CodeRay.scan_file(__FILE__, :ruby).div(line_numbers: :table)} <a href=https://github.com/artemave/thesmallestrailsapp.com>Small!</a> "  endend
    104. 104. HOW ABOUTSINATRA?
    105. 105. HOW ABOUT RAILS-API?
    106. 106. QUESTIONS?https://joind.in/6473

    ×