Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
async Rails 3 stack demo
Branch: master

This branch is 22 commits behind igrigorik:master

Fetching latest commit…

Cannot retrieve the latest commit at this time

Failed to load latest commit information.
app
config
db
doc
lib/tasks
public
script
test
vendor/plugins
.gitignore
Gemfile
README.md
Rakefile
config.ru

README.md

Async Rails 3 stack demo

Simple async demo stack with Rails 3 + EventMachine and Fibers.

  • Hit localhost:3000/widgets to do a 1s async mysql query
  • Hit localhost:3000/widgets/http to make an HTTP call back to /widgets - recursive! :-)
  • Hit localhost:3000/twitter to load a mounted async Sinatra app (reports latests rails 3 tweets)

Howto / example commits:

Requirements:

  • Ruby 1.9.x
  • Async app server (thin)
  • Rails 3

Environment setup:

  • rvm install 1.9.2-preview3
  • rvm gemset create async-rails
  • rvm use 1.9.2-preview3@async-rails
  • gem install rails --pre
  • gem install thin

Starting up Rails:

  • bundle install
  • thin -D start

Test:

ab -c 5 -n 10 http://127.0.0.1:3000/widgets/http

    Concurrency Level:      5
    Time taken for tests:   2.740 seconds
    Complete requests:      10

We're running on a single reactor, so above is proof that we can execute HTTP+MySQL queries in non-blocking fashion on a single run loop. Pushing the stack on my MBP (pool = 200; env = production) results in:

    concurrency       time
      75              73.426
      60              66.411
      50              65.502
      40              78.105
      30              106.624

Looks like a single thin on my MBP peaks ~50 req/s (with internal hit, so 100 req/s total). For more details see http://gist.github.com/445603

Scenario:

  • AB opens 5 concurrent requests (10 total)
  • Each request to /widgets/http opens an async HTTP request to /widgets - aka, we ourselves spawn another 5 requests
  • Because the fiber pool is set to 10, it means we can process all 5 requests within ~1s (each mysql req takes 1s)
  • 10 requests finish in ~2s

So, keep in mind that the size of 'database pool' is basically your concurrency throttle. In example above, we spawn 10 requests, which open another 10 internally, so in total we process 20 req's in ~2s on a single thing server. Just as expected.

Resources:

Something went wrong with that request. Please try again.