Rails, Passenger & Nginx Performance Tuning: Quick Start Guide

At work we’ve been testing out rails as a candidate for production use. The company as a whole is generally cautious (read:slow) about adopting new technology, specifically when it gets outside of the jvm. For that reason I’ve been tasked with preformance testing rails against one of our existing frameworks and in doing so, I’ve learned a little bit about the out of the box settings for rails, passenger and ruby. Here is the summary:

1. Passenger – By default fusion passenger is set to 6 instances. From the fusion documentation, this probably needs adjustment based on your hardware:

The optimal value depends on your system’s hardware and the server’s average load. You should experiment with different values. But generally speaking, the value should be at least equal to the number of CPUs (or CPU cores) that you have. If your system has 2 GB of RAM, then we recommend a value of 30. If your system is a Virtual Private Server (VPS) and has about 256 MB RAM, and is also running other services such as MySQL, then we recommend a value of 2.

For our small AWS instance, with 1.7mb of ram, we raised it to 25. In your nginx.config, add:

passenger_max_pool_size: 25

2. REE – We switched from standard Ruby to Enterprise Edition. REE make better use of system memory. RVM makes this a cake walk.

3. Ruby Garbage collection – much has been written about how rails isn’t that compatible with ruby’s default garbage collection settings. You can read the full details here, here or here. A summary of the change is to create a new file with the following in it. Make sure the last line points to the location of ruby.

#!/bin/bash
export RUBY_HEAP_MIN_SLOTS=1250000
export RUBY_HEAP_SLOTS_INCREMENT=100000
export RUBY_HEAP_SLOTS_GROWTH_FACTOR=1
export RUBY_GC_MALLOC_LIMIT=30000000
export RUBY_HEAP_FREE_MIN=12500
exec "/opt/ree/bin/ruby" "$@"

The tell nginx to read the file. In your nginx.config the following and replace “PATH” with the path of your newly created file.

passenger_ruby PATH;

The garbage collection setting will need to be tunned based on what your application actually does. Go here for a collection of setting that 37s, twitter and others have published.

Results

Your mileage may vary, but for us, we say tremendous gains. Before the chagnes our performance/load test was reporting an average page load time of 2.6 seconds. After the tweaks it went to a crazy low 0.8 seconds! Holy crap.

One Response to “Rails & Passenger on Nginx Performance Tuning: Quick Start Guide”

  1. Look at you, Mr. Fancy Ruby Pants!

    Chris

Leave a Reply