Share
Embed
Transcript
59 slides
-
Building Asynchronous APIs Richard Huang flyerhzm at gmail dot com
github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
I’m ... • Currently Ruby/Rails Freelancer • Previously worked for
OpenFeint (GREE) • Author of bullet and rails_best_practices gems flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Hardware is Cheap, Programmers are Expensive flyerhzm at gmail dot
com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
App Server DB Server Web Server Single Server flyerhzm at
gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
App Server DB Server Web Server Load Balancer App Server
DB Server Web Server App Server Web Server App Server Web Server Memcached ... ... flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm ... Friday, June 7, 13 -
Hardwares get Expensive, so... flyerhzm at gmail dot com github.com/flyerhzm
twitter.com/flyerhzm Friday, June 7, 13 -
Is Ruby/Rails so slow? flyerhzm at gmail dot com github.com/flyerhzm
twitter.com/flyerhzm Friday, June 7, 13 -
From my experience, no! flyerhzm at gmail dot com github.com/flyerhzm
twitter.com/flyerhzm Friday, June 7, 13 -
• Rails REE 1.8.7 Mysql 50 ms 60k rpm 13
machines x 6 passenger instances • Rails JRuby 1.7.0 Mysql 30 ms 60k rpm 10 machines x torquebox (5 threads) • Goliath Ruby 1.9.3 Redis 4 ms 240k rpm 4 machines x 4 goliath instances flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Migrating from ruby to ruby, 48 servers cut and10x faster
flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Sync vs Async Blocking vs Nonblocking flyerhzm at gmail dot
com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Blocking IO flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm DB
HTTP IO CPU Friday, June 7, 13 -
Multi Processes DB HTTP IO CPU DB HTTP IO CPU
Process A Process B flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Multi Processes • Parallel on a multi cores cpu •
Easy to manage • Process switching is expensive • Consume many memory flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Multi Threads DB HTTP IO CPU DB HTTP IO CPU
Thread A Thread B flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Multi Threads • Thread switching is cheap • Consume less
memory • Thread safety • GIL flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Evented flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm DB HTTP
IO CPU Friday, June 7, 13 -
Evented (Eventmachine) • No blocking io • No context switching
• Least memory usage • Callback! Callback! Callback! flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Evented (em-synchrony) • EM + Fiber = No callback! flyerhzm
at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Multi Processes + Evented flyerhzm at gmail dot com github.com/flyerhzm
twitter.com/flyerhzm Friday, June 7, 13 -
View Controller Model Skinny controller Fat model Render json response
Rails Router Dispatch request flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Rails => Grape Decrease response time flyerhzm at gmail dot
com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Grape Model Router / Controller / View Fat model Grape
flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Fat model No need to do any change, nice! flyerhzm
at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Grape => Grape + Goliath Increase throughput flyerhzm at gmail
dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Grpae Model Router / Controller / View Fat model Grape
+ Goliath Goliath Non-blocking web server framework flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Goliath with Grape mysql2 => em_mysql2 net/http => em-http-request mongo
=> em-mongo ... flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Rails => Sinatra Decrease response time flyerhzm at gmail dot
com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Sinatra Model Router / Controller / View Fat model Sinatra
flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
Sinatra / Sinatra-synchrony Model Router / Controller / View Fat
model Sinatra-synchrony+Thin Thin Ruby web server based on EM flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
CPU bound measured with ab flyerhzm at gmail dot com
github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
0 5 10 15 20 n1000 c10 n1000 c50 n1000
c100 n1000 c200 n1000 c500 n2000 c1000 rails sinatra grape sinatra threads grape threads sinatra-synchrony + thin grape + goliath rails, sinatra, grape, sinatra threads and grape threads are timed out with n 1000 c 200 flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
IO bound measured with NewRelic flyerhzm at gmail dot com
github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
leaderboard-sinatra threads 3000 rpm flyerhzm at gmail dot com github.com/flyerhzm
twitter.com/flyerhzm Friday, June 7, 13 -
leaderboard-grape threads 3000 rpm flyerhzm at gmail dot com github.com/flyerhzm
twitter.com/flyerhzm Friday, June 7, 13 -
leaderboard grape goliath 3000 rpm flyerhzm at gmail dot com
github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13 -
flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Known issues •
Failed to add newrelic to sinatra-synchrony • Failed to add fiber_pool to goliath Friday, June 7, 13 -
Conclusion • Rails is good • Sinatra/Grape decreases response time
• Sinatra-synchrony/Goliath increases throughput • Async I/O is awesome! Try Friday, June 7, 13