Upgrade to Pro — share decks privately, control downloads, hide ads and more …

High Performance Ruby: Threading versus Evented (Ruby MidWest edition)

High Performance Ruby: Threading versus Evented (Ruby MidWest edition)

I wanted to know, "Do I need to learn about EventMachine or node.js? Can I use threads? What is so good or bad about threading in Ruby 1.8, Ruby 1.9, JRuby and Rubinius 2.0?"
What was important to me was the the choice was abstracted away. I wanted to write normal, step-by-step Ruby code. Yet I wanted it to be performant.
I've asked a lot of people. I even hosted EM RubyConf (http://emrubyconf.com) during RailsConf 2011 in order to gather the brightest minds in the Ruby community.
"What choices do I need to make, how different does my code look, and how do I do testing?" These are the questions I searched for answers.
I'd like to now share the answers.

Cb2b768a5e546b24052ea03334e43676?s=128

Dr Nic Williams

November 05, 2011
Tweet

Transcript

  1. High-Performance Ruby: Evented vs readed @drnic | drnicwilliams.com | engineyard.com

    Dr Nic Williams
  2. “ruby midwest”

  3. “ruby midwest”

  4. midwest

  5. midwest

  6. kansas

  7. kansas city

  8. in Missouri

  9. midwest?!

  10. an actual safari

  11.  ________   <  Safari  >  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐        

             \      ^__^                  \    (xx)\_______                        (__)\              )\/\                          ^    ||-­‐-­‐-­‐-­‐w  |                                ||          ||
  12. drnic cowsay

  13. None
  14. You have $

  15. New project/rapid dev Optimise (features/$) (req latency/$) (req thru-put/$) (bugs/$)

  16. Add resources

  17. Resources = $ $

  18. or even $$$$$ $ $ $ $ $

  19. Optimise New project/rapid dev (features/$) (performance/$0) how?

  20. None
  21. •Add resources •Use resources better Options

  22. you? just write code

  23. evented -> threads -> your code

  24. nginx -> trinidad -> web app

  25. evented? arrange I/O

  26. threads? actual work

  27. you? just write code

  28. evented -> threads -> your code

  29. nginx -> trinidad -> web app

  30. •Add resources •Use resources better Options

  31. •process concurrency •threaded concurrency •evented concurrency Concurrency

  32. Process vs readed

  33. •1 req = 1 process = 150Mb •“100 reqs?” =

    15Gb! •Orchestrated by? Kernel •Unicorn, Passenger Process Concurrency
  34. •1 req = 1 thread = 2kb •“100 reqs?”=150Mb+200kb •Orchestrated

    by? Kernel •Mongrel* readed Concurrency
  35. 1 req = 1 thread = 2kb

  36. Process vs readed

  37. evented -> threads -> your code

  38. None
  39. •.... •.... •.... •Mongrel* readed Concurrency

  40. •.... •.... •.... •Mongrel* readed Concurrency

  41. “Mongrel is threaded?!”

  42. None
  43. Mongrel * Rails * Ruby1.8 --> process conc. 2006 reality:

  44. 20068 reality: “Rails 2.2... rst...thread safe Rails” --Pratik Naik

  45. 20068 reality: config.threadsafe! http://guides.rubyonrails.org/configuring.html

  46. 20068 reality: # do nothing http://www.sinatrarb.com/configuration.html

  47. Mongrel * Rails * Ruby1.8 --> process conc. 20068 reality:

  48. 200810 reality: “Ruby 1.9.2 is released”

  49. 200810 reality: 1.9 has native threads!

  50. 200810 reality: 1 thread of Ruby at a time :(

  51. 200810 reality: GIL = Global Interpreter Lock :(

  52. 200810 reality: GIL:( “Grumpy Gil”

  53. Mongrel * Rails * Ruby1.9 --> process conc. 200810 reality:

  54. “Mongrel is threaded?!” “Rails is threadsafe” but no threads...

  55. None
  56. “reads please!”

  57. rvm install jruby rvm use jruby gem install rails rails

    new myapp
  58. gem 'activerecord- jdbcsqlite3- adapter' gem 'jruby-openssl'

  59. Mongrel * Rails * JRuby --> thread concurrency!

  60. 1 req = 1 thread = 2kb

  61. “How do I run JRuby/ Rails?”

  62. How? -> trinidad -> web app

  63. $ gem install trinidad $ trinidad --> localhost:3000

  64. $ git clone git:// github.com/ engineyard/todo.git $ cd todo $

    bundle $ rake db:migrate $ trinidad
  65. # config.threadsafe! $ trinidad Info: max runtimes = 5

  66. config.threadsafe! # yes! $ trinidad -e production Info: max runtimes

    = 1 (Thread limit 200)
  67. 1 req = 1 thread = 2kb

  68. And its just normal web app code!

  69. evented -> threads -> your code

  70. nginx -> trinidad -> web app

  71. “I’m scared of threads”

  72. Web apps are* thread safe

  73. “Store state elsewhere; Unless its safe” Dr Nic’s Joy of

    Concurrency
  74. None
  75. evented -> threads -> your code

  76. nginx -> trinidad -> web app

  77. “I came to hear good things about EventMachine”

  78. “Evented is cool, threads are not”

  79. “You haven’t said ‘nodejs’ once!?”

  80. What is evented? any I/O? do something with it

  81. cache hits req ? web app cache miss

  82. cache hits req ? web app cache miss

  83. req I/O work

  84. req I/O work evented threads

  85. req I/O work evented threads trinidad nginx

  86. nginx vs apache

  87. “Hosted?” 500 hrs free!

  88. jruby core trinidad core rubinius core Come work with us!

  89. -> your code “Be a happy developer”

  90. -> threads -> your code

  91. evented -> threads -> your code

  92. nginx -> trinidad -> web app @drnic | drnicwilliams.com |

    engineyard.com
  93.  _____________   <  Power  Ruby!  >  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐      

               \      ^__^                  \    (Oo)\_______                        (__)\              )\/\                          <>  ||-­‐-­‐-­‐-­‐w  |                                ||          || @drnic | drnicwilliams.com | engineyard.com