Ruby on the Server
    Max out your rubies!
Ruby Version Manager

Use it on the server!
  Multiple isolated Rubies
  Easy environment variables
  Easy install with patch integration
setup/rvm.rb: https://gist.github.com/3946908
Ruby Garbage Collection

 Mark and Sweep, more or less:

   Stop the world

   Traverse and mark reference tree

   Free all unused objects (lazy = free)

   Still no free slots? Allocate 1.8 times more
   slots than on previous alloc
Typical Web Applications

 Small variations in used slots

 100,000 - 200,000 slots in total

 2,000 - 10,000 objects per request

 Most memory occupied by eternal (never
 freed) objects (code, classes, etc...)
Ruby 1.9.3-p286

Ruby 1.9.3 GC is not copy-on-write friendly

By default, 10,000 slots are allocated on
startup

1.8 growth rate means Ruby is likely to
allocate a lot more memory than needed!
falcon patch

Copy-on-write friendly GC

Performance++ for require and Hash

Supported in RVM (--patch falcon)
GC Configuration

We want to allocate enough memory, so that ruby
never has to resize it’s heap

Typically 100,000 to 200,000 initial slots

RUBY_HEAP_MIN_SLOTS environment variable
/usr/bin/time -l rails r "User.first(2000); pp GC.stat"
RAINBOWS


Serves HTTP over a Unix socket

Does not need root

Master preloads app and forks workers

Multiple net concurrency models

Example config: https://gist.github.com/3946921
FOREMAN



Process launcher

Exports to Upstart, launchd and others

Keeps developers happy!
FOREMAN: PROCFILE

  Environment-specific config called Procfile

  Advanced example: https://gist.github.com/3946937
# Simple (no rvm)   Procfile
web:       bundle   exec rainbows
resque:    bundle   exec rake resque:work QUEUE=*
scheduler: bundle   exec rake resque:scheduler

# Run with
$ foreman start web

# Export with
$ sudo foreman export upstart /etc/init -u app_user -a app
Seamless deployments

Tell Rainbows master to restart (USR2)

On spawning a new worker, kill an old one

Extra memory requirement:
1 worker + 1 master

Code here: https://gist.github.com/3946921
Capistrano


Mostly “just works” with rvm-capistrano
rvm_run command + restart_web task here:
https://gist.github.com/3947004

Ruby on the server

  • 1.
    Ruby on theServer Max out your rubies!
  • 2.
    Ruby Version Manager Useit on the server! Multiple isolated Rubies Easy environment variables Easy install with patch integration setup/rvm.rb: https://gist.github.com/3946908
  • 3.
    Ruby Garbage Collection Mark and Sweep, more or less: Stop the world Traverse and mark reference tree Free all unused objects (lazy = free) Still no free slots? Allocate 1.8 times more slots than on previous alloc
  • 4.
    Typical Web Applications Small variations in used slots 100,000 - 200,000 slots in total 2,000 - 10,000 objects per request Most memory occupied by eternal (never freed) objects (code, classes, etc...)
  • 5.
    Ruby 1.9.3-p286 Ruby 1.9.3GC is not copy-on-write friendly By default, 10,000 slots are allocated on startup 1.8 growth rate means Ruby is likely to allocate a lot more memory than needed!
  • 6.
    falcon patch Copy-on-write friendlyGC Performance++ for require and Hash Supported in RVM (--patch falcon)
  • 7.
    GC Configuration We wantto allocate enough memory, so that ruby never has to resize it’s heap Typically 100,000 to 200,000 initial slots RUBY_HEAP_MIN_SLOTS environment variable /usr/bin/time -l rails r "User.first(2000); pp GC.stat"
  • 8.
    RAINBOWS Serves HTTP overa Unix socket Does not need root Master preloads app and forks workers Multiple net concurrency models Example config: https://gist.github.com/3946921
  • 9.
    FOREMAN Process launcher Exports toUpstart, launchd and others Keeps developers happy!
  • 10.
    FOREMAN: PROCFILE Environment-specific config called Procfile Advanced example: https://gist.github.com/3946937 # Simple (no rvm) Procfile web: bundle exec rainbows resque: bundle exec rake resque:work QUEUE=* scheduler: bundle exec rake resque:scheduler # Run with $ foreman start web # Export with $ sudo foreman export upstart /etc/init -u app_user -a app
  • 11.
    Seamless deployments Tell Rainbowsmaster to restart (USR2) On spawning a new worker, kill an old one Extra memory requirement: 1 worker + 1 master Code here: https://gist.github.com/3946921
  • 12.
    Capistrano Mostly “just works”with rvm-capistrano rvm_run command + restart_web task here: https://gist.github.com/3947004

Editor's Notes