Slow Clients Are Problematic
Most benchmarks we've seen don't tell you this, and unicorn doesn't care about slow
clients... but you should.
-- The Philosophy Behind unicorn
4. Profit?
• unicorn is not suited for all applications. unicorn
is optimized for applications that are CPU/
memory/disk intensive and spend little time
waiting on external resources (e.g. a database
server or external API)
— The philosophy of unicorn
5. EventMachine / Thin
• Asynchronous IO, but
• Everything you do by default in Rails is blocking.
6. What’s the advantage
compared to unicorn?
* Non blocking (partly)
class Thin::Connection < EM::Connection
# When the request is received and parsed
def receive_data(data)
# anything happening here is blocking
resp = @app.call(Request.new.parse(data))
# handoff to EventMachine, non-blocking
send_data resp.body.join(“”)
end
end
* Persistent connection
10. Asynchronous Rack?
require 'em-http-request'
app = -> env do
http = EM::HttpRequest.new("http://www.google.com").get
http.callback do
env['async.callback'].call([200, {}, http.response])
end
throw :async # read: yield
end
run app
(demo)
11. async_sinatra
• Demo (async_sinatra.ru)
• Good for normal usage
• Test `include Sinatra::Async::Test::Methods`
• Doesn’t work with Rack::URLMap, could be fixed
though
• The middleware has to be registered by the
‘async.callback’
12. Thread safety of MRI
• Native thread & GIL
• Demo (multi_thread.rb)
13. Puma
• Imagine a variation of unicorn that each worker has a
thread pool
• Designed for implementation with real thread
(Rubinius, JRuby)
• Puma still improves MRI's throughput by allowing
blocking IO to be run concurrently
• Cluster mode for MRI
• Thread-safety
14. Thread safety of Rails
• config.threadsafe! (Before Rails4)
• demo (rails.ru)
• Rack::Lock (puma cluster mode downgrade to
unicorn) Don’t gain much with JRuby / Rubinius
15. –Someone on the internet
“The speed of the web server doesn’t matter
that much, you request spends a very little
portion of time in the server.”