nginx and Passenger

Posted by Ronnie Zownir on April 24, 2009

After taking a liking to nginx, I refused to bail on it when Passenger first arrived on the scene. But now I can have my cake and eat it too! And so can the other holdouts. Nginx and Passenger are truly a dream team for Ruby/Rails web application hosting. I still think very highly of thin. It has served me very well. But the fact of the matter is that the simpler solution is usually the better one. This situation is no exception. I’ve created a second webfaction-rails-environment installation script that you can get here.

The only downside to nginx+Passenger I see with regard to WebFaction is that the reported RSS is higher than the monit+nginx+thin solution. In reality, the nginx+Passenger solution uses far less physical memory (especially with Ruby Enterprise Edition) than the other solution. The reported RSS is higher because it includes memory that is shared among processes/threads. Some memory is counted multiple times. If memory usage is monitored purely on the basis of RSS, then you’re not getting your money’s worth of memory with passenger. “Private Dirty RSS” is a better representation of actual physical memory usage. Find out how to find it in the README of webfaction-rails-environment. The difference can be quite substantial.

Rails Deployment Options 2

Posted by Ronnie Zownir on June 19, 2008

There are quite a number of deployment options for Rails these days. You have a choice on a variety of things and the list of options is ever expanding. What follows is my short list:

Ruby Virtual Machine

Several Ruby implementations can run Rails. Others cannot. The ability to run Rails is a major achievement for alternative Ruby VMs. For detailed comparisons check out Antonio Cangiano’s blog. He does the Ruby shootout.

  • MRI
    • 1.8 standard
    • Ruby 1.8.6 is the recommended version to run Rails on
  • YARV
    • 1.9 standard
    • Significantly faster than MRI
    • Rails is not yet fully compatible with Ruby 1.9
    • Many gems are not compatible with Ruby 1.9
  • JRuby
    • Java implementation of Ruby
    • Runs Rails
  • Rubinius
    • “Ruby in Ruby”
    • Runs Rails
  • Ruby Enterprise Edition
    • From the creators of mod_rails
    • Fork of MRI
    • 33% less memory consumption on average when used with mod_rails
  • MagLev
    • Commercial
    • Pending release
    • Lots of promise in terms of performance and features, but won’t run Rails for some time

For critical production applications, there are really only two implementations you should consider. If you are using mod_rails, go with Ruby Enterprise Edition. Otherwise, the standard Ruby implementation, MRI, is the way to go. The other implementations are progressing rapidly and in time will be good to go for production.

Server Configuration

There is much activity in this area. These are the choices worth noting:

  • nginx + mongrel | thin | ebb | fuzed (yaws)
    • nginx is a powerful lightweight frontend server/reverse proxy/load balancer that can take a licking and keep on ticking
    • mongrel is the veteran backend web server for Ruby on Rails
    • thin is an evented backend server that’s faster than mongrel and supports unix socket connections
    • ebb is an evented backend server written in C that’s faster than thin and also supports unix socket connections, but it uses more memory than thin while idling
    • fuzed allows Rails to be served up by yaws, a server written in Erlang that provides an unparalleled degree of concurrency
  • Apache + Passenger (mod_rails) + Ruby Enterprise Edition
    • New and exciting deployment option for Apache
    • Easy to setup
    • Deploying an app can be as simple is uploading your app
    • mod_rails and Ruby Enterprise Edition, both developed by Phusion, together provide a 33% lower memory footprint (for Rails) on average
    • Integrated monitoring and load balancing – monitors Rails processes and starts/kills them as necessary based on demand
  • LiteSpeed
    • Commercial
    • Relatively easy to setup
    • Better performance than most other solutions
    • Despite its qualities, not amazingly popular

I only mention LiteSpeed because of its performance. Few people actually use it for serious Rails deployments. I omitted lighttpd from the list because nginx has stolen the show. Ancient solutions like fastcgi were also omitted.

I personally use nginx + thin. I have not transitioned to ebb because of higher memory consumption (at least on the low end). I included the fuzed project in my list because I find yaws and Erlang fascinating. Yaws puts Apache to shame when it comes to concurrency. I’m not sure how polished the fuzed project is, but it looks like a contender to me! It’s also good to see cooperation between Ruby and Erlang. Mongrel, thin, and ebb are all good options. It all depends on your needs and preferences.

I have not tried out mod_rails. It is being touted as a breakthrough solution because of how simple it makes deployment. Not to take anything away from it, but my impression is that it is more for deployment novices and people with shared hosting provided by operations like DreamHost. With WebFaction, you have the freedom and ability to build your own stack. I’ve made this a breeze with a shake and bake shell script. Nginx is a better frontend server than Apache in its ability to serve static pages and with regard to memory usage. What would be great is if mongrel/thin/ebb could take advantage of the memory saving features of Ruby Enterprise Edition. I’m sure that the mod_ruby solution is outstanding. I will check it out for myself and report.

Load Balancing

Load balancing allows your applications to scale horizontally.

  • Hardware
    • For very large applications
    • Most advanced
    • Expensive
  • HAProxy
    • For large applications
    • Very advanced
    • Very difficult to setup
  • nginx-upstream-fair
    • Third party module for nginx
    • Adds fair load balancing to nginx (replaces standard round-robin load balancing)
    • Very simple to setup
    • Small to large applications

I use the nginx-upstream-fair module for load balancing. Written by Grzegorz Nosek, the module works very well and is so easy to setup that there is no reason not to do so.

Monitoring

To make sure that your processes are behaving, you need a process monitor.

I use monit. I haven’t tried the god gem, but I’ve heard good things.