Rails高速化戦略 / Speeding Up Rails

Rails高速化戦略 / Speeding Up Rails

Roppongi.rb#1( http://roppongirb.connpass.com/event/33502/ ) での発表スライドです。

- 本発表内容をまとめたブログ記事はこちら: http://blog.toshimaru.net/roppongirb-speeding-up-rails/

5919537a0ecfa5d4dea704cf878ae90e?s=128

toshimaru

July 14, 2016
Tweet

Transcript

  1. Rails ߴ଎Խઓུ 2016.7.14 Roppongi.rb #1 Toshimaru Roppongi.rb #1 1

  2. self.introduction • name: Toshimaru • Github: tohimaru • Twitter: @toshimaru_e

    • ! Roppongi.rb Organizer Roppongi.rb #1 2
  3. Rails (Ruby) ஗͍ΑͶ Roppongi.rb #1 3

  4. Go ࢖͓͏ Roppongi.rb #1 4

  5. ׬ Roppongi.rb #1 5

  6. Ͱ΋Rails ࢖͍͍ͨΑͶ Roppongi.rb #1 6

  7. begin Roppongi.rb #1 7

  8. جຊฤ Roppongi.rb #1 8

  9. Ruby Version Up Roppongi.rb #1 9

  10. Ruby Version History • 2013.2: Ruby 2.0 • 2013.12: Ruby

    2.1 • 2014.12: Ruby 2.2 • 2015.12: Ruby 2.3 • 20xx: Ruby 3.0 Roppongi.rb #1 10
  11. Roppongi.rb #1 11

  12. Ruby 3 x 3 Roppongi.rb #1 12

  13. ͱ͋ΔRailsΞϓϦͷ৔߹ Roppongi.rb #1 13

  14. Roppongi.rb #1 14

  15. Ruby 2.0 => 2.1 Ruby Version Up ͚ͩͰ Ϩεϙϯε଎౓͕໿2ഒ޲্ Roppongi.rb

    #1 15
  16. Ruby Version Up ΞϓϦέʔγϣϯίʔυมߋθϩͰ΋1 Rubyόʔδϣϯ্͛Δ͜ͱͰ଎౓վળ 1 ͨͩ͠RubyόʔδϣϯࠩҟʹΑΔඇޓ׵ੑΛղফ͢ΔͨΊͷมߋ͸ඞཁ Roppongi.rb #1 16

  17. What about Rails? Roppongi.rb #1 17

  18. Roppongi.rb #1 18

  19. Roppongi.rb #1 19

  20. Roppongi.rb #1 20

  21. ࣃΛ৯͍͠͹ͬͯ Rails/RubyΛ όʔδϣϯΞοϓ! Roppongi.rb #1 21

  22. What about Rails? Rails ஗͘ͳͬͯ΋ Ruby ͸଎͘ͳ͍ͬͯΔ ͜ΕͰτϯτϯͩΖ, Φʔέʔ❓ Roppongi.rb

    #1 22
  23. ϘτϧωοΫΛ௵͢ Roppongi.rb #1 23

  24. ਪଌ͢Δͳ ܭଌͤΑɹ Roppongi.rb #1 24

  25. Find bottleneck • NewRelic • rack-mini-profiler • rack-lineprof Roppongi.rb #1

    25
  26. ActiveRecord Optimization ! ໰୊ൃݟܕ • bullet: Kill N+1 issue! •

    activerecord-cause: Logs where ActiveRecord actually loads record Roppongi.rb #1 26
  27. ActiveRecord Optimization ! DBεΩʔϚ࠷దԽܕ • flag_shih_tzu: Bit fields for ActiveRecord

    • counter-cache: cacheing count query result • counter_culture: Better counter-cache Roppongi.rb #1 27
  28. ActiveRecord Optimization ⚡ ΫΤϦޮ཰Խܕ • activerecord-precount: Yet another counter_cache alternative.

    • activerecord-import: bulk inserting data Roppongi.rb #1 28
  29. ΫΤϦΛҙࣝͯ͠ ActiveRecord ࢖͍͜ͳͦ͏ by ࿝֐ Roppongi.rb #1 29

  30. Less rendering partials Roppongi.rb #1 30

  31. N+1 partial rendering <%# views/items/index %> <% @items.each do |item|

    %> <%= render item %> <% end %> <%# views/items/_item %> <tr> <td><%= item.name %></td> <td><%= link_to 'Show', item %></td> <td><%= link_to 'Edit', edit_item_path(item) %></td> <td><%= link_to 'Destroy', item, method: :delete %></td> </tr> Roppongi.rb #1 31
  32. Log with N+1 rendering Processing by ItemsController#index as HTML Rendering

    items/index.html.erb within layouts/application Item Load (0.3ms) SELECT "items".* FROM "items" Rendered items/_item.html.erb (0.5ms) Rendered items/_item.html.erb (0.3ms) ...snip... Rendered items/_item.html.erb (0.5ms) Rendered items/_item.html.erb (0.3ms) Rendered items/index.html.erb within layouts/application (57.7ms) Completed 200 OK in 80ms (Views: 77.1ms | ActiveRecord: 0.3ms) Roppongi.rb #1 32
  33. Collection rendering <%# views/items/index %> <%= render @items %> <%#

    views/items/_item %> <tr> <td><%= item.name %></td> <td><%= link_to 'Show', item %></td> <td><%= link_to 'Edit', edit_item_path(item) %></td> <td><%= link_to 'Destroy', item, method: :delete %></td> </tr> Roppongi.rb #1 33
  34. Log without N+1 rendering Processing by ItemsController#index as HTML Rendering

    items/index.html.erb within layouts/application Item Load (0.4ms) SELECT "items".* FROM "items" Rendered collection of items/_item.html.erb [29 times] (6.9ms) Rendered items/index.html.erb within layouts/application (10.3ms) Completed 200 OK in 29ms (Views: 26.4ms | ActiveRecord: 0.4ms) Roppongi.rb #1 34
  35. [جຊฤ] ·ͱΊ • Ruby Version Up • ϘτϧωοΫ೺Ѳ => ௵͢

    • gemΛ༗ޮ׆༻ͯ͠ActiveRecord/ΫΤϦ࠷ద Խ • ύʔγϟϧϨϯμϦϯάΛݮΒ͢ Roppongi.rb #1 35
  36. தڃฤ Roppongi.rb #1 36

  37. Rails caching Roppongi.rb #1 37

  38. 3 types of cacheing2 1. Fragment Cache: View fragment caching.

    2. Action Cache: Controller's action caching (removed in Rails4). 3. Page Cache: Static page caching (removed in Rails4). 2 Caching with Rails: An overview — Ruby on Rails Guides Roppongi.rb #1 38
  39. 1. Fragment Cache Roppongi.rb #1 39

  40. 1. Fragment Cache • άϩʔόϧφϏɾαΠυόʔͳͲͷଟ͘ݺ͹Ε Δڞ௨ίϯςϯπʹ༗ޮ • ॏ͍ॲཧ͕૸ΔϏϡʔͷҰ෦෼Ͱ͋Ε͹͋Δ΄ Ͳߴ଎Խ͕ظ଴Ͱ͖Δ •

    Advanced Usage: Russian Doll Caching Roppongi.rb #1 40
  41. 2. Action Cache Roppongi.rb #1 41

  42. 2. Action Cache • Rails4Ͱ࡟আ͞ΕͯgemԽ: actionpack- action_caching • ViewͷखલͷControllerͷActionࣗମͷॲཧ͕ ॏ͍৔߹ʹ༗ޮ

    • cache_path ͰΩϟογϡΩʔΛΧελϚΠζ Մೳ Roppongi.rb #1 42
  43. 3. Page Cache • Rails4Ͱ࡟আ͞ΕͯgemԽ: actionpack- page_caching • Ωϟογϡର৅ͱͳΔControllerͷActionͷੜ ੒͢ΔHTMLΛ·Δͬͱ੩తϑΝΠϧʹు͖ग़

    ͢ • ͦͷ੩తϑΝΠϧΛNGINXͳͲͷWeb Server/ Reverse ProxyͰϋϯυϦϯά Roppongi.rb #1 43
  44. [தڃฤ] ·ͱΊ • FragmentCache Λ࢖ͬͯϏϡʔͷϨϯμϦϯ άΛߴ଎Խ • Controller ࣗମͷॲཧ͕ॏ͍ͳΒ ActionCache/PageCacheΛݕ౼

    Roppongi.rb #1 44
  45. ⚠ ஫ҙ • Ωϟογϡͯ͠΋ࠜͬ͜ͷ໰୊͸ফ͑ͳ͍ • ΩϟογϡͷϥΠϑαΠΫϧ؅ཧ • ༻๏༻ྔΛकͬͯਖ਼͓͘͠࢖͍͍ͩ͘͞ Roppongi.rb #1

    45
  46. ্ڃฤ Roppongi.rb #1 46

  47. ੩తϑΝΠϧ഑৴ Roppongi.rb #1 47

  48. NGINX Roppongi.rb #1 48

  49. CDN Roppongi.rb #1 49

  50. ͋Ε Railsͷ࿩͡Όͳ͘Ͷʁ Roppongi.rb #1 50

  51. Rails Web ߴ଎Խઓུ Roppongi.rb #1 51

  52. Roppongi.rb #1 52

  53. Roppongi.rb #1 53

  54. ϨΠςϯγʹෛ͚ͳ͍ϓϩτίϧ HTTP/2 Roppongi.rb #1 54

  55. Before HTTP/2 Roppongi.rb #1 55

  56. After HTTP/2 Roppongi.rb #1 56

  57. HTTP/2 • ϦΫΤετଟॏԽ • HTTP vs HTTPS — Test them

    both yourself Roppongi.rb #1 57
  58. Question ͋ͳͨ͸౒ྗͯ͠Ծʹαʔ όʔϨεϙϯελΠϜΛ1ms ʹͨ͠ͱ͠·͠ΐ͏ɻ े෼ʹ଎͘ͳͬͨͱݴ͑·͢ ͔ʁ Roppongi.rb #1 58

  59. Answer ! No Roppongi.rb #1 59

  60. Ϣʔβʔͷମײ଎౓ ! Server-side response ➕ Client-side Speed Roppongi.rb #1 60

  61. Rails5 on Heroku Ͱ HelloWorld App ͷ৔߹4 4 PageSpeed Insights

    Λ࢖ͬͯܭଌ Roppongi.rb #1 61
  62. 80఺ҎԼ... ! Roppongi.rb #1 62

  63. ⚡ AMP • Webߴ଎ԽͷϕετϓϥΫςΟεΛ٧ΊࠐΜͩ ࢓༷/੍ݶ • Why AMP is fast

    — Medium • Google AMP Cache: GoogleͰΩϟογϡͯ͠ ίϯςϯπ഑৴Λݞ୅ΘΓ • => Google AMP CDN Roppongi.rb #1 63
  64. [্ڃฤ] ·ͱΊ • CDN Ͱίϯςϯπ഑৴Λ࠷దԽ • HTTP/2 ͰϨΠςϯγʹෛ͚ͳ͍ • AMPʹΑΔWebϖʔδͷߴ଎Խ

    Roppongi.rb #1 64
  65. end Roppongi.rb #1 65

  66. ࢀߟࢿྉ • High Performance Rails (long edition) // Speaker Deck

    • RailsύϑΥʔϚϯεجຊͷΩ // Speaker Deck • 3x Rails // Speaker Deck • ΢ΣϒΛ଎͘͢ΔͨΊʹDeNA͕΍͍ͬͯΔ͜ ͱ - HTTP/2ͱɺ͞Βʹͦͷઌ Roppongi.rb #1 66
  67. ࢀߟࢿྉ • Rails Upgrade Casual Talks // Speaker Deck •

    σβΠφʔ΍σΟϨΫλʔ΋஌͓͖͍ͬͯͨɺ ϖʔδදࣔ଎౓ͷߴ଎Խͷجຊ – Rriver • ep22 AMP | mozaic.fm Roppongi.rb #1 67