Exploring Mongrel... Finally

Posted by Curtis Miller Curtis Miller

Well, it's time for me to learn about mongrel and mongrel cluster.

What is Mongrel?

Mongrel is a fast HTTP library and server for Ruby that is intended for hosting Ruby web applications of any kind using plain HTTP rather than FastCGI or SCGI.

Mongrel was created by Zed Shaw and modeled after a Java web server framework called Simple. It is written in C and Ruby with an HTTP 1.1 parser written in Ragel. Mongrel is open-source, so you can modify it to fit your own needs, but it also supports using GemPlugin as an extension mechanism.

First, install Mongrel from the gem.

sudo gem install mongrel

Now create an application to test that we can use Mongrel.

rails test_mongrel
cd test_mongrel
mongrel_rails start

You should see Mongrel start the development environment on That's it… Well, there are other options that you can pass to Mongrel if you want to customize, find them here. What we have right now is good enough for development purposes.

What is Mongrel Cluster?

Mongrel cluster is a wrapper around Mongrel to allow you to run a “pack of mongrels”. It basically lets you configure and run multiple Mongrel servers. Add to this a load balancing reverse proxy solution (e.g., Apache) and you can have multiple Mongrels running with requests balanced between them. Mongrel cluster was created by Bradley Taylor of RailsMachine.

First, install Mongrel cluster from the gem.

sudo gem install mongrel_cluster

Now, using our application created earlier, we configure Mongrel cluster. (From the test_mongrel directory)

mongrel_rails cluster::configure -p 8000 -N 3

This creates a configuration file in config/mongrel_cluster.yml. The configuration file is chock full of defaults except for the two items we specified on the command line. The first option (-p 8000) specifies the beginning port number. The second option (-N 3) specifies that there will be 3 mongrel instances. Now you can start your cluster.

mongrel_rails cluster::start

You should be able to navigate to your localhost on ports 8000, 8001 and 8002. Give it a try. To see the other options when creating the configuration files, see the docs.

The only thing missing at this point is configuring your load balancing proxy, so your users don't need to specify the port number of the instance they want to make requests on. I have not done this yet, but it looks like the Mongrel site has good documentation for Apache, Lighttpd, Pound and Pen/Balance.

Velocity Labs

Need web application development, maintenance for your existing app, or a third party code review?

Velocity Labs can help.

Hire us!