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

Building Asynchronous APIs

Building Asynchronous APIs

flyerhzm

June 07, 2013
Tweet

More Decks by flyerhzm

Other Decks in Technology

Transcript

  1. Building Asynchronous APIs Richard Huang flyerhzm at gmail dot com

    github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  2. 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
  3. Hardware is Cheap, Programmers are Expensive flyerhzm at gmail dot

    com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  4. App Server DB Server Web Server Single Server flyerhzm at

    gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  5. 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
  6. • 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
  7. 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
  8. Sync vs Async Blocking vs Nonblocking flyerhzm at gmail dot

    com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  9. 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
  10. 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
  11. 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
  12. 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
  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
  14. Evented (em-synchrony) • EM + Fiber = No callback! flyerhzm

    at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  15. 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
  16. Rails => Grape Decrease response time flyerhzm at gmail dot

    com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  17. Grape Model Router / Controller / View Fat model Grape

    flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  18. Fat model No need to do any change, nice! flyerhzm

    at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  19. Grape => Grape + Goliath Increase throughput flyerhzm at gmail

    dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  20. 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
  21. 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
  22. Rails => Sinatra Decrease response time flyerhzm at gmail dot

    com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  23. Sinatra Model Router / Controller / View Fat model Sinatra

    flyerhzm at gmail dot com github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  24. 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
  25. CPU bound measured with ab flyerhzm at gmail dot com

    github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  26. 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
  27. IO bound measured with NewRelic flyerhzm at gmail dot com

    github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  28. leaderboard grape goliath 3000 rpm flyerhzm at gmail dot com

    github.com/flyerhzm twitter.com/flyerhzm Friday, June 7, 13
  29. 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
  30. Conclusion • Rails is good • Sinatra/Grape decreases response time

    • Sinatra-synchrony/Goliath increases throughput • Async I/O is awesome! Try Friday, June 7, 13