High-Performance Ruby:  Evented vs readed          Dr Nic Williams @drnic | drnicwilliams.com | engineyard.com
“rubymidwest”
“rubymidwest”
midwest
midwest
kansas
kansas city
in Missouri
midwest?!
an actual safari
 ________	  <	  Safari	  >	  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	  	  	  	  	  	  	  	  	  	  	  	  ^__^	  	  	  	  	  	  	  	  	  	 ...
drniccowsay
You have $
New project/rapid dev(features/$)            Optimise               (bugs/$)      (req thru-put/$)        (req latency/$)
Add resources
Resources = $   $
or even $$$$$        $      $      $$$
New project/rapid dev(features/$)    how?     (performance/$0)            Optimise
Options•Add resources•Use resources better
you?just write   code
evented -> threads-> your code
nginx-> trinidad-> web app
evented? arrange   I/O
threads?  actual  work
you?just write   code
evented -> threads-> your code
nginx-> trinidad-> web app
Options•Add resources•Use resources better
Concurrency•process concurrency•threaded concurrency•evented concurrency
Process  vsreaded
Process Concurrency•1 req = 1 process = 150Mb•“100 reqs?” = 15Gb!•Orchestrated by? Kernel•Unicorn, Passenger
readed Concurrency•1 req = 1 thread = 2kb•“100 reqs?”=150Mb+200kb•Orchestrated by? Kernel•Mongrel*
1 req =1 thread =    2kb
Process  vsreaded
evented -> threads-> your code
readed Concurrency•....•....•....•Mongrel*
readed Concurrency•....•....•....•Mongrel*
“Mongrel isthreaded?!”
2006 reality:Mongrel * Rails * Ruby1.8 -->process conc.
20068 reality:“Rails 2.2... rst...threadsafe Rails”        --Pratik Naik
20068 reality:config.threadsafe!    http://guides.rubyonrails.org/configuring.html
20068 reality:# do nothing    http://www.sinatrarb.com/configuration.html
20068 reality:Mongrel * Rails *  Ruby1.8 --> process conc.
200810 reality:“Ruby 1.9.2 is  released”
200810 reality:1.9 has native   threads!
200810 reality:1 thread of Ruby at a  time :(
200810 reality:GIL = GlobalInterpreter  Lock :(
200810 reality:    GIL:(“Grumpy Gil”
200810 reality:Mongrel * Rails * Ruby1.9 -->process conc.
“Mongrel is  threaded?!”    “Rails is  threadsafe”but no threads...
“readsplease!”
rvm install jrubyrvm use jrubygem install railsrails new myapp
gem activerecord-       jdbcsqlite3-          adaptergem jruby-openssl
Mongrel * Rails *   JRuby -->    thread concurrency!
1 req =1 thread =    2kb
“How do Irun JRuby/  Rails?”
How?-> trinidad -> web app
$ gem install trinidad$ trinidad--> localhost:3000
$ git clone git://    github.com/    engineyard/todo.git$ cd todo$ bundle$ rake db:migrate$ trinidad
# config.threadsafe!$ trinidadInfo: max runtimes = 5
config.threadsafe! # yes!$ trinidad -e productionInfo: max runtimes = 1(Thread limit 200)
1 req =1 thread =    2kb
And its justnormal web app code!
evented -> threads-> your code
nginx-> trinidad-> web app
“I’m scaredof threads”
Web apps   are*thread safe
Dr Nic’s Joy of Concurrency“Store state elsewhere;Unless its safe”
evented -> threads-> your code
nginx-> trinidad-> web app
“I came to hear  good things     aboutEventMachine”
“Evented is cool,threads are not”
“You haven’tsaid ‘nodejs’  once!?”
What isevented? any   do somethingI/O?       with it
cache     hitsreq   ?    cache   web    miss    app
cache     hitsreq   ?    cache   web    miss    app
req   I/O   work
req    I/O      work      evented   threads
req    I/O      work      evented   threads       nginx    trinidad
nginxvs apache
“Hosted?”500 hrs free!
Come work   with us!jruby coretrinidad corerubinius core
“Be a happy developer”-> your code
-> threads-> your code
evented -> threads-> your code
nginx-> trinidad-> web app@drnic | drnicwilliams.com | engineyard.com
 _____________	  <	  Power	  Ruby!	  >	  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐	  	  	  	  	  	  	  	  	  	  	  	  ^__^	 ...
High Performance Ruby: Evented vs. Threaded
High Performance Ruby: Evented vs. Threaded
High Performance Ruby: Evented vs. Threaded
High Performance Ruby: Evented vs. Threaded
High Performance Ruby: Evented vs. Threaded
High Performance Ruby: Evented vs. Threaded
Upcoming SlideShare
Loading in …5
×

High Performance Ruby: Evented vs. Threaded

2,940 views
2,837 views

Published on

Dr Nic Williams' Ruby Midwest 2011 presentation

Presentation description:
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 that 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 EventMachine 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 to. I'd like to now share the answers.

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

No Downloads
Views
Total views
2,940
On SlideShare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
15
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide

High Performance Ruby: Evented vs. Threaded

  1. 1. High-Performance Ruby: Evented vs readed Dr Nic Williams @drnic | drnicwilliams.com | engineyard.com
  2. 2. “rubymidwest”
  3. 3. “rubymidwest”
  4. 4. midwest
  5. 5. midwest
  6. 6. kansas
  7. 7. kansas city
  8. 8. in Missouri
  9. 9. midwest?!
  10. 10. an actual safari
  11. 11.  ________  <  Safari  >  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐                        ^__^                      (xx)_______                        (__)              )/                          ^    ||-­‐-­‐-­‐-­‐w  |                                ||          ||
  12. 12. drniccowsay
  13. 13. You have $
  14. 14. New project/rapid dev(features/$) Optimise (bugs/$) (req thru-put/$) (req latency/$)
  15. 15. Add resources
  16. 16. Resources = $ $
  17. 17. or even $$$$$ $ $ $$$
  18. 18. New project/rapid dev(features/$) how? (performance/$0) Optimise
  19. 19. Options•Add resources•Use resources better
  20. 20. you?just write code
  21. 21. evented -> threads-> your code
  22. 22. nginx-> trinidad-> web app
  23. 23. evented? arrange I/O
  24. 24. threads? actual work
  25. 25. you?just write code
  26. 26. evented -> threads-> your code
  27. 27. nginx-> trinidad-> web app
  28. 28. Options•Add resources•Use resources better
  29. 29. Concurrency•process concurrency•threaded concurrency•evented concurrency
  30. 30. Process vsreaded
  31. 31. Process Concurrency•1 req = 1 process = 150Mb•“100 reqs?” = 15Gb!•Orchestrated by? Kernel•Unicorn, Passenger
  32. 32. readed Concurrency•1 req = 1 thread = 2kb•“100 reqs?”=150Mb+200kb•Orchestrated by? Kernel•Mongrel*
  33. 33. 1 req =1 thread = 2kb
  34. 34. Process vsreaded
  35. 35. evented -> threads-> your code
  36. 36. readed Concurrency•....•....•....•Mongrel*
  37. 37. readed Concurrency•....•....•....•Mongrel*
  38. 38. “Mongrel isthreaded?!”
  39. 39. 2006 reality:Mongrel * Rails * Ruby1.8 -->process conc.
  40. 40. 20068 reality:“Rails 2.2... rst...threadsafe Rails” --Pratik Naik
  41. 41. 20068 reality:config.threadsafe! http://guides.rubyonrails.org/configuring.html
  42. 42. 20068 reality:# do nothing http://www.sinatrarb.com/configuration.html
  43. 43. 20068 reality:Mongrel * Rails * Ruby1.8 --> process conc.
  44. 44. 200810 reality:“Ruby 1.9.2 is released”
  45. 45. 200810 reality:1.9 has native threads!
  46. 46. 200810 reality:1 thread of Ruby at a time :(
  47. 47. 200810 reality:GIL = GlobalInterpreter Lock :(
  48. 48. 200810 reality: GIL:(“Grumpy Gil”
  49. 49. 200810 reality:Mongrel * Rails * Ruby1.9 -->process conc.
  50. 50. “Mongrel is threaded?!” “Rails is threadsafe”but no threads...
  51. 51. “readsplease!”
  52. 52. rvm install jrubyrvm use jrubygem install railsrails new myapp
  53. 53. gem activerecord- jdbcsqlite3- adaptergem jruby-openssl
  54. 54. Mongrel * Rails * JRuby --> thread concurrency!
  55. 55. 1 req =1 thread = 2kb
  56. 56. “How do Irun JRuby/ Rails?”
  57. 57. How?-> trinidad -> web app
  58. 58. $ gem install trinidad$ trinidad--> localhost:3000
  59. 59. $ git clone git:// github.com/ engineyard/todo.git$ cd todo$ bundle$ rake db:migrate$ trinidad
  60. 60. # config.threadsafe!$ trinidadInfo: max runtimes = 5
  61. 61. config.threadsafe! # yes!$ trinidad -e productionInfo: max runtimes = 1(Thread limit 200)
  62. 62. 1 req =1 thread = 2kb
  63. 63. And its justnormal web app code!
  64. 64. evented -> threads-> your code
  65. 65. nginx-> trinidad-> web app
  66. 66. “I’m scaredof threads”
  67. 67. Web apps are*thread safe
  68. 68. Dr Nic’s Joy of Concurrency“Store state elsewhere;Unless its safe”
  69. 69. evented -> threads-> your code
  70. 70. nginx-> trinidad-> web app
  71. 71. “I came to hear good things aboutEventMachine”
  72. 72. “Evented is cool,threads are not”
  73. 73. “You haven’tsaid ‘nodejs’ once!?”
  74. 74. What isevented? any do somethingI/O? with it
  75. 75. cache hitsreq ? cache web miss app
  76. 76. cache hitsreq ? cache web miss app
  77. 77. req I/O work
  78. 78. req I/O work evented threads
  79. 79. req I/O work evented threads nginx trinidad
  80. 80. nginxvs apache
  81. 81. “Hosted?”500 hrs free!
  82. 82. Come work with us!jruby coretrinidad corerubinius core
  83. 83. “Be a happy developer”-> your code
  84. 84. -> threads-> your code
  85. 85. evented -> threads-> your code
  86. 86. nginx-> trinidad-> web app@drnic | drnicwilliams.com | engineyard.com
  87. 87.  _____________  <  Power  Ruby!  >  -­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐-­‐                        ^__^                      (Oo)_______                        (__)              )/                          <>  ||-­‐-­‐-­‐-­‐w  |                                ||          || @drnic | drnicwilliams.com | engineyard.com

×