Thursday, November 15, 2007

Rails on Swiftiply / Mongrel

Hosting Ruby on Rails is complicated, right? Not necessarily. Let's take a look at a fast and flexible way to serve up your Rails application.

Rails on Swiftiply / Mongrel
If you have done any reading or work with Rails you have probably heard of Mongrel. According to the Mongrel site, "Mongrel is a fast HTTP library and server for Ruby".

You may not have heard of Swiftiply yet. But you will. Well, actually I guess you just did. But you will no doubt be hearing more about it. Swiftiply "is a very fast, narrowly targetted clustering proxy". The Swiftiply website says "scaling your web applications should be easy". Sounds good to me. Let's see if it is!

We will assume that you have already properly installed and configured the following:

Version Info
Here are the most recent versions tested at the time of this article.  Different versions may cause varying results.
  • OS: Ubuntu 7.10
  • Ruby: 1.8.6
  • RubyGems: 1.0.1
  • Swiftiply:
  • Mongrel: 1.1.3
  • Rails: 2.0.2

Installation: It's a breeze.
sudo gem install mongrel --include-dependencies
sudo gem install swiftiply --include-dependencies
I told you it was a breeze.

Configuration: Piece of cake.
Let's go ahead and configure Swiftiply first. Swiftiply does not install a configuration file for you, so you are creating a new blank file in the following step:
sudo vim /etc/swiftiply.conf
Here is the configuration that I used:
cluster_address: 123.45.678.9  # Use your IP here
cluster_port: 80
daemonize: true
epoll: true
epoll_descriptors: 20000
- incoming:
outgoing: # Where Mongrels will connect
default: true
docroot: /var/www/ # Rails root
cache_directory: public
redeployable: true
- htm
- html
- txt
Amazingly that is all that is required in the way of configuration. If you wanted to serve up other applications on other domains or sub-domains, simply repeat the "map:" section for each.

Start up Swiftiply with:
swiftiply -c /etc/swiftiply.conf
With Swiftiply running, now all you need to do is start Mongrel(s) from within the directory of the Rails app you wish to serve. We let the system know that we want to use the special Swiftiply version of Mongrel with the "env SWIFT=1" directive. We also tell them to connect to the IP and port that you defined in your swiftiply.conf, like so:
env SWIFT=1 mongrel_rails start -d -a -p 30000

You may also wish to include the directive "-e production", but I ran out of room on the line! ;)

Now if you want multiple mongrels serving up your app, just start another one up exactly the same. Same IP and port.

And now... your done.
And that is it. You have your basic Swiftiply / Mongrel stack setup.

Keep your eyes on Swiftiply as there will be some exciting new features be adding in the coming weeks and months.

With a blank Rails 1.2.5 application I show the following memory usages in "top":

Ruby (Mongrel): 47M
Swiftiply: 17M

For some statistics on performance of Swiftiply, please see the post "Testing Various Configurations of Rails, Merb, Swiftiply and Nginx" on Webficient.

Stuff To Know
Here are a couple things to remember about Swiftiply as you are setting it up:
  • Each application you wish to serve will be linked to ONE port.
  • All mongrels serving that application will connect to the exact same ONE port.
  • Even if you have (n) mongrels, they all connect to the exact same ONE port per application.
  • Swiftiply can serve multiple applications, just repeat the part from "map:" down in the swiftiply.conf file assigning your new application a single unique port.


  1. Awesome tutorial. I get to the swiftiply -c command and get: :in `em_config': private method `split' called for nil:NilClass (NoMethodError)

    I have uninstall and reinstalled several times. I'm on Debian.

  2. Im getting the same error as well when trying to bring up swiftiply. Did anything work out?

  3. Unfortunately no. I ended up just going apache + mongrel for now. Eagerly waiting for when the swiftiply has matured.

  4. Sorry guys. I just went through the steps again on a fresh Ubuntu 7.10 (gutsy) install and it works for me.

    What version of Ruby are you using?? I'm vaguely remembering having a problem on 1.8.5. Perhaps try 1.8.6 and see if your problem goes away.

  5. Nice blog Justin. I had the same problem described above. The problem might be because of white spaces in your yaml file(swiftiply.conf). Problem got solved when I removed some extra white spaces that I accidentally typed in.Hope this helps.