High-Performance Ruby:Evented vs readed
@drnic | drnicwilliams.com | engineyard.com
Dr Nic Williams
________ < Safari > -‐-‐-‐-‐-‐-‐-‐-‐ \ ^__^ \ (xx)\_______ (__)\ )\/\ ^ ||-‐-‐-‐-‐w | || ||
New project/rapid dev
Optimise
(features/$)
(req latency/$)(req thru-put/$)
(bugs/$)
or even $$$$$$$ $$ $
Optimise
New project/rapid dev(features/$)
(performance/$0)
how?
•Add resources•Use resources better
Options
you?just write
code
evented-> threads
-> your code
nginx-> trinidad-> web app
evented?arrange
I/O
threads?actualwork
you?just write
code
evented-> threads
-> your code
nginx-> trinidad-> web app
•Add resources•Use resources better
Options
•process concurrency•threaded concurrency•evented concurrency
Concurrency
•1 req = 1 process = 150Mb•“100 reqs?” = 15Gb!•Orchestrated by? Kernel•Unicorn, Passenger
ProcessConcurrency
•1 req = 1 thread = 2kb•“100 reqs?”=150Mb+200kb•Orchestrated by? Kernel•Mongrel*
readedConcurrency
1 req = 1 thread =
2kb
evented-> threads
-> your code
•....•....•....•Mongrel*
readedConcurrency
•....•....•....•Mongrel*
readedConcurrency
“Mongrel is threaded?!”
Mongrel * Rails * Ruby1.8 -->
process conc.
2006 reality:
20068 reality:“Rails 2.2... "rst...thread
safe Rails”--Pratik Naik
20068 reality:
config.threadsafe!
http://guides.rubyonrails.org/configuring.html
20068 reality:
# do nothing
http://www.sinatrarb.com/configuration.html
Mongrel * Rails * Ruby1.8 -->
process conc.
20068 reality:
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 = Global Interpreter
Lock :(
200810 reality:
GIL:( “Grumpy Gil”
Mongrel * Rails * Ruby1.9 -->
process conc.
200810 reality:
“Mongrel is threaded?!”
“Rails is threadsafe”
but no threads...
rvm install jrubyrvm use jrubygem install railsrails new myapp
gem 'activerecord- jdbcsqlite3- adapter'gem 'jruby-openssl'
Mongrel * Rails * JRuby --> thread
concurrency!
1 req = 1 thread =
2kb
“How do I run 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 just normal web
app code!
evented-> threads
-> your code
nginx-> trinidad-> web app
“I’m scared of threads”
Web apps are*
thread safe
“Store state elsewhere; Unless its safe”
Dr Nic’s Joy of Concurrency
evented-> threads
-> your code
nginx-> trinidad-> web app
“I came to hear good things
about EventMachine”
“Evented is cool, threads are not”
“You haven’t said ‘nodejs’
once!?”
What is evented?
any I/O?
do somethingwith it
cache hits
both
req ?webapp
cachemiss
cache hits
both
req ?webapp
cachemiss
both
req I/O work
both
req I/O workevented threads
both
req I/O workevented threads
trinidadnginx
“Hosted?”
500 hrs free!
jruby core trinidad corerubinius core
Come workwith us!
-> your code
“Be a happy developer”
-> threads-> your code
evented-> threads
-> your code
nginx-> trinidad-> web app
@drnic | drnicwilliams.com | engineyard.com
_____________ < Power Ruby! > -‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐-‐ \ ^__^ \ (Oo)\_______ (__)\ )\/\ <> ||-‐-‐-‐-‐w | || ||
@drnic | drnicwilliams.com | engineyard.com